ec1117

Untitled

Nov 26th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. SPARSE SEG
  2.  
  3. const int SZ = 1<<17;
  4. template<class T> struct node {
  5.     T val = 0; node<T>* c[2];
  6.     node() { c[0] = c[1] = NULL; }
  7.     void upd(int ind, T v, int L = 0, int R = SZ-1) { // add v
  8.         if (L == ind && R == ind) { val += v; return; }
  9.         int M = (L+R)/2;
  10.         if (ind <= M) {
  11.             if (!c[0]) c[0] = new node();
  12.             c[0]->upd(ind,v,L,M);
  13.         } else {
  14.             if (!c[1]) c[1] = new node();
  15.             c[1]->upd(ind,v,M+1,R);
  16.         }
  17.         val = 0; F0R(i,2) if (c[i]) val += c[i]->val;
  18.     }
  19.     T query(int lo, int hi, int L = 0, int R = SZ-1) { // query sum of segment
  20.         if (hi < L || R < lo) return 0;
  21.         if (lo <= L && R <= hi) return val;
  22.         int M = (L+R)/2; T res = 0;
  23.         if (c[0]) res += c[0]->query(lo,hi,L,M);
  24.         if (c[1]) res += c[1]->query(lo,hi,M+1,R);
  25.         return res;
  26.     }
  27.     void UPD(int ind, node* c0, node* c1, int L = 0, int R = SZ-1) {
  28.         if (L != R) {
  29.             int M = (L+R)/2;
  30.             if (ind <= M) {
  31.                 if (!c[0]) c[0] = new node();
  32.                 c[0]->UPD(ind,c0?c0->c[0]:NULL,c1?c1->c[0]:NULL,L,M);
  33.             } else {
  34.                 if (!c[1]) c[1] = new node();
  35.                 c[1]->UPD(ind,c0?c0->c[1]:NULL,c1?c1->c[1]:NULL,M+1,R);
  36.             }
  37.         }
  38.         val = (c0?c0->val:0)+(c1?c1->val:0);
  39.     }
  40. };
  41.  
  42. BITSEG
  43. /**
  44.  * Description: BIT of SegTrees. $x\in (0,SZ), y\in [0,SZ)$.
  45.  * Memory: O(N\log^2 N)
  46.  */
  47.  
  48. #include "../1D Range Queries (9.2)/SparseSeg (9.2).h"
  49.  
  50. template<class T> struct BITseg {
  51.     node<T> seg[SZ];
  52.     BITseg() { F0R(i,SZ) seg[i] = node<T>(); }
  53.     void upd(int x, int y, int v) { // add v
  54.         for (; x < SZ; x += x&-x) seg[x].upd(y,v); }
  55.     T query(int x, int yl, int yr) {
  56.         T res = 0; for (; x; x-=x&-x) res += seg[x].query(yl,yr);
  57.         return res; }
  58.     T query(int xl, int xr, int yl, int yr) { // query sum of rectangle
  59.         return query(xr,yl,yr)-query(xl-1,yl,yr); }
  60. };
  61.  
  62. BITTREE NOT INCLUDED
  63.  
  64. SEGSEG
  65. /**
  66.  * Description: SegTree of SegTrees. $x,y\in [0,SZ).$
  67.  * Memory: O(N\log^2 N)
  68.  */
  69.  
  70. #include "../1D Range Queries (9.2)/SparseSeg (9.2).h"
  71.  
  72. template<class T> struct Node {
  73.     node<T> seg; Node* c[2];
  74.     Node() { c[0] = c[1] = NULL; }
  75.     void upd(int x, int y, T v, int L = 0, int R = SZ-1) { // add v
  76.         if (L == x && R == x) { seg.upd(y,v); return; }
  77.         int M = (L+R)/2;
  78.         if (x <= M) {
  79.             if (!c[0]) c[0] = new Node();
  80.             c[0]->upd(x,y,v,L,M);
  81.         } else {
  82.             if (!c[1]) c[1] = new Node();
  83.             c[1]->upd(x,y,v,M+1,R);
  84.         }
  85.         seg.upd(y,v); // only for addition
  86.         // seg.UPD(y,c[0]?&c[0]->seg:NULL,c[1]?&c[1]->seg:NULL);
  87.     }
  88.     T query(int x1, int x2, int y1, int y2, int L = 0, int R = SZ-1) { // query sum of rectangle
  89.         if (x1 <= L && R <= x2) return seg.query(y1,y2);
  90.         if (x2 < L || R < x1) return 0;
  91.         int M = (L+R)/2; T res = 0;
  92.         if (c[0]) res += c[0]->query(x1,x2,y1,y2,L,M);
  93.         if (c[1]) res += c[1]->query(x1,x2,y1,y2,M+1,R);
  94.         return res;
  95.     }
  96. };
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment