From cf76ecc1f0a65038adf40ae2189dd2dde8a5dd6c Mon Sep 17 00:00:00 2001 From: zscoder Date: Fri, 7 Oct 2016 17:22:24 +0800 Subject: [PATCH] Added Treap --- Data Structures Class Template.cpp | 102 +++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 6 deletions(-) diff --git a/Data Structures Class Template.cpp b/Data Structures Class Template.cpp index 4b92a03..d0540f2 100644 --- a/Data Structures Class Template.cpp +++ b/Data Structures Class Template.cpp @@ -1035,13 +1035,12 @@ struct ConvexHull return m * x + c; } }; - #define sz(x) (int)(x).size() deque d; bool irrelevant(Line Z) { - if (sz(d) < 2) return false; + if (int(d.size()) < 2) return false; - Line X = d[sz(d)-2], Y = d[sz(d)-1]; + Line X = d[int(d.size())-2], Y = d[int(d.size())-1]; return (X.c - Z.c) * (Y.m - X.m) <= (X.c - Y.c) * (Z.m - X.m); } @@ -1052,7 +1051,7 @@ struct ConvexHull d.push_back(l); } ll query(ll x) { - while (sz(d) > 1 && (d[0].c - d[1].c <= x * (d[1].m - d[0].m))) d.pop_front(); + while (int(d.size()) > 1 && (d[0].c - d[1].c <= x * (d[1].m - d[0].m))) d.pop_front(); return d.front().pass(x); } }; @@ -1534,10 +1533,102 @@ struct Tree }; //End Tree +//Start Treap (see https://threads-iiith.quora.com/Treaps-One-Tree-to-Rule-em-all-Part-2) +struct Treap +{ + struct node + { + int prior,siz; + ll val;//value stored in the array + ll sum;//whatever info you want to maintain in segtree for each node + ll lazy;//whatever lazy update you want to do + struct node *l,*r; + }; + + typedef node* pnode; + + int sz(pnode t) + { + return t?t->siz:0; + } + void upd_sz(pnode t) + { + if(t)t->siz=sz(t->l)+1+sz(t->r); + } + void lazy(pnode t) + { + if(!t || !t->lazy)return; + t->val+=t->lazy;//operation of lazy + t->sum+=t->lazy*sz(t); + if(t->l)t->l->lazy+=t->lazy;//propagate lazy + if(t->r)t->r->lazy+=t->lazy; + t->lazy=0; + } + void reset(pnode t) + { + if(t)t->sum = t->val;//no need to reset lazy coz when we call this lazy would itself be propagated + } + void combine(pnode& t,pnode l,pnode r){//combining two ranges of segtree + if(!l || !r)return void(t = l?l:r); + t->sum = l->sum + r->sum; + } + void operation(pnode t){//operation of segtree + if(!t)return; + reset(t);//reset the value of current node assuming it now represents a single element of the array + lazy(t->l);lazy(t->r);//imp:propagate lazy before combining t->l,t->r; + combine(t,t->l,t); + combine(t,t,t->r); + } + void split(pnode t,pnode &l,pnode &r,int pos,int add=0){ + if(!t)return void(l=r=NULL); + lazy(t); + int curr_pos = add + sz(t->l); + if(curr_pos<=pos)//element at pos goes to left subtree(l) + split(t->r,t->r,r,pos,curr_pos+1),l=t; + else + split(t->l,l,t->l,pos,add),r=t; + upd_sz(t); + operation(t); + } + void merge(pnode &t,pnode l,pnode r){ //l->leftarray,r->rightarray,t->resulting array + lazy(l);lazy(r); + if(!l || !r) t = l?l:r; + else if(l->prior>r->prior)merge(l->r,l->r,r),t=l; + else merge(r->l,l,r->l),t=r; + upd_sz(t); + operation(t); + } + pnode init(int val){ + pnode ret = (pnode)malloc(sizeof(node)); + ret->prior=rand();ret->siz=1; + ret->val=val; + ret->sum=val;ret->lazy=0; + return ret; + } + int range_query(pnode t,int l,int r){//[l,r] + pnode L,mid,R; + split(t,L,mid,l-1); + split(mid,t,R,r-l);//note: r-l!! + int ans = t->sum; + merge(mid,L,t); + merge(t,mid,R); + return ans; + } + void range_update(pnode t,int l,int r,ll val){//[l,r] + pnode L,mid,R; + split(t,L,mid,l-1); + split(mid,t,R,r-l);//note: r-l!! + t->lazy+=val; //lazy_update + merge(mid,L,t); + merge(t,mid,R); + } +}; +//End Treap + /* TO-DO LIST : 1. SQRT DECOMP (MO) 2. SQRT DECOMP (REAL) -3. TREE (LCA, HLD) +3. TREE (HLD, centroid?) 12. OTHER STRING STRUCTS SUCH AS PALINDROMIC TREE, MANACHAR, Z? 14. FFT 15. Karatsuba @@ -1545,7 +1636,6 @@ struct Tree 17. KMP 18. Trie 19. Suffix Tree (?) -20. Treap */ int main() //Testing Zone {