Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MX = 1e5+69;
- typedef long long ll;
- int S[MX],n,q;
- struct Node
- {
- ll storage,Init;
- Node()
- {
- storage=Init=0;
- }
- } tree[4*MX];
- Node merge(Node a,Node b)
- {
- Node ret;
- ret.Init=a.Init+b.Init;
- ret.storage=a.storage+b.storage;
- return ret;
- }
- void pushin(Node& a,Node& b,Node& c)
- {
- if(c.Init==c.storage)
- {
- a.Init=a.storage;
- b.Init=b.storage;
- }
- if(c.Init==0)
- {
- a.Init=0;
- b.Init=0;
- }
- }
- int d;
- void updateL(int x,int l,int r,int &y)
- {
- if(l>d||y==0)return;
- if(l!=r)
- pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
- if(r<=d){
- if(tree[x].storage-tree[x].Init<=y)
- {
- y-=tree[x].storage-tree[x].Init;
- tree[x].Init=tree[x].storage;
- return;
- }
- }
- if(l==r)
- {
- tree[x].Init+=y;
- y=0;
- return;
- }
- updateL(2*x+1,(l+r)/2+1,r,y);
- updateL(2*x,l,(l+r)/2,y);
- tree[x]=merge(tree[x*2],tree[x*2+1]);
- }
- void updateR(int x,int l,int r,int &y)
- {
- if(r<d||y==0)return;
- if(l!=r)
- pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
- if(l>=d){
- if(tree[x].storage-tree[x].Init<=y)
- {
- y-=tree[x].storage-tree[x].Init;
- tree[x].Init=tree[x].storage;
- return;
- }
- }
- if(l==r)
- {
- tree[x].Init+=y;
- y=0;
- return;
- }
- updateR(2*x,l,(l+r)/2,y);
- updateR(2*x+1,(l+r)/2+1,r,y);
- tree[x]=merge(tree[x*2],tree[x*2+1]);
- }
- int search(int x=1,int l=1,int r=n)
- {
- if(l>d||r<d)return 0;
- if(l!=r)
- pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
- if(l==r)
- {
- return tree[x].Init;
- }
- return search(2*x,l,(l+r)/2)+search(2*x+1,(l+r)/2+1,r);
- }
- int st,en;
- void clear(int x=1,int l=1,int r=n)
- {
- if(l>en||r<st)return;
- if(l!=r)
- pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
- if(st<=l&&r<=en)
- {
- tree[x].Init=0;
- return;
- }
- clear(2*x,l,(l+r)/2);
- clear(2*x+1,(l+r)/2+1,r);
- tree[x]=merge(tree[x*2],tree[x*2+1]);
- }
- void build(int x=1,int l=1,int r=n)
- {
- if(l==r)
- {
- tree[x].Init=0;
- tree[x].storage=S[l];
- return;
- }
- build(2*x,l,(l+r)/2);
- build(2*x+1,(l+r)/2+1,r);
- tree[x]=merge(tree[x*2],tree[x*2+1]);
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- cin>>n>>q;
- memset(S,0,sizeof S);
- for(int i=1; i<=n; i++)
- scanf("%d",S+i);
- for(int i=1;i<4*MX-69;i++)
- tree[i].Init=tree[i].storage=0;
- build();
- for(int i=1; i<=q; i++)
- {
- int t;
- scanf("%d",&t);
- if(t==1)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- d=x;
- updateL(1,1,n,y);
- }
- else if(t==2)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- d=x;
- updateR(1,1,n,y);
- }
- else if(t==3)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- st=x,en=y;
- clear(1,1,n);
- }
- else
- {
- int x;
- scanf("%d",&x);
- d=x;
- printf("%d\n",search());
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement