poj 3468 A Simple Problem with Integers(线段树)

来源:互联网 发布:js实现分页 编辑:程序博客网 时间:2024/06/05 16:40

A Simple Problem with Integers
Time Limit: 5000MS
Memory Limit: 131072KTotal Submissions: 85145
Accepted: 26414Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

Hint

The sums may exceed the range of 32-bit integers.

解题思路:使用lazy标记。。具体见代码,第一次基本上自己写这个错了好多地方。。。伤心==

#include <iostream>#include <cstdio>using namespace std;long long a[100005],tree[400005],lazy[400005];void build(long long p,long long l,long long r){    lazy[p]=0;    if(l==r){tree[p]=a[l];return;}    long long mid=(l+r)/2;    build(p*2,l,mid);    build(p*2+1,mid+1,r);    tree[p]=tree[p*2]+tree[p*2+1];}void add(long long p,long long l,long long r,long long x,long long y,long long num)//add这部分也要把lazy值往下传。。不要忘记了{    if(x<=l&&r<=y){lazy[p]+=num;tree[p]+=num*(r-l+1);return;}    long long mid=(l+r)/2;    if(lazy[p]!=0)    {        lazy[p*2]+=lazy[p];//是“+=”不是“=”,因为lazy开始的时候可能就不是0        lazy[p*2+1]+=lazy[p];        tree[p*2]+=lazy[p]*(mid-l+1);//这个地方乘以多少要注意,不一定就是(r-l<span style="font-family: Arial, Helvetica, sans-serif;">)..还有我第一次写了个(l-r)..不知道我是怎么想的==都不能更嫌弃自己了==</span>
        tree[p*2+1]+=lazy[p]*(r-mid);        lazy[p]=0;    }    if(x<=mid){add(p*2,l,mid,x,y,num);}//这边注意不能return啊!!!不然后面就没算了    if(y>mid){add(p*2+1,mid+1,r,x,y,num);}    tree[p]=tree[2*p]+tree[2*p+1];}long long finds(long long p,long long l,long long r,long long x,long long y){    if(x<=l&&r<=y)return tree[p];    long long mid=(l+r)/2;    long long ans=0;    if(lazy[p]!=0)    {        lazy[p*2]+=lazy[p];        lazy[p*2+1]+=lazy[p];        tree[p*2]+=lazy[p]*(mid-l+1);        tree[p*2+1]+=lazy[p]*(r-mid);        lazy[p]=0;    }    if(x<=mid)ans+=finds(p*2,l,mid,x,y);    if(y>mid)ans+=finds(p*2+1,mid+1,r,x,y);    return ans;}int main(){    long long N,Q,m,n,i,u;    char query[10];    scanf("%lld%lld",&N,&Q);    for(i=1;i<=N;i++)scanf("%lld",&a[i]);    build(1,1,N);    for(i=0;i<Q;i++)    {        scanf("%s",query);        if(query[0]=='C')        {            scanf("%lld%lld%lld",&m,&n,&u);            add(1,1,N,m,n,u);        }        else if(query[0]=='Q')        {            scanf("%lld%lld",&m,&n);            printf("%lld\n",finds(1,1,N,m,n));        }    }    return 0;}























0 0
原创粉丝点击