Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int t,n,q;
- const int mxn=(1e5)+6;
- int a[mxn];
- long long st[mxn*4];
- long long su[mxn*4];
- long long laz[mxn*4][2];
- void build(int p,int l,int r)
- {
- if(l==r)st[p]=(a[l]*a[l]),su[p]=a[l];
- else
- {
- int m=(l+r)/2;
- st[p*2]=0;
- st[p*2+1]=0;
- build(p*2,l,m);
- build(p*2+1,m+1,r);
- st[p]=st[p*2]+st[p*2+1];
- su[p]=su[p*2]+su[p*2+1];
- }
- }
- //laz[p][0]: set query
- //laz[p][1]: add query
- void pros(int p,int l,int r)
- {
- if(laz[p][0]!=0)
- {
- st[p]=(laz[p][0]*laz[p][0])*((r-l)+1);
- su[p]=laz[p][0]*((r-l)+1);
- if(l!=r)
- {
- laz[p*2][0]=laz[p][0];
- laz[p*2][1]=0;
- laz[p*2+1][0]=laz[p][0];
- laz[p*2+1][1]=0;
- }
- laz[p][0]=0;
- }
- if(laz[p][1]!=0)
- {
- st[p]=(st[p]+(2*laz[p][1]*su[p])+(laz[p][1]*laz[p][1]*(r-l+1)));
- su[p]=(su[p]+(laz[p][1]*(r-l+1)));
- if(l!=r)
- {
- if(laz[p*2][0]!=0)
- laz[p*2][0]+=laz[p][1];
- else
- laz[p*2][1]+=laz[p][1];
- if(laz[p*2+1][0]!=0)
- laz[p*2+1][0]+=laz[p][1];
- else
- laz[p*2+1][1]+=laz[p][1];
- }
- laz[p][1]=0;
- }
- }
- void update(int p,int l,int r,int i,int j,int xx)
- {
- pros(p,l,r);
- if(r<i||l>j)return;
- if(l>=i&&r<=j)
- {
- laz[p][1]+=xx;
- pros(p,l,r);
- return;
- }
- int m=(l+r)/2;
- update(p*2,l,m,i,j,xx);
- update(p*2+1,m+1,r,i,j,xx);
- st[p]=st[p*2]+st[p*2+1];
- su[p]=su[p*2]+su[p*2+1];
- }
- void change(int p,int l,int r,int i,int j,int xx)
- {
- pros(p,l,r);
- if(r<i||l>j)return;
- if(l>=i&&r<=j)
- {
- laz[p][0]=xx;
- pros(p,l,r);
- return;
- }
- int m=(l+r)/2;
- change(p*2,l,m,i,j,xx);
- change(p*2+1,m+1,r,i,j,xx);
- st[p]=st[p*2]+st[p*2+1];
- su[p]=su[p*2]+su[p*2+1];
- }
- long long rsq(int p,int l,int r,int i,int j)
- {
- pros(p,l,r);
- if(r<i||l>j)return 0;
- if(l>=i&&r<=j)return st[p];
- int m=(l+r)/2;
- return rsq(p*2,l,m,i,j)+rsq(p*2+1,m+1,r,i,j);
- }
- int main()
- {
- scanf("%d",&t);
- for(int test=1;test<=t;test++)
- {
- st[1]=0;
- scanf("%d%d",&n,&q);
- for(int i=0;i<n;i++)scanf("%d",&a[i]);
- build(1,0,n-1);
- printf("Case %d:\n",test);
- int l,r,ty,xx;
- while(q--)
- {
- scanf("%d%d%d",&ty,&l,&r);
- if(ty==0)
- {
- scanf("%d",&xx);
- change(1,0,n-1,l-1,r-1,xx);
- }
- else if(ty==1)
- {
- scanf("%d",&xx);
- update(1,0,n-1,l-1,r-1,xx);
- }
- else printf("%lld\n",rsq(1,0,n-1,l-1,r-1));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement