Advertisement
shamiul93

gss1

Apr 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. //for GSS1 remove update part from main function
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define INT -1000000
  6. int k;
  7.  
  8.  
  9.  
  10.  
  11.  
  12. struct node
  13. {
  14.     int result,pre,suf,sum;
  15.     void split(node &a, node &b) {}
  16.  
  17.  
  18.     void merge(node a, node b)
  19.     {
  20.         sum = a.sum + b.sum ;
  21.         pre = max(a.pre, (a.sum + b.pre));
  22.         suf = max(b.suf, (b.sum + a.suf));
  23.         result = max(a.suf + b.pre,max(a.result, b.result));
  24.     }
  25.  
  26.  
  27.     node()
  28.     {
  29.         result = pre = suf = sum = INT;
  30.     }
  31.     node(int temp)
  32.     {
  33.         result = pre = suf = sum = temp;
  34.     }
  35. } tree[131080];
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. void update(int pos)
  46. {
  47.     pos>>=1;
  48.     while(pos!=0)
  49.     {
  50.         tree[pos].merge(tree[pos*2],tree[pos*2 + 1]);
  51.         pos>>=1;
  52.     }
  53. }
  54. node range_query(int root, int left_most, int right_most, int l,int r)
  55. {
  56.     if( l <= left_most && r >= right_most )
  57.         return tree[root];
  58.  
  59.     int l_child = (root<<1), r_child = l_child + 1, mid = (left_most + right_most )>>1 ;
  60.  
  61.     tree[root].split(tree[l_child],tree[r_child]);
  62.  
  63.     node l_node = node(), r_node = node();
  64.  
  65.     if(l <= mid)
  66.         l_node = range_query(l_child, left_most, mid, l, r);
  67.     if(r > mid)
  68.         r_node = range_query(r_child, mid + 1, right_most, l, r);
  69.  
  70.     tree[root].merge(tree[l_child], tree[r_child]);
  71.  
  72.     node temp = node();
  73.     temp.merge(l_node,r_node);
  74.  
  75.     return temp;
  76. }
  77. int main()
  78. {
  79.     int n,temp,l,r;
  80.     scanf("%d",&n);
  81.     k = ceil(log(n)/log(2));
  82.     int pos = (1<<k);
  83.     int a[n];
  84.     for(int i=0; i<n; i++)
  85.     {
  86.         scanf("%d",&temp);
  87.         tree[pos+i] = node(temp);
  88.         update(pos+i);
  89.     }
  90.     int m,c;
  91.  
  92.     scanf("%d",&m);
  93.     while(m--)
  94.     {
  95.         scanf("%d%d%d",&c,&l,&r);
  96.         if(c==1)
  97.             printf("%d\n",(range_query(1,1,pos,l,r)).result);
  98.         else if(c==0)// Update part-> remove this condition for GSS1.
  99.         {
  100.             tree[pos+l-1] = node(r);
  101.             update(pos + l -1);
  102.         }
  103.     }
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement