Advertisement
jeff69

Untitled

May 9th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MX = 1e5+69;
  4. typedef long long ll;
  5. int S[MX],n,q;
  6. struct Node
  7. {
  8.     ll storage,Init;
  9.     Node()
  10.     {
  11.         storage=Init=0;
  12.     }
  13. } tree[4*MX];
  14. Node merge(Node a,Node b)
  15. {
  16.     Node ret;
  17.     ret.Init=a.Init+b.Init;
  18.     ret.storage=a.storage+b.storage;
  19.     return ret;
  20. }
  21. void pushin(Node& a,Node& b,Node& c)
  22. {
  23.    if(c.Init==c.storage)
  24.    {
  25.        a.Init=a.storage;
  26.        b.Init=b.storage;
  27.    }
  28.    if(c.Init==0)
  29.    {
  30.        a.Init=0;
  31.        b.Init=0;
  32.    }
  33. }
  34. int d;
  35. void updateL(int x,int l,int r,int &y)
  36. {
  37.     if(l>d||y==0)return;
  38.     if(l!=r)
  39.         pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
  40.     if(r<=d){
  41.         if(tree[x].storage-tree[x].Init<=y)
  42.         {
  43.             y-=tree[x].storage-tree[x].Init;
  44.             tree[x].Init=tree[x].storage;
  45.             return;
  46.         }
  47.     }
  48.     if(l==r)
  49.     {
  50.         tree[x].Init+=y;
  51.         y=0;
  52.         return;
  53.     }
  54.     updateL(2*x+1,(l+r)/2+1,r,y);
  55.     updateL(2*x,l,(l+r)/2,y);
  56.     tree[x]=merge(tree[x*2],tree[x*2+1]);
  57. }
  58. void updateR(int x,int l,int r,int &y)
  59. {
  60.     if(r<d||y==0)return;
  61.     if(l!=r)
  62.         pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
  63.     if(l>=d){
  64.         if(tree[x].storage-tree[x].Init<=y)
  65.         {
  66.            y-=tree[x].storage-tree[x].Init;
  67.             tree[x].Init=tree[x].storage;
  68.             return;
  69.         }
  70.     }
  71.     if(l==r)
  72.     {
  73.         tree[x].Init+=y;
  74.           y=0;
  75.         return;
  76.     }
  77.     updateR(2*x,l,(l+r)/2,y);
  78.     updateR(2*x+1,(l+r)/2+1,r,y);
  79.     tree[x]=merge(tree[x*2],tree[x*2+1]);
  80. }
  81. int search(int x=1,int l=1,int r=n)
  82. {
  83.      if(l>d||r<d)return 0;
  84.     if(l!=r)
  85.         pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
  86.     if(l==r)
  87.     {
  88.         return tree[x].Init;
  89.     }
  90.     return search(2*x,l,(l+r)/2)+search(2*x+1,(l+r)/2+1,r);
  91. }
  92. int st,en;
  93. void clear(int x=1,int l=1,int r=n)
  94. {
  95.     if(l>en||r<st)return;
  96.     if(l!=r)
  97.         pushin(tree[x*2],tree[x*2+1],tree[x]);/// what if it was a leaf
  98.     if(st<=l&&r<=en)
  99.     {
  100.         tree[x].Init=0;
  101.         return;
  102.     }
  103.     clear(2*x,l,(l+r)/2);
  104.     clear(2*x+1,(l+r)/2+1,r);
  105.     tree[x]=merge(tree[x*2],tree[x*2+1]);
  106. }
  107. void build(int x=1,int l=1,int r=n)
  108. {
  109.     if(l==r)
  110.     {
  111.         tree[x].Init=0;
  112.         tree[x].storage=S[l];
  113.         return;
  114.     }
  115.     build(2*x,l,(l+r)/2);
  116.     build(2*x+1,(l+r)/2+1,r);
  117.     tree[x]=merge(tree[x*2],tree[x*2+1]);
  118. }
  119. int main()
  120. {
  121.     int t;
  122.     cin>>t;
  123.     while(t--)
  124.     {
  125.         cin>>n>>q;
  126.         memset(S,0,sizeof S);
  127.         for(int i=1; i<=n; i++)
  128.             scanf("%d",S+i);
  129.  
  130.         for(int i=1;i<4*MX-69;i++)
  131.             tree[i].Init=tree[i].storage=0;
  132.        build();
  133.         for(int i=1; i<=q; i++)
  134.         {
  135.             int t;
  136.             scanf("%d",&t);
  137.             if(t==1)
  138.             {
  139.                 int x,y;
  140.                 scanf("%d%d",&x,&y);
  141.                 d=x;
  142.                 updateL(1,1,n,y);
  143.             }
  144.             else if(t==2)
  145.             {
  146.                 int x,y;
  147.                 scanf("%d%d",&x,&y);
  148.                 d=x;
  149.                 updateR(1,1,n,y);
  150.             }
  151.             else if(t==3)
  152.             {
  153.                 int x,y;
  154.                 scanf("%d%d",&x,&y);
  155.                 st=x,en=y;
  156.                 clear(1,1,n);
  157.             }
  158.             else
  159.             {
  160.                 int x;
  161.                 scanf("%d",&x);
  162.                 d=x;
  163.                 printf("%d\n",search());
  164.             }
  165.         }
  166.     }
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement