1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #define int long long #define MAXN 100010*4 using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } int n,m,a[MAXN],tree[MAXN],add[MAXN],N; inline void build(){ for(int i=N-1;i>=1;i--) tree[i]=tree[i<<1]+tree[i<<11]; } inline void update(int l,int r,int k){ int CountLeft=0,CountRight=0,now=1; for(l=N+l-1,r=N+r+1;l^r^1;l>>=1,r>>=1,now<<=1){ tree[l]+=k*CountLeft; tree[r]+=k*CountRight; if(l%2==0) add[l^1]+=k,tree[l^1]+=k*now,CountLeft+=now; if(r%2==1) add[r^1]+=k,tree[r^1]+=k*now,CountRight+=now; } for(;l;l>>=1,r>>=1){ tree[l]+=k*CountLeft; tree[r]+=k*CountRight; } } inline int query(int l,int r){ int CountLeft=0,CountRight=0,now=1,res=0; for(l=N+l-1,r=N+r+1;l^r^1;l>>=1,r>>=1,now<<=1){ if(add[l]) res+=add[l]*CountLeft; if(add[r]) res+=add[r]*CountRight; if(l%2==0) res+=tree[l^1],CountLeft+=now; if(r%2==1) res+=tree[r^1],CountRight+=now; } for(;l;l>>=1,r>>=1){ res+=add[l]*CountLeft; res+=add[r]*CountRight; } return res; } signed main(){ n=read();m=read(); for(N=1;N<=n+1;N<<=1) ; for(int i=N+1;i<=N+n;i++) tree[i]=read(); build(); for(int op,x,y,k,i=1;i<=m;i++){ op=read(); if(op==1){ x=read();y=read();k=read(); update(x,y,k); }else if(op==2){ x=read();y=read(); write(query(x,y));putchar('\n'); } } return 0; }
|