线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

来源:互联网 发布:java转义html特殊字符 编辑:程序博客网 时间:2024/05/16 04:25

A Simple Problem with Integers

Time Limit: 10000MS
Memory Limit: 65536K

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 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

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


解题心得:

  1. 这是一个最简单的线段树,题意就是给你一系列数,每次询问l到r的和或者每次在l到r之间的每一个数加上一个数。很简单啊,但是万万没想到比赛居然崩在这个题上面,将r和R传参的时候写反了,tm样例和所有自己造的数据都过了,哎,已砍手。google翻译还吧这题翻译成了神题,我去。
  2. 这个题还是很有教训的,线段树的代码比较麻烦,要不停的向上维护,向下传递,不停的传递参数,所以在写的时候一定要注意,写慢一点,不然线段树出现了bug很难找,思路不复杂别死在了手贱上面。线段树的lazy标记的时候一定要记得下移,以及在每次递归之后记得向上维护,向上维护的时候一定要注意父节点和两个子节点的关系,该传递一些什么,别和lazy下移的时候搞混了。

#include<cstring>#include<stdio.h>#include<string>#include<queue>#include<algorithm>#include<stack>#include<math.h>using namespace std;const int maxn = 1e6+100;struct node{    long long l,r,sum,lazy;}tree[maxn<<2];void pushup(long long root){    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;}void pushdown(long long root){    if(tree[root].lazy == 0)        return ;    tree[root<<1].lazy += tree[root].lazy;    tree[root<<1|1].lazy += tree[root].lazy;    tree[root<<1].sum += (tree[root<<1].r - tree[root<<1].l + 1)*tree[root].lazy;    tree[root<<1|1].sum += (tree[root<<1|1].r - tree[root<<1|1].l + 1)*tree[root].lazy;    tree[root].lazy = 0;}void buildtree(long long l,long long r,long long root){    tree[root].l = l;    tree[root].r = r;    tree[root].lazy = 0;    if(l == r)    {        scanf("%lld",&tree[root].sum);        return ;    }    long long mid = (l + r) >> 1;    buildtree(l,mid,root<<1);    buildtree(mid+1,r,root<<1|1);    pushup(root);}long long query(long long L,long long R,long long l,long long r,long long root){    if(l == L && R ==r)    {        return tree[root].sum;    }    long long mid = (l + r)>>1;    pushdown(root);    if(R <= mid)    {        return query(L,R,l,mid,root<<1);    }    else if(L > mid)    {        return query(L,R,mid+1,r,root<<1|1);    }    else        return query(mid+1,R,mid+1,r,root<<1|1)+ query(L,mid,l,mid,root<<1);//这里手贱找了一个小时的bug    pushup(root);}void add(long long L,long long R,long long l,long long r,long long root,long long h){    if(l == L && R ==r)    {        tree[root].lazy += h;        tree[root].sum += (r - l +1)*h;        return;    }    pushdown(root);    long long mid = (l + r) >> 1;    if(R <= mid)        add(L,R,l,mid,root<<1,h);    else if(L > mid)        add(L,R,mid+1,r,root<<1|1,h);    else    {        add(L,mid,l,mid,root<<1,h);        add(mid+1,R,mid+1,r,root<<1|1,h);    }    pushup(root);}int main(){    long long n,m;    while(scanf("%lld%lld",&n,&m) != EOF)    {        long long sum = 0;        buildtree(1,n,1);        while(m--)        {            long long a,b,h;            char c[10];//尽量别输入%c,容易挂掉,%s挺好的            scanf("%s",&c);//这里也要注意一下输入的问题,不同的字符对应的不同个数的int输入            if(c[0] == 'C')            {                long long h;                scanf("%lld%lld%lld",&a,&b,&h);                add(a,b,1,n,1,h);            }            else if(c[0] == 'Q')            {                scanf("%lld%lld",&a,&b);                sum = query(a,b,1,n,1);                printf("%lld\n",sum);            }        }    }    return 0;}
阅读全文
0 0
原创粉丝点击