码迷,mamicode.com
首页 > 其他好文 > 详细

A Simple Problem with Integers POJ - 3468

时间:2017-08-15 15:09:47      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:com   cst   inpu   second   build   interval   efi   sim   nes   

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.
 
不是很明白pushdown这条语句。。。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define lson l , m , rt << 1
 6 #define rson m+1,r , rt << 1 | 1
 7 using namespace std;
 8 typedef long long ll;
 9 
10 const int maxn=200000;
11 
12 int n,q;
13 ll sum[maxn<<2],add[maxn<<2];
14 
15 void Pushup(int rt){ sum[rt]=sum[rt<<1]+sum[(rt<<1)|1]; }
16 
17 void Pushdown(int rt,int m){
18     if(add[rt]){
19         add[rt<<1]+=add[rt];
20         sum[rt<<1]+=(m-(m>>1))*add[rt];
21         add[(rt<<1)|1]+=add[rt];
22         sum[(rt<<1)|1]+=(m>>1)*add[rt];
23         add[rt]=0;
24     }
25 }
26 
27 void Build(int l,int r,int rt){
28     add[rt]=0;
29     if(l==r) { scanf("%lld",&sum[rt]); return; }
30     int m=(l+r)>>1;
31     Build(lson);
32     Build(rson);
33     Pushup(rt);
34 }
35 
36 void Update(int L,int R,int c,int l,int r,int rt){
37     if(L<=l&&r<=R){
38         add[rt]+=c;
39         sum[rt]+=(ll)c*(r-l+1);
40         return ;
41     }
42     Pushdown(rt,r-l+1);
43     int m=(l+r)>>1;
44     if(L<=m) Update(L,R,c,lson);
45     if(R>m)  Update(L,R,c,rson);
46     Pushup(rt);
47 }
48 
49 ll Query(int L,int R,int l,int r,int rt){
50     if(L<=l&&r<=R) return sum[rt];
51     Pushdown(rt,r-l+1);
52     int m=(l+r)>>1;
53     ll temp=0;
54     if(L<=m) temp+=Query(L,R,lson);
55     if(R>m)  temp+=Query(L,R,rson);
56     return temp;
57 }
58 
59 int main()
60 {   cin>>n>>q;
61     Build(1,n,1);
62     while(q--){
63         char op[2];
64         scanf("%s",op);
65         int a,b,c;
66         if(op[0]==Q){
67             scanf("%d%d",&a,&b);
68             printf("%lld\n",Query(a,b,1,n,1));
69         }
70         if(op[0]==C){
71             scanf("%d%d%d",&a,&b,&c);
72             Update(a,b,c,1,n,1);
73         }
74     }
75     return 0;
76 }

 

A Simple Problem with Integers POJ - 3468

标签:com   cst   inpu   second   build   interval   efi   sim   nes   

原文地址:http://www.cnblogs.com/zgglj-com/p/7364913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!