halfo

Segment Tree Template

Jul 28th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. struct node {
  2.     int idx, lazy, lhs, rhs;
  3.     node (int idx = 0, int lhs = 0, int rhs = 0) : idx (idx), lhs (lhs), rhs (rhs) {
  4.         // lazy সেট করা নেই, করে নিতে হবে, সাথে অন্যান্য value গুলাও
  5.         // normal single node আর temp (identity) যদি একই ভাবে init করা না যায় তাহলে if (lhs == rhs && idx) {
  6.         // temp কে তখন আলাদা ভাবে init করে নিতে হবে
  7.         if (lhs == rhs) {
  8.         } else {
  9.         }
  10.     }
  11.  
  12.     bool operator < (const node &temp) const {}
  13.     friend ostream &operator << (ostream &ostr, node &temp) {
  14.         ostr << "{" << temp.lazy << ", " << ", " << temp.lhs << ", " << temp.rhs << "}";
  15.         return ostr;
  16.     }
  17.  
  18.     void normalize (int addLazy) { /* normalize মানে current node এর lazy এর সাথে শুধু নতুন lazy (addLazy) যোগ করা  */ }
  19.     void modify (node &le, node &ri) { /* left child আর right child থেকে current node এর value গুলা সেট করতে হবে */ }
  20.     void split ();
  21.     void merge ();
  22. } tree [MAX_N << 2];
  23.  
  24. void node :: split () {
  25.     if (!lazy) return;
  26.     // lazy অনুসারে এইখানে current node এর value গুলা সেট করতে হবে, lazy তখন 0 হয়ে যাবে
  27.  
  28.     if (lhs == rhs) { lazy = 0; return; }
  29.     tree [idx << 1].normalize (lazy);
  30.     tree [idx << 1 | 1].normalize (lazy);
  31.     lazy = 0;
  32. }
  33.  
  34. void node :: merge () {
  35.     tree [idx << 1].split ();
  36.     tree [idx << 1 | 1].split ();
  37.     modify (tree [idx << 1], tree [idx << 1 | 1]);
  38. }
  39.  
  40. void build (int idx, int lhs, int rhs) {
  41.     tree [idx] = node (idx, lhs, rhs);
  42.     if (lhs == rhs) return;
  43.  
  44.     int mid = (lhs + rhs) >> 1;
  45.     build (idx << 1, lhs, mid);
  46.     build (idx << 1 | 1, mid + 1, rhs);
  47.     tree [idx].merge ();
  48. }
  49.  
  50. node query (node &cur, int qLhs, int qRhs) {
  51.     int mid = (cur.lhs + cur.rhs) >> 1;
  52.     node temp, right;
  53.     cur.split ();
  54.  
  55.     if (qLhs <= cur.lhs && cur.rhs <= qRhs) return cur;
  56.     if (qLhs <= mid) temp = query (tree [cur.idx << 1], qLhs, qRhs);
  57.     if (qRhs > mid) { right = query (tree [cur.idx << 1 | 1], qLhs, qRhs); temp.modify (temp, right); }
  58.     return temp;
  59. }
  60.  
  61. void update (node &cur, int uLhs, int uRhs, int lazy) {
  62.     int mid = (cur.lhs + cur.rhs) >> 1;
  63.     if (uLhs <= cur.lhs && cur.rhs <= uRhs) {
  64.         cur.normalize (lazy);
  65.         cur.split ();
  66.         return;
  67.     }
  68.  
  69.     cur.split ();
  70.     if (uLhs <= mid) update (tree [cur.idx << 1], uLhs, uRhs, lazy);
  71.     if (uRhs > mid) update (tree [cur.idx << 1 | 1], uLhs, uRhs, lazy);
  72.     cur.merge ();
  73. }
  74.  
  75. void build  (int n) { build (1, 1, n); }
  76. node query  (int qLhs, int qRhs) { return query (tree [1], qLhs, qRhs); }
  77. void update (int pos, int val) { update (tree [1], pos, pos, val); }
  78. void update (int uLhs, int uRhs, int lazy) { update (tree [1], uLhs, uRhs, lazy); }
Advertisement
Add Comment
Please, Sign In to add comment