看起来问题是操作3
#include<bits/stdc++.h>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=2e5+5,Inf=1e9+7;
int n;
map<string,PII>mp;
int rt,tot;
struct SplayNode{
int ch[2],fa,val,tim,siz;
string name;
}tr[N];
int new_node(int tim,int val,string s){
tr[++tot]={0,0,0,val,tim,1,s};
return tot;
}
void pushup(int x){
if(!x) return;
tr[x].siz=1+tr[tr[x].ch[0]].siz+tr[tr[x].ch[1]].siz;
}
void pushdown(int x){
if(!x) return;
}
void maintain(int x){
while(x){
pushup(x);
x=tr[x].fa;
}
}
void rotate(int x){
int y=tr[x].fa,z=tr[y].fa;
pushdown(z),pushdown(y),pushdown(x);
int p=(tr[y].ch[1]==x);
tr[y].ch[p]=tr[x].ch[p^1],tr[tr[y].ch[p]].fa=y;
tr[x].ch[p^1]=y,tr[y].fa=x;
if(z) tr[z].ch[tr[z].ch[1]==y]=x;
tr[x].fa=z;
pushup(y),pushup(x),pushup(z);
}
void splay(int x,int k=0){
while(tr[x].fa!=k){
int y=tr[x].fa,z=tr[y].fa;
if(z!=k){
if((tr[y].ch[1]==x)^(tr[z].ch[1]==y)){
rotate(x);
}else{
rotate(y);
}
}
rotate(x);
}
if(!k) rt=x;
}
int get_pos(int tim,int val){
int x=rt;
while(x){
pushdown(x);
if(tr[x].val==val){
return x;
}
x=tr[x].ch[val>tr[x].val||tr[x].val==val&&tim>tr[x].tim];
}
return -1;
}
int insert(int tim,int val,string s){
if(!rt){
rt=new_node(tim,val,s);
return rt;
}
int x=rt;
while(x){
pushdown(x);
int p=(val>tr[x].val||val==tr[x].val&&tim<tr[x].tim);
if(tr[x].ch[p]){
x=tr[x].ch[p];
}else{
tr[x].ch[p]=new_node(tim,val,s);
tr[tr[x].ch[p]].fa=x;
x=tr[x].ch[p];
break;
}
}
maintain(x);
splay(x);
if(!tr[x].fa) rt=x;
return x;
}
int get_kth(int k,int type=0){
int x=rt;
if(k>tr[x].siz) return -1;
if(type){
k=tr[x].siz-k+1;
}
while(x){
pushdown(x);
if(k<=tr[tr[x].ch[0]].siz) x=tr[x].ch[0];
else if(k==tr[tr[x].ch[0]].siz+1) return x;
else k-=tr[tr[x].ch[0]].siz+1,x=tr[x].ch[1];
}
return -1;
}
int get_prev(int tim,int val){
int x=rt,last=0,ans=-1;
while(x){
pushdown(x);
last=x;
if(tr[x].val<val||tr[x].val==val&&tr[x].tim>tim){
ans=x;
x=tr[x].ch[1];
}else{
x=tr[x].ch[0];
}
}
if(last){
splay(last);
}
return ans;
}
int get_rank(int tim,int val){
int x=rt,last=0,res=0;
while(x){
pushdown(x);
last=x;
if(tr[x].val==val&&tr[x].tim==tim){
res+=tr[tr[x].ch[0]].siz+1;
splay(x);
return res;
}
if(tr[x].val>val||tr[x].val==val&&tr[x].tim<tim){
x=tr[x].ch[0];
}else{
res+=tr[tr[x].ch[0]].siz+1;
x=tr[x].ch[1];
}
}
if(last){
splay(last);
}
return res+1;
}
void del(int tim,int val){
int x=get_pos(tim,val);
splay(x);
tr[tr[x].ch[0]].fa=tr[tr[x].ch[1]].fa=0;
if(!tr[x].ch[0]||!tr[x].ch[1]){
if(!tr[x].ch[0]&&!tr[x].ch[1]){
rt=0;
}else{
rt=tr[x].ch[!tr[x].ch[0]];
}
return;
}
int y=tr[x].ch[0],z=tr[x].ch[1];
while(tr[y].ch[1]){
pushdown(y);
y=tr[y].ch[1];
}
tr[y].ch[1]=z,tr[z].fa=y;
maintain(y);
splay(y);
}
void fun(int u){
if(!u) return;
cout<<u<<":"<<tr[u].ch[0]<<","<<tr[u].ch[1]<<" "<<tr[u].fa<<" "<<tr[u].siz<<" "<<tr[u].val<<" "<<tr[u].tim<<" "<<tr[u].name<<"\n";
fun(tr[u].ch[0]);
fun(tr[u].ch[1]);
}
void dfs(int u){
if(!u) return;
dfs(tr[u].ch[1]);
cout<<tr[u].name<<" ";
dfs(tr[u].ch[0]);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
insert(0,-Inf,"");
insert(0,Inf,"");
for(int t=1;t<=n;t++){
char op;cin>>op;
if(op=='+'){
string name;int score;cin>>name>>score;
if(mp.count(name)){
auto t=mp[name];
int val=t.fi,tim=t.se;
del(tim,val);
}
insert(t,score,name);
mp[name]={score,t};
}else if(op=='?'){
string str;cin>>str;
if(str[0]>='0'&&str[0]<='9'){
int index=0;
for(int i=0;i<str.size();i++){
index*=10,index+=str[i]-'0';
}
int l=get_kth(tr[rt].siz-min(tr[rt].siz,index+11)+1),r=get_kth(tr[rt].siz-index+1);
// cout<<min(tr[rt].siz,index+11)<<" "<<index<<";";
// cout<<l<<" "<<r<<"\n";
splay(l),splay(r,l);
// fun(rt);
dfs(tr[r].ch[0]);
cout<<"\n";
}else{
auto t=mp[str];
int val=t.fi,tim=t.se;
cout<<tr[rt].siz-get_rank(tim,val)<<"\n";
}
}
}
return 0;
}
/*
20
+ADAM 1000000
+BOB 1000000
+TOM 2000000
+CATHY 10000000
?TOM
?1
+DAM 100000
+BOB 1200000
+ADAM 900000
+FRANK 12340000
+LEO 9000000
+KAINE 9000000
+GRACE 8000000
+WALT 9000000
+SANDY 8000000
+MICK 9000000
+JACK 7320000
?2
?10
?KAINE
*/
悬奖2关注