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

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 3468

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 abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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 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 #include <math.h>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <algorithm>
  5 using namespace std;
  6
  7 int a[100005],sum[450005],lazy[450005],ql,qr,c;
  8
  9 void down(int o,int l,int r)
 10 {
 11     if(lazy[o]!=0)
 12     {
 13         int mid=(l+r)/2;
 14         lazy[o*2]=lazy[o*2]+lazy[o];
 15         lazy[o*2+1]=lazy[o*2+1]+lazy[o];
 16         sum[o*2]=sum[o*2]+lazy[o]*(mid-l+1);
 17         sum[o*2+1]=sum[o*2+1]+lazy[o]*(r-mid);
 18         lazy[o]=0;
 19     }
 20 }
 21
 22 void up(int o)
 23 {
 24     sum[o]=sum[o*2]+sum[o*2+1];
 25 }
 26
 27 void build(int o,int l,int r)
 28 {
 29     if(l==r)
 30     {
 31         sum[o]=a[l];
 32         return;
 33     }
 34     int mid=(l+r)/2;
 35     build(o*2,l,mid);
 36     build(o*2+1,mid+1,r);
 37     up(o);
 38 }
 39
 40 void update(int o,int l,int r)
 41 {
 42     if(ql<=l && qr>=r)
 43     {
 44         lazy[o]=lazy[o]+c;
 45         sum[o]=sum[o]+c*(r-l+1);
 46         return;
 47     }
 48     down(o,l,r);
 49     int mid=(l+r)/2;
 50     if(ql<=mid)
 51         update(o*2,l,mid);
 52     if(qr>mid)
 53         update(o*2+1,mid+1,r);
 54     up(o);
 55 }
 56
 57 int query(int o,int l,int r)
 58 {
 59     if(ql<=l && qr>=r)
 60     {
 61         return sum[o];
 62     }
 63     down(o,l,r);
 64     int mid=(l+r)/2,ans=0;
 65     if(ql<=mid)
 66         ans=ans+query(o*2,l,mid);
 67     if(qr>mid)
 68         ans=ans+query(o*2+1,mid+1,r);
 69     up(o);
 70     return ans;
 71 }
 72
 73 int main()
 74 {
 75     int n,q,i;
 76     char sre[10];
 77     while(scanf("%d %d",&n,&q)!=EOF)
 78     {
 79         memset(sum,0,sizeof(sum));
 80         memset(lazy,0,sizeof(lazy));
 81         for(i=1;i<=n;i++)
 82             scanf("%d",&a[i]);
 83
 84         build(1,1,n);
 85         for(i=1;i<=q;i++)
 86         {
 87             scanf("%s %d %d",sre,&ql,&qr);
 88             if(sre[0]==‘C‘)
 89             {
 90                 scanf("%d",&c);
 91                 update(1,1,n);
 92             }
 93             else
 94             {
 95                 printf("%d\n",query(1,1,n));
 96             }
 97         }
 98     }
 99     return 0;
100 }

64位

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <algorithm>
  4 using namespace std;
  5
  6 long long a[100005],sum[450005],lazy[450005],c;
  7 int ql,qr;
  8
  9 void down(int o,int l,int r)
 10 {
 11     if(lazy[o]!=0)
 12     {
 13         int mid=(l+r)/2;
 14         lazy[o*2]=lazy[o*2]+lazy[o];
 15         lazy[o*2+1]=lazy[o*2+1]+lazy[o];
 16         sum[o*2]=sum[o*2]+lazy[o]*(mid-l+1);
 17         sum[o*2+1]=sum[o*2+1]+lazy[o]*(r-mid);
 18         lazy[o]=0;
 19     }
 20 }
 21
 22 void up(int o)
 23 {
 24     sum[o]=sum[o*2]+sum[o*2+1];
 25 }
 26
 27 void build(int o,int l,int r)
 28 {
 29     if(l==r)
 30     {
 31         sum[o]=a[l];
 32         return;
 33     }
 34     int mid=(l+r)/2;
 35     build(o*2,l,mid);
 36     build(o*2+1,mid+1,r);
 37     up(o);
 38 }
 39
 40 void update(int o,int l,int r)
 41 {
 42     if(ql<=l && qr>=r)
 43     {
 44         lazy[o]=lazy[o]+c;
 45         sum[o]=sum[o]+c*(r-l+1);
 46         return;
 47     }
 48     down(o,l,r);
 49     int mid=(l+r)/2;
 50     if(ql<=mid)
 51         update(o*2,l,mid);
 52     if(qr>mid)
 53         update(o*2+1,mid+1,r);
 54     up(o);
 55 }
 56
 57 long long query(int o,int l,int r)
 58 {
 59     if(ql<=l && qr>=r)
 60     {
 61         return sum[o];
 62     }
 63     down(o,l,r);
 64     int mid=(l+r)/2;
 65     long long ans=0;
 66     if(ql<=mid)
 67         ans=ans+query(o*2,l,mid);
 68     if(qr>mid)
 69         ans=ans+query(o*2+1,mid+1,r);
 70     up(o);
 71     return ans;
 72 }
 73
 74 int main()
 75 {
 76     int n,q,i;
 77     char sre[10];
 78     while(scanf("%d %d",&n,&q)!=EOF)
 79     {
 80         memset(sum,0,sizeof(sum));
 81         memset(lazy,0,sizeof(lazy));
 82         for(i=1;i<=n;i++)
 83             scanf("%lld",&a[i]);
 84         build(1,1,n);
 85         for(i=1;i<=q;i++)
 86         {
 87             scanf("%s %d %d",sre,&ql,&qr);
 88             if(sre[0]==‘C‘)
 89             {
 90                 scanf("%lld",&c);
 91                 update(1,1,n);
 92             }
 93             else
 94             {
 95                 printf("%lld\n",query(1,1,n));
 96             }
 97         }
 98     }
 99     return 0;
100 }

时间: 2024-08-02 11:04:34

A Simple Problem with Integers 线段树 成段更新的相关文章

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case 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

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

线段树功能 update:成段更新  query:区间求和 #include <iostream> #include <string> #include <cstdio> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; typedef long long ll; const int MAXN = 111111; ll sum[MAXN<&l

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

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 77486   Accepted: 23862 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

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

题目:poj 3468 A Simple Problem with Integers 题意:给出n个数,两种操作 1:l -- r 上的所有值加一个值val 2:求l---r 区间上的和 分析:线段树成段更新,成段求和 树中的每个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每个值要加的值 对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val .下面的区间标记num += val 对于求和操作,每次进行延迟更新,把num值

poj3468A Simple Problem with Integers(线段树+成段更新)

题目链接: huangjing题意: 给n个数,然后有两种操作. [1]Q a b 询问a到b区间的和. [2]C a b c将区间a到b的值都增加c. 思路: 线段树成段更新的入门题目..学会使用lazy即可.还需要注意的是,lazy的时候更改是累加,而不是直接修改..有可能连续几次进行修改操作..注意这一点就好了... 题目: Language: Default A Simple Problem with Integers Time Limit: 5000MS   Memory Limit:

线段树(成段更新) POJ 3468 A Simple Problem with Integers

题目传送门 1 /* 2 线段树-成段更新:裸题,成段增减,区间求和 3 注意:开long long:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using namespace std; 11 12 #define lson l, mid, rt <<

poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

题目链接 题意: 只有这两种操作 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. 分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久. val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一. 1 #include <iostream

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac