Advertisement
erfanul007

LOJ 1112 seg

Mar 1st, 2021
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5.  
  6. #define Max 2000009
  7. #define read()             freopen("input.txt", "r", stdin)
  8. #define write()            freopen("output.txt", "w", stdout)
  9.  
  10. int tree[Max],arr[Max];
  11.  
  12. void fastIO()
  13. {
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(NULL);
  16. }
  17.  
  18. void BuildTree(int node,int L,int R)
  19. {
  20.     if(L==R){
  21.         tree[node] = arr[L];
  22.         return;
  23.     }
  24.     int mid = (L+R)/2;
  25.     int Left = 2*node;
  26.     int Right = Left+1;
  27.     BuildTree(Left,L,mid);
  28.     BuildTree(Right,mid+1,R);
  29.     tree[node] = tree[Left] + tree[Right];
  30. }
  31.  
  32. void UpdateTree(int node,int L,int R,int index,int val)
  33. {
  34.     if(L==index && R==index){
  35.         tree[node]=tree[node]+val;
  36.         return;
  37.     }
  38.     if(L>index || R<index) return;
  39.     int mid = (L+R)/2;
  40.     int Left = 2*node;
  41.     int Right = Left+1;
  42.     UpdateTree(Left,L,mid,index,val);
  43.     UpdateTree(Right,mid+1,R,index,val);
  44.     tree[node] = tree[Left] + tree[Right];
  45. }
  46.  
  47. void UpdateZero(int node,int L,int R,int index)
  48. {
  49.     if(L==index && R==index){
  50.         printf("%d\n",tree[node]);
  51.         tree[node]=0;
  52.         return;
  53.     }
  54.     if(L>index || R<index) return;
  55.     int mid = (L+R)/2;
  56.     int Left = 2*node;
  57.     int Right = Left+1;
  58.     UpdateZero(Left,L,mid,index);
  59.     UpdateZero(Right,mid+1,R,index);
  60.     tree[node] = tree[Left] + tree[Right];
  61. }
  62.  
  63. int RangeSum(int node,int L,int R,int x,int y)
  64. {
  65.     if(L>=x && R<=y){
  66.         return tree[node];
  67.     }
  68.     if(L>y || R<x) return 0;
  69.     int mid = (L+R)/2;
  70.     int Left = 2*node;
  71.     int Right = Left+1;
  72.     int sum1 = RangeSum(Left,L,mid,x,y);
  73.     int sum2 = RangeSum(Right,mid+1,R,x,y);
  74.     return sum1+sum2;
  75. }
  76.  
  77. int main()
  78. {
  79.     //read();
  80.     //write();
  81.     //fastIO();
  82.     int t;
  83.     scanf("%d",&t);
  84.     for(int i=1;i<=t;i++){
  85.         int n,q;
  86.         scanf("%d %d",&n,&q);
  87.         for(int j=1;j<=n;j++){
  88.             scanf("%d",&arr[j]);
  89.         }
  90.         BuildTree(1,1,n);
  91.         printf("Case %d:\n",i);
  92.         while(q--){
  93.             int x,l,r;
  94.             scanf("%d",&x);
  95.             if(x==1){
  96.                 scanf("%d",&l);
  97.                 UpdateZero(1,1,n,l+1);
  98.             }
  99.             else{
  100.                 scanf("%d %d",&l,&r);
  101.                 if(x==2){
  102.                     UpdateTree(1,1,n,l+1,r);
  103.                 }
  104.                 else{
  105.                     printf("%d\n",RangeSum(1,1,n,l+1,r+1));
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement