POJ3468 A Simple Problem with Interger [树状数组,差分]
A Simple Problem with Integers
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
The sums may exceed the range of 32-bit integers.
分析:要求是在[l,r]的区间内修改或者查询和,当然会想到树状数组。但是因为每次是区间修改,所以需要转化一下。
首先,假设这里我们只考虑单点查询。新建一个数组b[i]来存储每次的修改信息。对于每一个C l r d,将b[l]加上d,在将b[r+1]减去d,那么每次查询的时候就输出a[x]+(b[x]的前缀和)就可以得到a[x]修改后的值。正确性易证,画图就很好理解了,这里蒟蒻就不画图了(偷懒一波)。
那么再考虑区间查询,易得a[1~x]整体修改的值为Σxi=1Σij=1b[j],推导Σxi=1Σij=1b[j]=Σxi=1(x-i+1)*b[i]=(x+1)Σxi=1b[i]-Σxi=1i*b[i](格式不太好看将就下吧)。那么这题的算法就可以确定了。
建立两个树状数组c0,c1,对于每一个修改操作,执行以下操作:
将c0中的l位置加d,将c0中的r+1位置减d
将c1中的l位置加l*d,将c1中的r+1位置减(r+1)*d
再用sum[]直接记录a[]的前缀和,对于每一个询问指令,输出(sum[r]+(r+1)*get(c0,r)-get(c1,r))-(sum[l-1]+l*get(c0,l-1)-get(c1,l-1))。实际上也就是用的一般的前缀和与树状数组相结合,并且运用了差分的思想。
Code:
//It is made by HolseLee on 17th May 2018 //POJ 3468 #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<iomanip> #include<algorithm> #define Fi(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; const int N=1e5+7; ll n,m,sum[N],c[2][N],ans; inline ll lowbit(int x){return x&-x;} inline void add(int k,int x,int y) {for(int i=x;i<=n;i+=lowbit(i))c[k][i]+=y;} inline ll get(int k,int x) {ll ret=0;for(int i=x;i>=1;i-=lowbit(i))ret+=c[k][i];return ret;} int main() { ios::sync_with_stdio(false); cin>>n>>m;int x,y,z;char opt; Fi(i,1,n)cin>>x,sum[i]=sum[i-1]+x; Fi(i,1,m){cin>>opt; if(opt=='C'){cin>>x>>y>>z; add(0,x,z);add(0,y+1,-z); add(1,x,x*z);add(1,y+1,-(y+1)*z);} else {cin>>x>>y; ll ans=(sum[y]+(y+1)*get(0,y)-get(1,y)); ans-=(sum[x-1]+x*get(0,x-1)-get(1,x-1)); printf("%lld\n",ans);}} return 0; }
蒟蒻写博客不易,如果有误还请大佬们提出
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:HolseLee
博客地址:www.cnblogs.com/cytus
个人邮箱:1073133650@qq.com
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:HolseLee
博客地址:www.cnblogs.com/cytus
个人邮箱:1073133650@qq.com