Advertisement
libobil

Untitled

Aug 21st, 2023
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <cassert>
  5. #include <vector>
  6.  
  7. typedef long long llong;
  8. const int MAXN = 100000 + 10;
  9. const llong INF = 1e18;
  10. const int INTINF = 1e9;
  11.  
  12. int n, q;
  13. struct SegmentTree
  14. {
  15.     struct Node
  16.     {
  17.         llong sum;
  18.         int min, max;
  19.         llong lazy;
  20.    
  21.         Node()
  22.         {
  23.             sum = lazy = 0;
  24.             min = INTINF;
  25.             max = 0;
  26.         }
  27.     };
  28.  
  29.     Node tree[4*MAXN];
  30.     Node combine(Node left, Node right)
  31.     {
  32.         Node res;
  33.         res.sum = left.sum + right.sum;
  34.         res.min = std::min(left.min, right.min);
  35.         res.max = std::max(left.max, right.max);
  36.         return res;
  37.     }
  38.  
  39.     void push(int node, int l, int r)
  40.     {
  41.         if (tree[node].lazy == 0)
  42.         {
  43.             return;
  44.         }
  45.  
  46.         tree[node].sum += tree[node].lazy * (r - l + 1);
  47.         tree[node].min += tree[node].lazy;
  48.         tree[node].max += tree[node].lazy;
  49.        
  50.         if (l < r)
  51.         {
  52.             tree[2*node].lazy += tree[node].lazy;
  53.             tree[2*node + 1].lazy += tree[node].lazy;
  54.         }
  55.  
  56.         tree[node].lazy = 0;
  57.     }
  58.  
  59.     void build(int l, int r, int node, int a[])
  60.     {
  61.         if (l == r)
  62.         {
  63.             tree[node].sum = tree[node].min = tree[node].max = a[l];
  64.             return;
  65.         }
  66.  
  67.         int mid = (l + r) / 2;
  68.         build(l, mid, 2*node, a);
  69.         build(mid + 1, r, 2*node + 1, a);
  70.         tree[node] = combine(tree[2*node], tree[2*node + 1]);
  71.     }
  72.  
  73.     void update(int l, int r, int node, int queryL, int queryR, int queryVal)
  74.     {
  75.         push(node, l, r);
  76.         if (queryR < l || r < queryL)
  77.         {
  78.             return;
  79.         }
  80.  
  81.         if (queryL <= l && r <= queryR)
  82.         {
  83.             tree[node].lazy += queryVal;
  84.             push(node, l, r);
  85.             return;
  86.         }
  87.  
  88.         int mid = (l + r) / 2;
  89.         update(l, mid, 2*node, queryL, queryR, queryVal);
  90.         update(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
  91.         tree[node] = combine(tree[2*node], tree[2*node + 1]);
  92.     }
  93.  
  94.     Node query(int l, int r, int node, int queryL, int queryR)
  95.     {
  96.         if (queryL <= l && r <= queryR)
  97.         {
  98.             return tree[node];
  99.         }
  100.  
  101.         Node res;
  102.         int mid = (l + r) / 2;
  103.         if (queryL <= mid) res = combine(res, query(l, mid, 2*node, queryL, queryR));
  104.         if (mid + 1 <= queryR) res = combine(res, query(mid + 1, r, 2*node + 1, queryL, queryR));
  105.         return res;
  106.     }
  107.  
  108.     void build(int a[])
  109.     {
  110.         build(1, n, 1, a);
  111.     }
  112.  
  113.     void update(int l, int r, int val)
  114.     {
  115.         update(1, n, 1, l, r, val);
  116.     }
  117.  
  118.     Node query(int l, int r)
  119.     {
  120.         return query(1, n, 1, l, r);
  121.     }
  122. };
  123.  
  124. int a[MAXN];
  125. SegmentTree tree;
  126. void solve()
  127. {
  128.     tree.build(a);
  129.     for (int i = 1 ; i <= q ; ++i)
  130.     {
  131.         char qType;
  132.         int l, r;
  133.        
  134.         std::cin >> qType >> l >> r;
  135.         if (qType == '+')
  136.         {
  137.             int val;
  138.             std::cin >> val;
  139.             tree.update(l, r, val);
  140.         } else if (qType == '?')
  141.         {
  142.             SegmentTree::Node res = tree.query(l, r);
  143.             std::cout << res.sum << ' ' << res.min << ' ' << res.max << '\n';
  144.         } else
  145.         {
  146.             assert(false);
  147.         }
  148.     }
  149. }
  150.  
  151. void input()
  152. {
  153.     std::cin >> n >> q;
  154.     for (int i = 1 ; i <= n ; ++i)
  155.     {
  156.         std::cin >> a[i];
  157.     }
  158. }
  159.  
  160. void fastIOI()
  161. {
  162.     std::ios_base :: sync_with_stdio(0);
  163.     std::cout.tie(nullptr);
  164.     std::cin.tie(nullptr);
  165. }
  166.  
  167. int main()
  168. {
  169.     fastIOI();
  170.     input();
  171.     solve();
  172.  
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement