xBloodY

Segment Tree 3

Jul 27th, 2022
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. void push(vector<int>& t, int v) {
  2.     if (t[v] != -1) {
  3.         t[v * 2 + 1] = t[v];
  4.         t[v * 2 + 2] = t[v];
  5.         t[v] = -1;
  6.     }
  7. }
  8.  
  9. void build(vector<int>& a, vector<int>& t, int v, int tl, int tr) {
  10.     if (tl == tr)
  11.         t[v] = a[tl];
  12.     else {
  13.         int tm = (tl + tr) / 2;
  14.         build(a, t, v * 2 + 1, tl, tm);
  15.         build(a, t, v * 2 + 2, tm + 1, tr);
  16.     }
  17. }
  18.  
  19. void update(vector<int> &t, int v, int tl, int tr, int l, int r, int color) {
  20.     if (l > r)
  21.         return;
  22.     if (l == tl && r == tr)
  23.         t[v] = color;
  24.     else {
  25.         push(t, v);
  26.         int tm = (tl + tr) / 2;
  27.         update(t, v * 2 + 1, tl, tm, l, min(tm, r), color);
  28.         update(t, v * 2 + 2, tm + 1, tr, max(tm + 1, l), r, color);
  29.     }
  30. }
  31.  
  32. int get(vector<int>& t, int v, int tl, int tr, int pos) {
  33.     if (tl == tr)
  34.         return t[v];
  35.     push(t, v);
  36.     int tm = (tl + tr) / 2;
  37.     if (pos <= tm)
  38.         return get(t, v * 2 + 1, tl, tm, pos);
  39.     else
  40.         return get(t, v * 2 + 2, tm + 1, tr, pos);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment