标签:信息 cee ini efi void values size plm poj
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 130735 | Accepted: 40585 | |
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 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 //It is made by HolseLee on 17th May 2018 2 //POJ 3468 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<iostream> 8 #include<iomanip> 9 #include<algorithm> 10 #define Fi(i,a,b) for(int i=a;i<=b;i++) 11 using namespace std; 12 typedef long long ll; 13 const int N=1e5+7; 14 ll n,m,sum[N],c[2][N],ans; 15 inline ll lowbit(int x){return x&-x;} 16 inline void add(int k,int x,int y) 17 {for(int i=x;i<=n;i+=lowbit(i))c[k][i]+=y;} 18 inline ll get(int k,int x) 19 {ll ret=0;for(int i=x;i>=1;i-=lowbit(i))ret+=c[k][i];return ret;} 20 int main() 21 { 22 ios::sync_with_stdio(false); 23 cin>>n>>m;int x,y,z;char opt; 24 Fi(i,1,n)cin>>x,sum[i]=sum[i-1]+x; 25 Fi(i,1,m){cin>>opt; 26 if(opt==‘C‘){cin>>x>>y>>z; 27 add(0,x,z);add(0,y+1,-z); 28 add(1,x,x*z);add(1,y+1,-(y+1)*z);} 29 else {cin>>x>>y; 30 ll ans=(sum[y]+(y+1)*get(0,y)-get(1,y)); 31 ans-=(sum[x-1]+x*get(0,x-1)-get(1,x-1)); 32 printf("%lld\n",ans);}} 33 return 0; 34 }
POJ3468 A Simple Problem with Interger [树状数组,差分]
标签:信息 cee ini efi void values size plm poj
原文地址:https://www.cnblogs.com/cytus/p/9052893.html