傻叉了一晚上,把t打成x,然后这题神奇在于输出一段数,不足的不用输出,一开始我的是直接找没有后面就退,然后这样会格式错误囧……然后最后zj的还卡了下空间,于是不用string就过了……string毁一生……
const
maxn=;
mm=;
var
hash,size,left,right,fix,value,time,num,shi,cost:array[..maxn]of longint;
who:array[..maxn,..]of longint;
trie:array[..maxn,'A'..'Z']of longint;
n,tot,total,peo,u,i,j,k,ii,t:longint;
s:string;
ch:char; procedure lt(var t:longint);
var
k:longint;
begin
k:=right[t];
right[t]:=left[k];
left[k]:=t;
size[k]:=size[t];
size[t]:=size[left[t]]+size[right[t]]+;
t:=k;
end; procedure rt(var t:longint);
var
k:longint;
begin
k:=left[t];
left[t]:=right[k];
right[k]:=t;
size[k]:=size[t];
size[t]:=size[left[t]]+size[right[t]]+;
t:=k;
end; procedure insert(var t:longint;y,z,l:longint);
begin
if t= then begin
inc(total);
t:=total;
value[t]:=y;
fix[t]:=random(mm);
time[t]:=z;
hash[t]:=l;
left[t]:=;
right[t]:=;
size[t]:=;
exit;
end;
inc(size[t]);
if y<=value[t] then begin
insert(right[t],y,z,l);
if fix[right[t]]<fix[t] then lt(t);
end
else begin
insert(left[t],y,z,l);
if fix[left[t]]<fix[t] then rt(t);
end;
end; procedure delete(var t:longint;y,z:longint);
begin
if t= then exit;
dec(size[t]);
if (value[t]=y) and (time[t]=z) then begin
if left[t]= then t:=right[t]
else
if right[t]= then t:=left[t]
else
if fix[right[t]]<fix[left[t]] then begin
lt(t);
delete(left[t],y,z);
end
else begin
rt(t);
delete(right[t],y,z);
end;
exit;
end;
if (y>value[t]) or (y=value[t]) and (z<time[t])
then delete(left[t],y,z)
else delete(right[t],y,z);
end; function rank(t,y,z:longint):longint;
begin
if t= then exit();
if (y=value[t]) and (z=time[t]) then exit(size[left[t]]+);
if (y>value[t]) or (y=value[t]) and (z<time[t]) then exit(rank(left[t],y,z));
exit(size[left[t]]++rank(right[t],y,z));
end; procedure outs(x:longint);
var
i:longint;
begin
for i:= to who[x][] do
write(chr(who[x][i]));
end; procedure find(t,x:longint;var y:longint);
begin
if (t=) or (y=) then exit;
if x<=size[left[t]] then begin
find(left[t],x,y);
if y> then begin
dec(y);
if y= then begin
outs(hash[t]);
writeln;
exit;
end;
outs(hash[t]);
write(' ');
find(right[t],,y);
end;
end
else begin
if x=size[left[t]]+ then begin
dec(y);
if y= then begin
outs(hash[t]);
writeln;
exit;
end;
outs(hash[t]);
write(' ');
inc(x);
end;
find(right[t],x-size[left[t]]-,y);
end;
end; begin
readln(n);
randomize;
peo:=;
total:=;
tot:=;
for ii:= to n do begin
read(ch);
if ch='+' then begin
u:=;
s:='';
read(ch);
repeat
s:=s+ch;
if trie[u][ch]= then begin
inc(tot);
trie[u][ch]:=tot;
end;
u:=trie[u][ch];
read(ch);
until ch=' ';
readln(j);
if num[u]= then begin
inc(peo);
num[u]:=peo;
cost[peo]:=j;
shi[peo]:=ii;
who[peo][]:=length(s);
for i:= to who[peo][] do who[peo][i]:=ord(s[i]);
insert(t,j,ii,peo);
end
else begin
delete(t,cost[num[u]],shi[num[u]]);
cost[num[u]]:=j;
shi[num[u]]:=ii;
insert(t,j,ii,num[u]);
end;
end
else
if ch='?' then begin
readln(s);
if (ord(s[])>=) and (ord(s[])<=) then begin
u:=;
for i:= to length(s) do u:=trie[u][s[i]];
writeln(rank(t,cost[num[u]],shi[num[u]]));
end
else begin
val(s,i);
j:=;
if i+j>peo then j:=peo-i+;
find(t,i,j);
end;
end;
end;
end.