题目地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1056 & http://www.lydsy.com/JudgeOnline/problem.php?id=1862
题目大意:维护3种操作:上传得分(删除原得分),查询排名,查询某段区间的排名。
算法讨论:
Splay模板题。
需要注意的是“查询某段区间内的排名”操作。
如果暴力对区间内的每个值进行查询会TLE。
那么可以联想到Splay操作:可以先把需要查询的区间通过Splay至Root的右儿子的左子树位置,然后对Root的右儿子的左子树进行中序遍历就可以了。
Splay的题目总是细节比较多。
/*双倍经验哦亲~*/
Code:
/*
* Problem:1056 & 1862
* Author:PYC
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define rsz(x) (tree[(x)].r?tree[tree[(x)].r].sz:0)
#define lsz(x) (tree[(x)].l?tree[tree[(x)].l].sz:0)
#define p 10007
#define maxn 1000000
#define oo 2147483647
using namespace std;
int n,tot,tot2,root,num,next[maxn],son[maxn],to[maxn],ans[maxn];
char s[20],ed[maxn][20];
struct node{
int key,l,r,fa,id,sz;
}tree[maxn];
int gethash(char *s){
int l=strlen(s),ans=0;
for (int i=0;i<l;++i) ans=(ans*26+s[i]-'A')%p;
return ans;
}
void add(int x,char *y){
next[++tot]=son[x];
son[x]=tot;
memcpy(ed[tot],y,strlen(y));
to[tot]=++tot2;
}
void up(int rt){
tree[rt].sz=lsz(rt)+rsz(rt)+1;
}
int min_node(int rt){
if (!tree[rt].l) return rt;
return min_node(tree[rt].l);
}
void zig(int rt){
int t=tree[rt].l;
tree[rt].l=tree[t].r;
tree[t].r=rt;
tree[t].fa=tree[rt].fa;
tree[rt].fa=t;
tree[tree[rt].l].fa=rt;
if (rt==tree[tree[t].fa].l) tree[tree[t].fa].l=t;else tree[tree[t].fa].r=t;
up(rt);
up(t);
}
void zag(int rt){
int t=tree[rt].r;
tree[rt].r=tree[t].l;
tree[t].l=rt;
tree[t].fa=tree[rt].fa;
tree[rt].fa=t;
tree[tree[rt].r].fa=rt;
if (rt==tree[tree[t].fa].l) tree[tree[t].fa].l=t;else tree[tree[t].fa].r=t;
up(rt);
up(t);
}
void splay(int x,int &rt){
int fa=tree[rt].fa;
while (tree[x].fa!=fa){
int y=tree[x].fa,z=tree[y].fa;
if (z==fa) if (x==tree[y].l) zig(y);else zag(y);
else if (y==tree[z].l) if (x==tree[y].l) zig(z),zig(y);else zag(y),zig(z);
else if (x==tree[y].r) zag(z),zag(y);else zig(y),zag(z);
}
rt=x;
}
void insert(int &rt,int y,int id,int last){
if (!rt){
rt=to[id];
tree[rt].key=y;
tree[rt].l=tree[rt].r=0;
tree[rt].fa=last;
tree[rt].id=id;
tree[rt].sz=1;
splay(rt,root);
return;
}
if (y<=tree[rt].key){
insert(tree[rt].l,y,id,rt);
return;
}
if (y>tree[rt].key){
insert(tree[rt].r,y,id,rt);
return;
}
}
void del(int x){
splay(x,root);
if (!tree[root].r){
tree[tree[root].l].fa=0;
root=tree[root].l;
}
else{
splay(min_node(tree[root].r),tree[root].r);
tree[tree[root].r].l=tree[root].l;
tree[tree[root].l].fa=tree[root].r;
tree[tree[root].r].fa=0;
root=tree[root].r;
up(root);
}
}
int kth(int &rt,int y){
if (y==rsz(rt)+1) return rt;
if (y<rsz(rt)+1) return kth(tree[rt].r,y);
if (y>rsz(rt)+1) return kth(tree[rt].l,y-rsz(rt)-1);
}
int rank(int x){
splay(x,root);
return rsz(root)+1;
}
void prepare(){
tree[1].fa=0;
tree[1].id=0;
tree[1].key=-oo;
tree[1].l=0;
tree[1].r=2;
tree[1].sz=2;
tree[2].fa=1;
tree[2].id=0;
tree[2].key=oo;
tree[2].l=0;
tree[2].r=0;
tree[2].sz=1;
root=1;
tot2=2;
}
void travel(int rt){
if (!rt) return;
travel(tree[rt].r);
ans[++num]=tree[rt].id;
travel(tree[rt].l);
}
int main(){
scanf("%d",&n);
prepare();
for (int i=1;i<=n;++i){
char ch=getchar();
while (ch!='+' && ch!='?') ch=getchar();
if (ch=='+'){
int x;
scanf("%s%d",s,&x);
int H=gethash(s),ans=0;
for (int j=son[H];j;j=next[j])
if (!strcmp(ed[j],s)){
ans=j;
break;
}
if (!ans){
add(H,s);
insert(root,x,tot,0);
}
else{
del(to[ans]);
insert(root,x,ans,0);
}
}
else{
scanf("%s",s);
if (s[0]>='0' && s[0]<='9'){
int l=strlen(s),x=0;
for (int j=0;j<l;++j) x=x*10+s[j]-'0';
int y=min(x+9,tree[root].sz-2);
x++,y++;
x=kth(root,x-1);
y=kth(root,y+1);
swap(x,y);
splay(x,root);
splay(y,tree[root].r);
num=0;
travel(tree[tree[root].r].l);
for (int i=1;i<num;++i) printf("%s ",ed[ans[i]]);
printf("%s\n",ed[ans[num]]);
}
else{
int H=gethash(s);
for (int j=son[H];j;j=next[j])
if (!strcmp(ed[j],s)){
printf("%d\n",rank(to[j])-1);
break;
}
}
}
}
return 0;
}
By Charlie Pan
Mar 4,2014