标签:pre span owb blog include typedef efi code 树状数组
codevs 1082
白书上很详尽。维护两个东西,一个是差量,也就是哪一段被更新了多少,还有一个也是差量,只不过是记录一共减了多少。(说不清)
然后根据白书上的式子,自己yy一下。还是不是很懂
#include<cstdio> #include<cstring> using namespace std; #define lowbit(x) x&-x #define N 200010 typedef long long ll; int n,q; ll sum[N],t1[N],t2[N]; void add1(int pos,int delta) { for(int i=pos;i<=n;i+=lowbit(i)) t1[i]+=delta; } void add2(int pos,int delta) { for(int i=pos;i<=n;i+=lowbit(i)) t2[i]+=delta; } ll sum1(int pos) { ll ret=0; for(int i=pos;i>0;i-=lowbit(i)) ret+=t1[i]; return ret; } ll sum2(int pos) { ll ret=0; for(int i=pos;i>0;i-=lowbit(i)) ret+=t2[i]; return ret; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&sum[i]); sum[i]+=sum[i-1]; } scanf("%d",&q); while(q--) { int opt,a,b; scanf("%d%d%d",&opt,&a,&b); if(opt==1) { int x; scanf("%d",&x); add1(a,-(a-1)*x); add1(b+1,b*x); add2(a,x); add2(b+1,-x); } else { printf("%lld\n",sum[b]-sum[a-1]+sum1(b)-sum1(a-1)+sum2(b)*b-sum2(a-1)*(a-1)); } } return 0; }
标签:pre span owb blog include typedef efi code 树状数组
原文地址:http://www.cnblogs.com/19992147orz/p/6209539.html