#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 400005
#define p1 63
#define p2 103
#define mod1 1000007
#define mod2 2000007
int n,tot,len,need,fact,fa[maxn],son[maxn][],val[maxn],size[maxn];
char Name[maxn][],name[];
void read(int &x){
x=; int f=; char ch;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') f=-;
for (;isdigit(ch);ch=getchar()) x=x*+ch-''; x*=f;
}
struct S{
int root;
void prepare(){root=,memset(son,,sizeof(son));}
int which(int x){
return son[fa[x]][]==x;
}
void updata(int x){
size[x]=;
if (son[x][]) size[x]+=size[son[x][]];
if (son[x][]) size[x]+=size[son[x][]];
}
void rotata(int x){
int y=fa[x],d=which(x),dd=which(y);
if (fa[y]) son[fa[y]][dd]=x; fa[x]=fa[y];
fa[son[x][d^]]=y,son[y][d]=son[x][d^];
fa[y]=x,son[x][d^]=y,updata(y);
}
void splay(int x,int goal){
while (fa[x]!=goal){
if (fa[fa[x]]==goal) rotata(x);
else if (which(x)==which(fa[x])) rotata(fa[x]),rotata(x);
else rotata(x),rotata(x);
}
updata(x); if (goal==) root=x;
}
void insert(int x){
int y=root; bool bo;
if (root==){
root=x; updata(x);
return;
}
for (;;){
bo=;
if (val[x]<=val[y]){
if (!son[y][]) son[y][]=x,fa[x]=y,updata(x),updata(y),bo=,splay(x,);
else y=son[y][];
}else{
if (son[y][]==) son[y][]=x,fa[x]=y,updata(x),updata(y),bo=,splay(x,);
else y=son[y][];
}
if (bo==) break;
}
}
int prep(int x){
splay(x,);
int y=son[x][];
while (son[y][]) y=son[y][];
return y;
}
void Delete(int x){
int y=prep(x),z;
if (y==){
splay(x,); z=son[x][];
root=z,son[x][]=son[x][]=fa[x]=size[x]=fa[z]=;
}else{
splay(y,),splay(x,y); z=son[x][];
fa[z]=y,son[y][]=z,updata(y);
fa[x]=son[x][]=son[x][]=size[x]=;
}
}
int rank(int x){
splay(x,);
return size[son[x][]]+;
}
int kth(int x){
int y=root; bool bo;
for (;;){
if (size[son[y][]]+==x) return y;
else if (size[son[y][]]>=x) y=son[y][];
else x-=size[son[y][]]+,y=son[y][];
}
}
void print(int x){
if (son[x][]) print(son[x][]);
fact++;
for (int i=;i<=Name[x][];i++) printf("%c",Name[x][i]);
if (fact<need) printf(" ");
if (son[x][]) print(son[x][]);
}
void query(int u,int v){
int x=kth(u-),y=kth(v+),z; fact=;
splay(x,),splay(y,x); z=son[y][];
print(z); puts("");
}
}Splay;
struct hash{
int now[mod1+],prep[maxn],Ha[maxn][];
int ha1(){
int x=;
for (int i=;i<len;i++){
x=(x+(int)name[i])%mod1*p1%mod1;
}
return x;
}
int ha2(){
int x=;
for (int i=;i<len;i++){
x=(x+(int)name[i])%mod2*p2%mod2;
}
return x;
}
bool exist(){
int x1=ha1(),x2=ha2(); bool bo=;
for (int i=now[x1];i;i=prep[i]){
if (Ha[i][]==x2){
bo=; break;
}
}
return bo;
}
int number(){
int x1=ha1(),x2=ha2();
for (int i=now[x1];i;i=prep[i]){
if (Ha[i][]==x2) return Ha[i][];
}
}
void insert(){
int x1=ha1(),x2=ha2();
prep[++tot]=now[x1],now[x1]=tot;
Ha[tot][]=x2,Ha[tot][]=tot;
for (int i=;i<len;i++) Name[tot][]++,Name[tot][Name[tot][]]=name[i];
}
}Hash;
int main(){
char op[];
memset(size,,sizeof(size));
read(n),tot=; Splay.prepare();
val[n+]=-,val[n+]=;
Splay.insert(n+),Splay.insert(n+);
for (int w,u,i=;i<=n;i++){
scanf("%s",op+);
if (op[]=='+'){
len=strlen(op+); read(w);
for (int j=;j<=len;j++) name[j-]=op[j];
if (!Hash.exist()) Hash.insert(),u=Hash.number(),val[u]=w,Splay.insert(u);
else{
u=Hash.number();
Splay.Delete(u),val[u]=w,Splay.insert(u);
}
}else if (op[]=='?'&&!isdigit(op[])){
len=strlen(op+);
for (int j=;j<=len;j++) name[j-]=op[j];
w=Hash.number();
printf("%d\n",tot-Splay.rank(w)+);
}else{
len=strlen(op+); w=;
for (int j=;j<=len;j++) w=w*+op[j]-'';
w=tot-w+;
u=max(,w-+); need=w-u+;
Splay.query(u+,w+);
}
}
return ;
}
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1862
题意参照题面。
做法:Splay+Hash。
裸的splay,支持插入删除查询排名即可,为什么要用Hash呢,因为如果某玩家上传过记录就得把之前的记录清空,所以我们需要用字符串Hash来判重,字符串我们使用双hash值,一个用来确定地址,第一个来作为val,这样就能降低在哈希表中查找的复杂度,再记录该玩家在Splay树中的标号即可。
splay+Hash。