Advertisement
Ryan4253

Untitled

Aug 14th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. //#define endl '\n'
  6. #define pb push_back
  7. #define pii pair<int, int>
  8. #define mp make_pair
  9. #define F first;
  10. #define S second;
  11.  
  12. struct Node{
  13.     int val;
  14.     int l, r;
  15.     int lson, rson;
  16.     int aTag, bTag;
  17. };
  18.  
  19. const int N = 100000 + 5;
  20. int a[N];
  21. Node st[2 * N];
  22. int st_ptr = 0;
  23.  
  24. void build(int l, int r, int index){
  25.     st[index].l = l; st[index].r = r;
  26.     if(l + 1 == r){
  27.         st[index].val = a[l];
  28.         return;
  29.     }
  30.  
  31.     int mid = (st[index].l + st[index].r) / 2;
  32.     st[index].lson = ++st_ptr;
  33.     st[index].rson = ++st_ptr;
  34.  
  35.     build(l, mid, st[index].lson);
  36.     build(mid, r, st[index].rson);
  37.  
  38.     st[index].val = max(st[st[index].lson].val, st[st[index].rson].val);
  39. }
  40.  
  41. void push(int index){
  42.     if(st[index].aTag != 0){
  43.         st[index].val += st[index].aTag;
  44.         if(st[index].l + 1 != st[index].r){
  45.             if(st[st[index].lson].bTag != 0){
  46.                 st[st[index].lson].bTag += st[index].aTag;
  47.             }
  48.             else{
  49.                 st[st[index].lson].aTag += st[index].aTag;
  50.             }
  51.  
  52.             if(st[st[index].rson].bTag != 0){
  53.                 st[st[index].rson].bTag += st[index].aTag;
  54.             }
  55.             else{
  56.                 st[st[index].rson].aTag += st[index].aTag;
  57.             }
  58.         }
  59.     }
  60.     else if(st[index].bTag != 0){
  61.         st[index].val = st[index].bTag;
  62.         if(st[index].l + 1 != st[index].r){
  63.             st[st[index].lson].aTag = 0;
  64.             st[st[index].lson].bTag = st[index].bTag;
  65.  
  66.             st[st[index].rson].aTag = 0;
  67.             st[st[index].rson].bTag = st[index].bTag;
  68.         }
  69.     }
  70.  
  71.     st[index].aTag = st[index].bTag = 0;
  72. }
  73.  
  74. int query(int l, int r, int index){
  75.     push(index);
  76.     if(st[index].l == l && st[index].r == r){
  77.         return st[index].val;
  78.     }
  79.     int mid = (st[index].l + st[index].r) / 2;
  80.  
  81.     if(l >= mid){
  82.         return query(l, r, st[index].rson);
  83.     }
  84.     else if(r <= mid){
  85.         return query(l, r, st[index].lson);
  86.     }
  87.     else{
  88.         return max(query(l, mid, st[index].lson), query(mid, r, st[index].rson));
  89.     }
  90. }
  91.  
  92. void setTo(int l, int r, int val, int index){
  93.     push(index);
  94.     if(st[index].l == l && st[index].r == r){
  95.         st[index].aTag = 0;
  96.         st[index].bTag = val;        
  97.         return;
  98.     }
  99.  
  100.     int mid = (st[index].l + st[index].r) / 2;
  101.  
  102.     if(l >= mid){
  103.         setTo(l, r, val, st[index].rson);
  104.     }
  105.     else if(mid >= r){
  106.         setTo(l, r, val, st[index].lson);
  107.     }
  108.     else{
  109.         setTo(l, mid, val, st[index].lson);
  110.         setTo(mid, r, val, st[index].rson);
  111.     }
  112.  
  113.     st[index].val = st[index].bTag;
  114. }
  115.  
  116. void modify(int l, int r, int val, int index){
  117.     push(index);
  118.     if(st[index].l == l && st[index].r == r){
  119.         if(st[index].bTag != 0){
  120.             st[index].bTag += val;
  121.         }
  122.         else{
  123.             st[index].aTag += val;
  124.         }
  125.         return;
  126.     }
  127.  
  128.     int mid = (st[index].l + st[index].r) / 2;
  129.  
  130.     if(l >= mid){
  131.         modify(l, r, val, st[index].rson);
  132.     }
  133.     else if(mid >= r){
  134.         modify(l, r, val, st[index].lson);
  135.     }
  136.     else{
  137.         modify(l, mid, val, st[index].lson);
  138.         modify(mid, r, val, st[index].rson);
  139.     }
  140.  
  141.     st[index].val = max(st[st[index].lson].val + st[st[index].lson].aTag, st[st[index].rson].val + st[st[index].rson].aTag);
  142. }
  143.  
  144. void solve(){
  145.     int n, q; cin >> n >> q;
  146.     for(int i = 0; i < n; i++){
  147.         cin >> a[i];
  148.     }
  149.  
  150.     build(0, n, 0);
  151.  
  152.     for(int i = 0; i < q; i++){
  153.         int t, l, r, x; cin >> t;
  154.         if(t == 1){
  155.             cin >> l >> r >> x;
  156.             modify(l-1, r, x, 0);
  157.  
  158.         }
  159.         else if(t == 2){
  160.             cin >> l >> r;
  161.             cout << query(l-1, r, 0) << endl;
  162.         }
  163.         else{
  164.             cin >> l >> r >> x;
  165.             setTo(l-1, r, x, 0);
  166.         }
  167.     }
  168. }
  169.  
  170. int main(){
  171.   ios::sync_with_stdio(0); cin.tie(0);
  172.   int n = 1;
  173.   while(n--){
  174.     solve();
  175.   }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement