Advertisement
pratiyush7

Segment Tree Lazy Propogation

Nov 12th, 2021
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. // Pratiyush Mishra
  2.  
  3. #include <bits/stdc++.h>
  4. #define ull unsigned long long int
  5. #define ll long long int
  6. #define LL_MAX 9223372036854775807
  7. #define pb push_back
  8. #define pf push_front
  9. #define mp make_pair
  10. #define popb pop_back
  11. #define vl vector<ll>
  12. #define pl pair<ll,ll>
  13. #define bs(v, x) binary_search(v.begin(), v.end(), x)
  14. #define mem1(a) memset(a, -1, sizeof(a))
  15. #define popf pop_front
  16. #define p0(x) cout << x << " "
  17. #define p1(x) cout << x << '\n'
  18. #define p2(x, y) cout << x << " " << y << '\n'
  19. #define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
  20. #define printv(v)                   \
  21.   for (ll i = 0; i < v.size(); ++i) \
  22.     cout << v[i] << " ";            \
  23.   cout << '\n'
  24. #define pr1(x) cout << fixed << setprecision(15) << x << '\n'
  25. #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
  26. // ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
  27. // s.find_by_order(i)  itertor to ith element (0 indexed)
  28. #define mod 1000000007
  29. #define mod1 998244353
  30. #define fio                         \
  31.   ios_base::sync_with_stdio(false); \
  32.   cin.tie(NULL)
  33. #define get(n) \
  34.   ll n;        \
  35.   cin >> n
  36. #define getvec(v, n)         \
  37.   vector<ll> v(n);           \
  38.   for (ll i = 0; i < n; i++) \
  39.     cin >> v[i];
  40. #define getstr(s) \
  41.   string s;       \
  42.   cin >> s
  43. #define all(x) x.begin(), x.end()
  44. #define countBits(x) __builtin_popcount(x)
  45. using namespace std;
  46.  
  47.  
  48. // Solution to the problem: https://codeforces.com/edu/course/2/lesson/5/1/practice/contest/279634/problem/A
  49.  
  50. ll n, q;
  51. ll l, r, pos, val;
  52. const ll M = 100000 * 4 + 50;
  53. ll tree[M], lazy[M], v[M];
  54.  
  55. void build(int tl, int tr, ll node)
  56. {
  57.     if (tl == tr)
  58.     {
  59.         tree[node] = v[tl];
  60.         return;
  61.     }
  62.     int tm = (tl + tr) / 2;
  63.     build(tl, tm, 2 * node);
  64.     build(tm + 1, tr, 2 * node + 1);
  65.     tree[node] = (tree[2 * node] + tree[2 * node + 1]);
  66. }
  67.  
  68. void apply(int tl, int tr, ll node, ll val)
  69. {
  70.     tree[node] += (val * (tr - tl + 1));
  71.     if (tl != tr)
  72.         lazy[node] += val;
  73. }
  74.  
  75. void passdown(int tl, int tr, ll node)
  76. {
  77.     int tm = (tl + tr) / 2;
  78.     apply(tl, tm, 2 * node, lazy[node]);
  79.     apply(tm + 1, tr, 2 * node + 1, lazy[node]);
  80.     lazy[node] = 0;
  81. }
  82.  
  83. void update(int tl, int tr, ll node)
  84. {
  85.     if (tl > r || tr < l)
  86.         return;
  87.     if (tl >= l && tr <= r)
  88.     {
  89.         apply(tl, tr, node, val);
  90.         return;
  91.     }
  92.     passdown(tl, tr, node);
  93.     int tm = (tl + tr) / 2;
  94.     update(tl, tm, 2 * node);
  95.     update(tm + 1, tr, 2 * node + 1);
  96.     tree[node] = (tree[2 * node] + tree[2 * node + 1]);
  97. }
  98.  
  99. ll query(int tl, int tr, int node)
  100. {
  101.     if (tl > r || tr < l)
  102.         return 0;
  103.     if (tl >= l && tr <= r)
  104.         return tree[node];
  105.     passdown(tl, tr, node);
  106.     int tm = (tl + tr) / 2;
  107.     ll ans = query(tl, tm, 2 * node) + query(tm + 1, tr, 2 * node + 1);
  108.     return ans;
  109. }
  110.  
  111. void mainSolve()
  112. {
  113.     cin >> n >> q;
  114.     for (int i = 1; i <= n; i++)
  115.         v[i] = 0;
  116.     build(1, n, 1);
  117.     while (q--)
  118.     {
  119.         int t;
  120.         cin >> t;
  121.         if (t == 1)
  122.         {
  123.             cin >> l >> r >> val;
  124.             ++l;
  125.             update(1, n, 1);
  126.         }
  127.         else
  128.         {
  129.             cin >> l;
  130.             ++l;
  131.             r = l;
  132.             cout << query(1, n, 1) << endl;
  133.         }
  134.     }
  135. }
  136.  
  137. int main()
  138. {
  139.     fio;
  140. #ifndef ONLINE_JUDGE
  141.     freopen("input.txt", "r", stdin);
  142.     freopen("output.txt", "w", stdout);
  143. #endif
  144.     //get(t);
  145.     ll t = 1;
  146.     while (t--)
  147.     {
  148.         mainSolve();
  149.     }
  150.     return 0;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement