POJ - 3468 A Simple Problem with Integers
Posted 西北会法语
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ - 3468 A Simple Problem with Integers相关的知识,希望对你有一定的参考价值。
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define ls o<<1 5 #define rs o<<1|1 6 #define lson L,mid,ls 7 #define rson mid+1,R,rs 8 #define middnf int mid=(L+R)>>1; 9 #define ll long long 10 11 using namespace std; 12 13 const int maxn=100005; 14 ll mx[maxn<<2],lazy[maxn<<2]; 15 int n,m,x,y,z; 16 17 void pushup(int o) 18 { 19 mx[o]=mx[ls]+mx[rs]; 20 } 21 22 void pushdown(int o,int m) 23 { 24 if(!lazy[o]) return; 25 lazy[ls] += lazy[o]; 26 lazy[rs] += lazy[o]; 27 mx[ls]+=(m-(m>>1))*lazy[o]; 28 mx[rs]+=(m>>1)*lazy[o]; 29 lazy[o] = 0; 30 } 31 32 void build(int L,int R,int o) 33 { 34 if(L==R) 35 { 36 scanf("%lld",&mx[o]); 37 return ; 38 } 39 middnf; 40 build(lson); 41 build(rson); 42 pushup(o); 43 //cout<<o<<" "<<mx[o]<<endl; 44 } 45 46 void update(int l,int r,int L,int R,int o,int v){//区间更新 47 if(l<=L&&R<=r) 48 { 49 mx[o] += (ll)v*(R-L+1); 50 lazy[o] += v; 51 return; 52 } 53 pushdown(o,R-L+1); 54 middnf; 55 if(l<=mid) update(l,r,lson,v); 56 if(r>mid) update(l,r,rson,v); 57 pushup(o); 58 } 59 60 ll query(int l,int r,int L,int R,int o) 61 { 62 if(l<=L&&R<=r) 63 return mx[o]; 64 pushdown(o,R-L+1); 65 middnf; 66 ll ret = 0; 67 if(l<=mid) 68 ret = ret+query(l,r,lson); 69 if(r>mid) 70 ret = ret+query(l,r,rson); 71 return ret; 72 } 73 74 int main() 75 { 76 char c[5]; 77 while(~scanf("%d %d",&n,&m)) 78 { 79 build(1,n,1); 80 for(int i=0;i<m;i++) 81 { 82 scanf("%s",c); 83 if(c[0]==‘Q‘) 84 { 85 scanf("%d%d",&x,&y); 86 ll man=query(x,y,1,n,1); 87 printf("%lld\n",man); 88 } 89 else 90 { 91 scanf("%d%d%d",&x,&y,&z); 92 update(x,y,1,n,1,z); 93 } 94 } 95 } 96 97 98 return 0; 99 }
以上是关于POJ - 3468 A Simple Problem with Integers的主要内容,如果未能解决你的问题,请参考以下文章
A Simple Problem with Integers POJ - 3468
POJ - 3468 A Simple Problem with Integers
[poj3468]A Simple Problem with Integers
POJ3468 A Simple Problem with Integers 分块