Skip to content

Commit

Permalink
Added Treap
Browse files Browse the repository at this point in the history
  • Loading branch information
zscoder authored Oct 7, 2016
1 parent 7784bb1 commit cf76ecc
Showing 1 changed file with 96 additions and 6 deletions.
102 changes: 96 additions & 6 deletions Data Structures Class Template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,13 +1035,12 @@ struct ConvexHull
return m * x + c;
}
};
#define sz(x) (int)(x).size()
deque<Line> 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);
}
Expand All @@ -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);
}
};
Expand Down Expand Up @@ -1534,18 +1533,109 @@ 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
16. Other Flow Algo
17. KMP
18. Trie
19. Suffix Tree (?)
20. Treap
*/
int main() //Testing Zone
{
Expand Down

0 comments on commit cf76ecc

Please sign in to comment.