树状数组区间修改,区间查询
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 #define maxn 200010 7 #define lowbit(x) x&(-x) 8 #define LL long long 9 inline int read() 10 { 11 int s=0,f=1; 12 char ch=getchar(); 13 while(ch<'0'||ch>'9') 14 { 15 if(ch=='-') 16 f=-1; 17 ch=getchar(); 18 } 19 while(ch>='0'&&ch<='9') 20 s=s*10+ch-'0',ch=getchar(); 21 return s*f; 22 } 23 int n,m; 24 class fenwick 25 { 26 LL c1[maxn],c2[maxn]; 27 int i; 28 public: 29 inline void update(int x,int w) 30 { 31 for(i=x;i<=n;i+=lowbit(i)) 32 { 33 c1[i]+=w; 34 c2[i]+=x*w; 35 } 36 } 37 inline LL query(int x) 38 { 39 LL ans=0; 40 for(i=x;i>0;i-=lowbit(i)) 41 ans+=(x+1)*c1[i]-c2[i]; 42 return ans; 43 } 44 }T; 45 int s[maxn]; 46 int main() 47 { 48 int op,l,r,w,i; 49 n=read(); 50 for(i=1;i<=n;i++) 51 s[i]=s[i-1]+read(); 52 m=read(); 53 for(i=1;i<=m;i++) 54 { 55 op=read(); 56 l=read(); 57 r=read(); 58 if(op==1) 59 { 60 w=read(); 61 T.update(l,w); 62 T.update(r+1,-w); 63 } 64 else 65 printf("%lld\n",T.query(r)-T.query(l-1)+s[r]-s[l-1]); 66 } 67 }