|
| 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 | + |
0 commit comments