-
Notifications
You must be signed in to change notification settings - Fork 0
/
implicit-treap-by-tanuj-khattar.cpp
244 lines (226 loc) · 7.67 KB
/
implicit-treap-by-tanuj-khattar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
** Vishal Raj Roy
** Indian Institute of Technology Kharagpur
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define um(A,B) unordered_map <A, B>
#define us(A) unordered_set <A>
#define ms(A) multiset <A>
#define ifpresent(CON,VAL) (CON.find(VAL) != CON.end())
#define all(x) x.begin(),x.end()
#define ins insert
#define xx first
#define yy second
#define SZ(x) ((int)x.size())
#define count_ones __builtin_popcountl
#define nl cout<<endl;
#define name(X) cout<<endl<< (#X) << endl
#define watch(x) cout << (#x) << " is " << (x) << endl
#define sqr(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mem(x,val) memset((x),(val),sizeof(x))
#define rite(X) freopen(X,"w",stdout)
#define read(X) freopen(X,"r",stdin)
#define FOR(i,A,B) for( auto (i) = (A); (i) < (B); (i)++ )
#define FORN(i,A,B) for( auto (i) = (B); (i) >= (A); (i)-- )
#define ima INT_MAX
#define imi INT_MIN
#define lma LLONG_MAX
#define lmi LLONG_MIN
#define pv(X) name(X); FOR(i,0,sz(X)) { cout<<" "<< (#X) <<"["<<i<<"] = "<<X[i]<<endl; }
#define pvv(X) name(X); FOR(j,0,sz(X)) { cout<<endl; cout<<" "<< (#X) <<"["<<j<<"]"<<endl; FOR(i,0,sz(X[j])) { cout<<" "<< (#X) <<"["<<j<<"]["<<i<<"] = "<<X[j][i]<<endl; } }
#define pus(X) name(X); for( auto t : X ) cout<<" "<<t; cout<<endl;
#define pum(X) name(X); for( auto t : X ) cout<<" "<< (#X) <<"["<<t.first<<"] = "<<t.second<<endl;
#define TRACE
#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif
typedef double db;
typedef complex<db> cd;
typedef vector<cd> vcd;
typedef long long int li;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef unordered_map<int,int> umii;
typedef unordered_map<string,int> umsi;
typedef unordered_set<string> uss;
typedef unordered_set<int> usi;
typedef unordered_set<int, usi> usii;
typedef multiset<int> msi;
typedef multiset<string> mss;
typedef pair<li, li> ll;
typedef pair<li, ll> lll;
typedef vector<li> vl;
typedef vector<ll> vll;
typedef vector<lll> vlll;
typedef vector<vl> vvl;
typedef unordered_map<li,li> umll;
typedef unordered_map<string,li> umsl;
typedef unordered_set<string> uss;
typedef unordered_set<li> usl;
typedef unordered_set<li, usl> usll;
typedef multiset<li> msl;
typedef multiset<string> mss;
const double pi = 2 * acos(0.0);
const int oo = 0x3f3f3f3f; // don't use for long long
const double inf = 1.0/0.0;
typedef struct node{
int prior,size;
int val;//value stored in the array
int sum;//whatever info you want to maintain in segtree for each node
int lazy;//whatever lazy update you want to do
struct node *l,*r;
node() {}
node(int p, int v): prior(p), val(v), size(1), sum(v), lazy(0), l(NULL), r(NULL) {}
}node;
typedef node* pnode;
int sz(pnode t){
return t?t->size:0;
}
void upd_sz(pnode t){
if(t)t->size=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 printasarray( pnode & t ) {
if(!t) return;
printasarray(t->l);
cout<<t->val<<" ";
printasarray(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->size=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,int 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);
}
void heapify( pnode t ) {
if( !t ) return;
pnode maxx = t;
if( t->l && t->l->prior > maxx->prior ) maxx = t->l;
if( t->r && t->r->prior > maxx->prior ) maxx = t->r;
if( maxx != t ) {
swap(t->prior,maxx->prior);
heapify(maxx);
}
}
void buildonline( pnode & t, vi & v ) {
t = new node(rand(), v[0]);
FOR(i,1,SZ(v)) merge(t,t,new node(rand(),v[i]));
}
void buildoffline( pnode & t, vi & v, int l, int r ) {
if( r == l ) return;
int mid = (l+r)/2;
//cout<<l<<" "<<mid<<" "<<r; nl;
t = new node(rand(), v[mid]);
buildoffline(t->l, v, l, mid);
buildoffline(t->r, v, mid+1, r);
upd_sz(t); //upd_H(t);
heapify(t);
}
void pbt( pnode & root, int tab = 0, char desig = 'T' ) {
if( desig == 'T' ) name(root);
if(!root) return;
FOR(i,0,tab)cout<<" "; cout<<desig<<" : "; cout<<root->val; cout<<endl;
pbt(root->l, tab+2, 'L');
pbt(root->r, tab+2, 'R');
}
int main(int argc, char const *argv[])
{
int N = 10; vi v;
FOR(i,0,N) v.pb(2*i+1); pnode t;// = new node(rand(), 5);
// buildoffline(t,v,0,SZ(v)); has some problem
buildonline(t,v);
range_update(t,4,6,1);
range_update(t,5,7,1);
int s =range_query(t,4,6); watch(s);
// FOR(i,0,N)
// FOR(j,i+1,N) {
// buildonline(t,v);
// range_update(t,i,j,1);
// int s = range_query(t,i,j);
// if( s != (j-i+1)+sqr(j+1)-sqr(i) ) cout<<i<<" "<<j; nl;
// }
return 0;
}