Advertisement
vlatkovski

Segment tree (range assignment, single element query)

Apr 6th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<int, int> pi;
  5. typedef vector<int> vi;
  6.  
  7.  
  8.  
  9. const int MAXN = 300005;
  10.  
  11. int n;
  12. int arr[MAXN];
  13.  
  14. int st[4*MAXN];
  15. int marked[4*MAXN];
  16.  
  17. void build(int v, int tl, int tr) {
  18.     if (tl == tr) {
  19.         st[v] = arr[tl];
  20.     } else {
  21.         int tm = (tl + tr) / 2;
  22.         build(2*v, tl, tm);
  23.         build(2*v+1, tm+1, tr);
  24.         st[v] = st[2*v] + st[2*v+1];
  25.     }
  26. }
  27.  
  28. void push(int v) {
  29.     if (marked[v]) {
  30.         st[2*v] = st[2*v+1] = st[v];
  31.         marked[2*v] = marked[2*v+1] = true;
  32.         marked[v] = false;
  33.     }
  34. }
  35.  
  36. void update(int v, int tl, int tr, int l, int r, int new_val) {
  37.     if (l > r) return;
  38.     if (l == tl && tr == r) {
  39.         st[v] = new_val;
  40.         marked[v] = true;
  41.     } else {
  42.         push(v);
  43.         int tm = (tl + tr) / 2;
  44.         update(2*v, tl, tm, l, min(r, tm), new_val);
  45.         update(2*v+1, tm+1, tr, max(l, tm+1), r, new_val);
  46.     }
  47. }
  48.  
  49. int getvalue(int v, int tl, int tr, int pos) {
  50.     if (tl == tr) return st[v];
  51.     push(v);
  52.     int tm = (tl + tr) / 2;
  53.     if (pos <= tm) {
  54.         return getvalue(2*v, tl, tm, pos);
  55.     } else {
  56.         return getvalue(2*v+1, tm+1, tr, pos);
  57.     }
  58. }
  59.  
  60. void Main() {
  61.     cout << "Enter N, the size of the array, and then N numbers\n";
  62.     cin >> n;
  63.     for (int i = 0; i < n; ++i) {
  64.         cin >> arr[i];
  65.     }
  66.     build(1, 0, n-1);
  67.     cout << "First type of query:\n";
  68.     cout << "--> 1 p\n";
  69.     cout << "--> Get the element at position p (0-indexed)\n";
  70.     cout << "Second type of query:\n";
  71.     cout << "--> 2 l r x\n";
  72.     cout << "--> Update elements in range [l, r] to new value v\n";
  73.     while (true) {
  74.         int t;
  75.         cin >> t;
  76.         if (t == 1) {
  77.             int p;
  78.             cin >> p;
  79.             cout << "arr[" << p << "] = " << getvalue(1, 0, n-1, p) << "\n";
  80.         } else {
  81.             int l, r, x;
  82.             cin >> l >> r >> x;
  83.             update(1, 0, n-1, l, r, x);
  84.         }
  85.     }
  86. }
  87.  
  88. int main() {
  89. //    ios::sync_with_stdio(false);
  90. //    cin.tie(0);
  91.     #ifdef _DEBUG
  92. //    freopen("in.txt", "r", stdin);
  93. //    freopen("out.txt", "w", stdout);
  94.     #endif
  95.     #ifndef _DEBUG
  96.     #endif
  97.     Main();
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement