poj 3468 A Simple Problem with Integers 线段树 成段更新

来源:互联网 发布:东莞大朗淘宝电商培训 编辑:程序博客网 时间:2024/06/08 06:43
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 112353 Accepted: 34930Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1,A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... ,Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915线段树加lazy思想lazy 思想:在a~b期间上加c,暂时没必要加到每个点(能偷懒就偷懒嘛),在表示区间的root结构体上增加一个inc域,将要加的值赋给这个inc域,然后就不要再往下了。

      在求区间和时,将root中的inc值赋给要求的区间,并且将该节点的root置零。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;struct st{    int l;    int r;    ll n;//记录区间的加的值    ll sum;//区间的求和值}t[400000];void build(int l,int r,int k){    t[k].l=l;    t[k].r=r;    t[k].n=0;    if(t[k].l==t[k].r)    {        scanf("%lld",&t[k].sum);        return ;    }    int mid=(t[k].l+t[k].r)>>1;    build(l,mid,2*k);    build(mid+1,r,2*k+1);    t[k].sum=t[k*2].sum+t[k*2+1].sum;}void push(int k,int num){    if(t[k].n)    {        t[k<<1].n+=t[k].n;        t[k<<1|1].n+=t[k].n;        t[k<<1].sum+=t[k].n*(num-(num>>1));        t[k<<1|1].sum+=t[k].n*(num>>1);        t[k].n=0;    }}void insert(int n,int l,int r,int k){    if(t[k].l==l&&t[k].r==r)    {        t[k].n+=n;        t[k].sum+=(ll)n*(r-l+1);        return ;    }    if(t[k].l==t[k].r)        return ;    push(k,t[k].r-t[k].l+1);    int mid=(t[k].l+t[k].r)>>1;    if(r<=mid)        insert(n,l,r,2*k);    else        if(l>mid)        insert(n,l,r,2*k+1);    else    {        insert(n,l,mid,2*k);        insert(n,mid+1,r,2*k+1);    }    t[k].sum=t[k*2].sum+t[k*2+1].sum;}ll search(int l,int r,int k){    if(t[k].l==l&&t[k].r==r)    {        return t[k].sum;    }    push(k,t[k].r-t[k].l+1);    ll res=0;    int mid=(t[k].l+t[k].r)>>1;    if(r<=mid)        res+=search(l,r,2*k);    else    if(l>mid)        res+=search(l,r,2*k+1);    else    {        res+=search(l,mid,2*k);        res+=search(mid+1,r,2*k+1);    }    return res;}int main(){    int n,m;    int i,temp;    int a,b,c;    char s[5];    while(~scanf("%d%d",&n,&m))    {        build(1,n,1);        for(i=1;i<=m;i++)        {            scanf("%s",s);            if(s[0]=='Q')            {                scanf("%d%d",&a,&b);                printf("%lld\n",search(a,b,1));            }            else            {                scanf("%d%d%d",&a,&b,&c);                insert(c,a,b,1);            }        }    }    return 0;}



阅读全文
0 0
原创粉丝点击