luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay
实在不想调了QAQ...
Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <map>
#define setIO(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 1000000
#define ll long long
using namespace std;
int ch[maxn][2],f[maxn],lazy[maxn],siz[maxn];
ll maxv[maxn];
int cnt,root;
char tt[maxn];
map<string,int>P;
string h[maxn];
int newnode(){ return ++cnt; }
struct Node
{
int id;
ll val;
}node[maxn];
int cmp(Node a,Node b){ return (a.val==b.val)?a.id<b.id:a.val>b.val; }
int lson(int x) { return ch[x][0]; }
int rson(int x) { return ch[x][1]; }
struct Splay_Tree{
int get(int x){ return ch[f[x]][1] == x; }
void pushup(int x){
siz[x]=siz[lson(x)] + siz[rson(x)] + 1;
//maxv[x]=max(maxv[lson(x)],maxv[rson(x)]);
//maxv[x]=max(maxv[x],node[x].val);
}
void rotate(int x){
int old=f[x],oldf=f[old],which=get(x);
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=oldf;
if(oldf) ch[oldf][ch[oldf][1]==old]=x;
pushup(old),pushup(x);
}
void splay(int x,int &tar)
{
int a = f[tar];
for(int fa; (fa = f[x]) != a; rotate(x))
if(f[fa] != a) rotate(get(x) == get(fa) ? fa : x);
tar = x;
}
int findx(int x,int rk){
if(rk<=siz[lson(x)]) return findx(lson(x),rk);
else if(rk-siz[lson(x)]-1 > 0) return findx(rson(x),rk-siz[lson(x)]-1);
else return x;
}
void deletex(int x){
splay(x,root);
int p=findx(lson(x),siz[lson(x)]);
splay(p,ch[x][0]);
ch[p][1]=ch[x][1]; //f[ch[x][1]]=ch[x][0];
f[ch[x][1]]=p;
f[p]=0;
pushup(root=ch[x][0]);
f[x]=siz[x]=0;
ch[x][0]=ch[x][1]=0;
}
void ins(int fa,int &x,int cur){
if(!x) {
x=cur;
pushup(cur);
f[cur]=fa;
return ;
}
if(!cmp(node[cur],node[x])) ins(x,ch[x][1],cur);
else ins(x,ch[x][0],cur);
pushup(x);
}
}tree;
void print(int x){
if(!x) return;
print(lson(x));
cout<<h[x]<<" ";
print(rson(x));
}
int main(){
//setIO("game");
int T;
cin>>T;
node[maxn-1].val=-12312312300;
node[maxn-1].id=123123123;
node[maxn-2].val=12312310231231;
node[maxn-2].id=0;
tree.ins(0,root,maxn-1);
tree.ins(0,root,maxn-2);
for(int i=1;i<=T;++i){
char opt;
string str,ss;
cin>>str;
if(str[0]=='+'){
int cur,num,len=str.size();
for(int j=1;j<len;++j) ss+=str[j];
cin>>num;
if(P[ss])
{
cur=P[ss];
tree.deletex(cur);
}
cur=P[ss]=newnode();
h[cur]=ss;
node[cur].id=i,node[cur].val=num;
tree.ins(0,root,cur);
}
if(str[0]=='?'){
if(str[1]>='A' && str[1]<='Z'){
int len=str.size();
for(int j=1;j<len;++j) ss+=str[j];
tree.splay(P[ss],root);
cout<<siz[lson(P[ss])]<<endl;
}
else {
int num=0,r,p;
int len=str.size();
for(int j=1;j<len;++j) num=num*10+str[j]-'0';
r=min(siz[root],num+11);
int a=tree.findx(root,num);
int b=tree.findx(root,r);
tree.splay(a,root);
tree.splay(b,ch[root][1]);
print(ch[ch[root][1]][0]);
cout<<endl;
}
}
}
return 0;
}