ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106 | #8. 【模板】普通平衡树 | wuenzi | 100 | 1817ms | 7208kb | C++23 | 1.9kb | 2025-04-16 22:13:11 | 2025-04-16 23:42:02 |
answer
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct WZOI{
int val,v,sz,l,r;
}a[1000005];
int len=0;
int rt=0;
int add(int v){
a[++len]={v,rand(),1,0,0};
return len;
}
void pushup(int u){
a[u].sz=a[a[u].l].sz+a[a[u].r].sz+1;
}
void split(int u,int val,int &l,int &r){
if(!u)return l=r=0,void();
if(a[u].val<=val){
l=u;
split(a[u].r,val,a[u].r,r);
}else{
r=u;
split(a[u].l,val,l,a[u].l);
}
pushup(u);
}
int merge(int l,int r){
if(l==0)return r;
if(r==0)return l;
if(a[l].v<a[r].v){
a[l].r=merge(a[l].r,r);
pushup(l);
return l;
}else{
a[r].l=merge(l,a[r].l);
pushup(r);
return r;
}
}
void insert(int u){
int l,r,t=add(u);
split(rt,u,l,r);
rt=merge(merge(l,t),r);
}
void erase(int x){
int l,r,mid;
split(rt,x,l,r);
split(l,x-1,l,mid);
mid=merge(a[mid].l,a[mid].r);
rt=merge(merge(l,mid),r);
}
int query(int k){
int l,r;
split(rt,k-1,l,r);
int ans=a[l].sz+1;
rt=merge(l,r);
return ans;
}
int kth(int k){
// cout<<a[2].sz<<endl;
int y=rt;
while(1){
// cout<<a[1].l<<endl;
if(a[a[y].l].sz>=k)y=a[y].l;
else if(a[a[y].l].sz+1<k){
k-=(a[a[y].l].sz+1);
y=a[y].r;
}else return a[y].val;
}
}
int front(int x){
int y=rt,ans;
while(y){
if(a[y].val>=x)y=a[y].l;
else{
ans=a[y].val;
y=a[y].r;
}
}
return ans;
}
int back(int x){
int y=rt,ans;
while(y){
if(a[y].val<=x)y=a[y].r;
else{
ans=a[y].val;
y=a[y].l;
}
}
return ans;
}
signed main() {
srand(time(0));
int Q;
cin>>Q;
while(Q--){
int op;
cin>>op;
if(op==1){int x;cin>>x;insert(x);}
else if(op==2){int x;cin>>x;erase(x);}
else if(op==3){int x;cin>>x;cout<<query(x)<<endl;}
else if(op==4){int x;cin>>x;cout<<kth(x)<<endl;}
else if(op==5){int x;cin>>x;cout<<front(x)<<endl;}
else if(op==6){int x;cin>>x;cout<<back(x)<<endl;}
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 11
Accepted
time: 3ms
memory: 3540kb
input:
20 1 964673 5 968705 4 1 3 964673 5 965257 1 915269 1 53283 3 964673 3 53283 3 53283 1 162641 5 9739...
output:
964673 964673 1 964673 3 1 1 964673 964673
result:
ok 9 numbers
Test #2:
score: 11
Accepted
time: 2ms
memory: 3468kb
input:
50 1 577793 1 408221 1 880861 2 408221 1 460353 1 223489 6 577713 4 2 5 889905 2 880861 1 100033 1 7...
output:
577793 460353 880861 577793 577793 100033 22575 22575 1 100033 643621 632329 643621 4 6 13 737721
result:
ok 17 numbers
Test #3:
score: 11
Accepted
time: 4ms
memory: 3612kb
input:
100 1 914241 1 639471 5 917713 6 912745 1 234811 1 297728 3 297728 1 6927 5 645391 1 664569 2 914241...
output:
914241 914241 2 639471 842266 6927 868271 297728 568845 639471 11 286545 234811 9 639471 531105 5311...
result:
ok 39 numbers
Test #4:
score: 11
Accepted
time: 4ms
memory: 3236kb
input:
1000 1 543050 1 599175 6 538214 1 208361 1 415495 1 376755 5 599771 6 415415 1 460416 3 599175 1 413...
output:
543050 599175 415495 6 599175 3 460416 430219 543050 8 838037 413857 543050 413857 14 17 60423 46041...
result:
ok 415 numbers
Test #5:
score: 11
Accepted
time: 20ms
memory: 3812kb
input:
10000 1 27217 5 30155 4 1 1 454393 6 453417 1 311109 1 172817 1 303817 1 446169 1 674881 4 1 6 44439...
output:
27217 27217 454393 27217 446169 303817 454393 674881 454393 454393 839081 839081 9 180795 674881 898...
result:
ok 3928 numbers
Test #6:
score: 11
Accepted
time: 258ms
memory: 4384kb
input:
50000 1 5227 6 -1792 6 445 1 175483 2 5227 1 562399 2 175483 1 956113 4 1 1 487125 1 851001 1 233057...
output:
5227 5227 562399 3 233057 3 40129 487125 3 40129 508946 7 478150 3 20 275201 487125 319065 487125 56...
result:
ok 19932 numbers
Test #7:
score: 11
Accepted
time: 529ms
memory: 7208kb
input:
100000 1 288297 1 535013 3 288297 1 457318 2 457318 3 535013 1 103045 1 964881 1 514921 1 640601 1 5...
output:
1 2 2 288297 103045 5 7 103045 524478 13 3 3 553201 4 349197 612362 199958 349197 535013 23 847953 5...
result:
ok 39610 numbers
Test #8:
score: 11
Accepted
time: 491ms
memory: 7152kb
input:
100000 1 634561 6 630913 1 254516 6 630481 1 966881 1 18529 1 873593 3 254516 2 966881 3 254516 4 1 ...
output:
634561 634561 2 2 18529 18529 634561 634561 1 74233 5 695277 74233 14 9 800499 74233 800499 6 14 873...
result:
ok 39797 numbers
Test #9:
score: 11
Accepted
time: 506ms
memory: 7192kb
input:
100000 1 104152 1 405622 4 2 1 564237 1 290523 1 441856 1 910218 1 97083 1 104393 1 485841 3 910218 ...
output:
405622 9 290523 97083 7 104393 438977 12 485841 1 86501 86501 735257 405622 290523 874917 438977 851...
result:
ok 39516 numbers