Skip to content

Commit 7ae6d58

Browse files
committed
.
1 parent 79deabf commit 7ae6d58

27 files changed

+1156
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
const int N=2005,inf=~0u>>1;
2+
struct RBtree{
3+
#define black 0
4+
#define red 1
5+
struct node{
6+
node *l,*r,*p;
7+
int key,key1,size; int col;
8+
void update(){
9+
size=l->size+r->size+1;
10+
}
11+
};
12+
node a[N];
13+
int len;node *root,*null;
14+
void clear(){
15+
len=0;root=null=newnode(0,0);
16+
null->l=null->r=null->p=null;
17+
null->size=0;null->col=black;
18+
}
19+
RBtree(){clear();}
20+
node *newnode(int key,int key1){
21+
node *x=a+(++len);
22+
x->l=x->r=x->p=null;
23+
x->size=1;x->col=red;
24+
x->key=key;x->key1=key1;return x;
25+
}
26+
void zig(node *x){
27+
node *y=x->l; x->l=y->r;
28+
if (y->r!=null)y->r->p=x;
29+
y->p=x->p;
30+
if (x->p==null)root=y;
31+
else if (x==x->p->l)x->p->l=y;
32+
else x->p->r=y;
33+
y->r=x; x->p=y;
34+
x->update();y->update();
35+
}
36+
void zag(node *x){
37+
node *y=x->r; x->r=y->l;
38+
if (y->l!=null)y->l->p=x;
39+
y->p=x->p;
40+
if (x->p==null)root=y;
41+
else if (x==x->p->l)x->p->l=y;
42+
else x->p->r=y;
43+
y->l=x; x->p=y;
44+
x->update();y->update();
45+
}
46+
void insert(int key,int key1){
47+
node *x=root,*y=null,*fa=root->p,*z=newnode(key,key1);
48+
if (root==null){root=z;root->col=black;return;}
49+
while (x!=null){
50+
++(y=x)->size;
51+
if (key<x->key)x=x->l;
52+
else x=x->r;
53+
}
54+
z->p=y;
55+
if (key<y->key)y->l=z;
56+
else y->r=z;
57+
x=z;
58+
while (1){
59+
y=x->p;
60+
if (y==fa){root=x;x->col=black;return;}
61+
if (y->col==black)return;
62+
z=y->p;
63+
if (x==y->r&&y==z->l){zag(y);zig(z);y->col=black;}
64+
else if (x==y->l&&y==z->r){zig(y);zag(z);y->col=black;}
65+
else if (x==y->l&&y==z->l){zig(z);x->col=black;x=y;}
66+
else if (x==y->r&&y==z->r){zag(z);x->col=black;x=y;}
67+
}
68+
}
69+
void replace(node *x,node *y){
70+
if (x->p==null)root=y;
71+
else if (x==x->p->l)x->p->l=y;
72+
else x->p->r=y;
73+
y->p=x->p;
74+
}
75+
void del(node *z){
76+
node *y=z,*x; bool c1=y->col;
77+
for (x=z;x!=null;x=x->p)--x->size;
78+
if (z->l==null){x=z->r; replace(z,z->r);}
79+
else if (z->r==null){x=z->l; replace(z,z->l);}
80+
else {
81+
y=fmin(z->r); c1=y->col; x=y->r;
82+
if (y->p==z)x->p=y;
83+
else {
84+
replace(y,y->r);
85+
for (node *w=y->p;w!=z;w=w->p)--w->size;
86+
y->r=z->r; y->r->p=y;
87+
}
88+
replace(z,y); y->l=z->l; y->l->p=y;
89+
y->col=z->col; y->update();
90+
}
91+
if (c1==black){
92+
while (x!=root&&x->col==black){
93+
if (x==x->p->l){
94+
node *w=x->p->r;
95+
if (w->col==red){
96+
w->col=black; x->p->col=red;
97+
zag(x->p); w=x->p->r;
98+
}
99+
if (w->l->col==black&&w->r->col==black){w->col=red; x=x->p;}
100+
else {
101+
if (w->r->col==black){
102+
w->l->col=black; w->col=red;
103+
zig(w); w=x->p->r;
104+
}
105+
w->col=x->p->col; x->p->col=black;
106+
w->r->col=black; zag(x->p); x=root;
107+
}
108+
}
109+
else {
110+
node *w=x->p->l;
111+
if (w->col==red){
112+
w->col=black; x->p->col=red;
113+
zig(x->p); w=x->p->l;
114+
}
115+
if (w->l->col==black&&w->r->col==black){w->col=red; x=x->p;}
116+
else {
117+
if (w->l->col==black){
118+
w->r->col=black; w->col=red;
119+
zag(w); w=x->p->l;
120+
}
121+
w->col=x->p->col; x->p->col=black;
122+
w->l->col=black; zig(x->p); x=root;
123+
}
124+
}
125+
}
126+
x->col=black;
127+
}
128+
}
129+
node *find(node *x,int key){
130+
if (x==null)return null;
131+
if (key==x->key)return x;
132+
if (key<x->key)return find(x->l,key);
133+
else return find(x->r,key);
134+
}
135+
node *findkth(int k){
136+
node *x=root;
137+
while (k){
138+
if (k==x->l->size+1)break;
139+
if (k<=x->l->size)x=x->l;
140+
else k-=x->l->size+1,x=x->r;
141+
}
142+
return x;
143+
}
144+
node *pred(node *x,int key){
145+
if (x==null)return null;
146+
if (x->key>=key)return pred(x->l,key);
147+
else {
148+
node *res=pred(x->r,key);
149+
return res==null?x:res;
150+
}
151+
}
152+
node *succ(node *x,int key){
153+
if (x==null)return null;
154+
if (x->key<=key)return succ(x->r,key);
155+
else {
156+
node *res=succ(x->l,key);
157+
return res==null?x:res;
158+
}
159+
}
160+
node *pred(node *x){
161+
if (x==null)return null;
162+
if (x->l!=null)return fmax(x->l);
163+
while (x==x->p->l)x=x->p;
164+
return x->p;
165+
}
166+
node *succ(node *x){
167+
if (x==null)return null;
168+
if (x->r!=null)return fmin(x->r);
169+
while (x==x->p->r)x=x->p;
170+
return x->p;
171+
}
172+
node *fmin(node *x){while (x->l!=null)x=x->l;return x;}
173+
node *fmax(node *x){while (x->r!=null)x=x->r;return x;}
174+
void print(node *x){
175+
if (x==null)return;
176+
print(x->l);
177+
printf("nie %d l=%d r=%d col=%d\n",x->key,x->l->key,x->r->key,x->col);
178+
print(x->r);
179+
}
180+
#undef red
181+
#undef black
182+
}; typedef RBtree::node node;
183+
RBtree a;
184+
class RangeModule {
185+
public:
186+
RangeModule() {
187+
a.clear();
188+
}
189+
void addRange(int l, int r) {
190+
node *x=a.succ(a.root,l-1),*y;
191+
while (x!=a.null&&r>=x->key){
192+
r=max(r,x->key1); y=a.succ(x);
193+
a.del(x); x=y;
194+
}
195+
y=x==a.null?a.fmax(a.root):a.pred(x);
196+
if (y!=a.null&&y->key1>=l)y->key1=max(y->key1,r);
197+
else a.insert(l,r);
198+
}
199+
bool queryRange(int l, int r) {
200+
node *x=a.pred(a.root,l+1);
201+
return x!=a.null&&x->key1>=r;
202+
}
203+
void removeRange(int l, int r) {
204+
node *x=a.succ(a.root,l-1),*y;
205+
while (x!=a.null&&r>x->key)
206+
if (x->key1<=r){
207+
y=a.succ(x);
208+
a.del(x); x=y;
209+
}
210+
else {
211+
x->key=r;
212+
break;
213+
}
214+
y=x==a.null?a.fmax(a.root):a.pred(x);
215+
if (y!=a.null){
216+
if (r<y->key1)a.insert(r,y->key1);
217+
if (y->key1>l)y->key1=l;
218+
}
219+
}
220+
};
221+
222+
/**
223+
* Your RangeModule object will be instantiated and called as such:
224+
* RangeModule* obj = new RangeModule();
225+
* obj->addRange(left,right);
226+
* bool param_2 = obj->queryRange(left,right);
227+
* obj->removeRange(left,right);
228+
*/
229+
230+
//IO
231+
int _IO=[](){
232+
ios::sync_with_stdio(0);
233+
cin.tie(0); //cout.tie(0);
234+
return 0;
235+
}();
236+
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const int N=2005,inf=~0u>>1;
2+
struct treap{
3+
struct node{
4+
node *l,*r;int key,key1,fix,size;
5+
void update(){
6+
size=l->size+r->size+1;
7+
}
8+
};
9+
inline static int _rand(){static unsigned int x=31253125;x+=(x<<4)+(x<<21)+1;return x&=0x7fffffff;}
10+
node a[N],*root,*null;int len;
11+
void clear(){
12+
len=0;root=null=new node; //srand(time(0));
13+
null->l=null->r=null;null->size=0;null->fix=inf;
14+
}
15+
treap(){clear();}
16+
node *newnode(int key,int key1){
17+
node *x=a+(++len);x->l=x->r=null;
18+
x->size=1;x->key=key;x->key1=key1;x->fix=_rand();
19+
return x;
20+
}
21+
void zig(node *&x){
22+
node *y=x->l;x->l=y->r;y->r=x;
23+
x->update();y->update();x=y;
24+
}
25+
void zag(node *&x){
26+
node *y=x->r;x->r=y->l;y->l=x;
27+
x->update();y->update();x=y;
28+
}
29+
void insert(node *&x,int key,int key1){
30+
if (x==null)x=newnode(key,key1);
31+
else if (key<=x->key){
32+
insert(x->l,key,key1);++x->size;
33+
if (x->l->fix<x->fix)zig(x);
34+
}
35+
else {
36+
insert(x->r,key,key1);++x->size;
37+
if (x->r->fix<x->fix)zag(x);
38+
}
39+
}
40+
node *find(node *x,int key){
41+
if (x==null)return null;
42+
if (key==x->key)return x;
43+
if (key<x->key)return find(x->l,key);
44+
else return find(x->r,key);
45+
}
46+
void del(node *&x,int key){
47+
if (x==null)return;
48+
if (x->key==key)
49+
if (x->l==null||x->r==null)
50+
if (x->l==null)x=x->r;
51+
else x=x->l;
52+
else if (x->l->fix<x->r->fix)
53+
zig(x),del(x->r,key),x->update();
54+
else zag(x),del(x->l,key),x->update();
55+
else if (key<x->key)del(x->l,key),x->update();
56+
else del(x->r,key),x->update();
57+
}
58+
node *findkth(node *root,int k){
59+
node *x=root;
60+
while (k){
61+
if (k==x->l->size+1)break;
62+
if (k<=x->l->size)x=x->l;
63+
else k-=x->l->size+1,x=x->r;
64+
}
65+
return x;
66+
}
67+
node *pred(node *x,int key){
68+
if (x==null)return null;
69+
if (x->key>=key)return pred(x->l,key);
70+
else {
71+
node *res=pred(x->r,key);
72+
return res==null?x:res;
73+
}
74+
}
75+
node *succ(node *x,int key){
76+
if (x==null)return null;
77+
if (x->key<=key)return succ(x->r,key);
78+
else {
79+
node *res=succ(x->l,key);
80+
return res==null?x:res;
81+
}
82+
}
83+
node *fmin(node *x){while (x->l!=null)x=x->l;return x;}
84+
node *fmax(node *x){while (x->r!=null)x=x->r;return x;}
85+
void print(node *x){
86+
if (x==null)return;
87+
print(x->l);
88+
printf("nie %d %d\n",x->key,x->key1);
89+
print(x->r);
90+
}
91+
}; typedef treap::node node;
92+
treap a;
93+
class RangeModule {
94+
public:
95+
RangeModule() {
96+
a.clear();
97+
}
98+
void addRange(int l, int r) {
99+
node *x=a.succ(a.root,l-1),*y;
100+
while (x!=a.null&&r>=x->key){
101+
r=max(r,x->key1); y=a.succ(a.root,x->key);
102+
a.del(a.root,x->key); x=y;
103+
}
104+
y=x==a.null?a.fmax(a.root):a.pred(a.root,x->key);
105+
if (y!=a.null&&y->key1>=l)y->key1=max(y->key1,r);
106+
else a.insert(a.root,l,r);
107+
}
108+
bool queryRange(int l, int r) {
109+
node *x=a.pred(a.root,l+1);
110+
return x!=a.null&&x->key1>=r;
111+
}
112+
void removeRange(int l, int r) {
113+
node *x=a.succ(a.root,l-1),*y;
114+
while (x!=a.null&&r>x->key)
115+
if (x->key1<=r){
116+
y=a.succ(a.root,x->key);
117+
a.del(a.root,x->key); x=y;
118+
}
119+
else {
120+
x->key=r;
121+
break;
122+
}
123+
y=x==a.null?a.fmax(a.root):a.pred(a.root,x->key);
124+
if (y!=a.null){
125+
if (r<y->key1)a.insert(a.root,r,y->key1);
126+
if (y->key1>l)y->key1=l;
127+
}
128+
}
129+
};
130+
131+
/**
132+
* Your RangeModule object will be instantiated and called as such:
133+
* RangeModule* obj = new RangeModule();
134+
* obj->addRange(left,right);
135+
* bool param_2 = obj->queryRange(left,right);
136+
* obj->removeRange(left,right);
137+
*/
138+
139+
//IO
140+
int _IO=[](){
141+
ios::sync_with_stdio(0);
142+
cin.tie(0); //cout.tie(0);
143+
return 0;
144+
}();
145+

0701-0800/715. Range Module.pdf

139 KB
Binary file not shown.

0 commit comments

Comments
 (0)