hash 加上 平衡树(名次树)。

这道题麻烦的地方就在于输入的是一个名字,所以需要hash。

这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些。

mod一定要取得大一些。 hash时要减去@,否则A和B的hash值会相同导致tle。

比较难写?//蒟蒻本性。。。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000000 + 10;
const int mod = 999983;
const int INF = 2147483647; int n,sp,p;
char s[20],name[maxn][20],op;
int res[20];
int h[maxn];
int cnt; struct Splay {
int l[maxn],r[maxn],f[maxn];
int a[maxn],s[maxn];
int root; void update(int x) {
s[x]=s[l[x]]+s[r[x]]+1;
} void lr(int x) {
int y=f[x];
r[y]=l[x];
if(l[x]) f[l[x]]=y;
f[x]=f[y];
if(root==y) root=x;
else if(l[f[y]]==y) l[f[y]]=x;
else r[f[y]]=x;
f[y]=x; l[x]=y;
update(y);
update(x);
} void rr(int x) {
int y=f[x];
l[y]=r[x];
if(r[x]) f[r[x]]=y;
f[x]=f[y];
if(root==y) root=x;
else if(l[f[y]]==y) l[f[y]]=x;
else r[f[y]]=x;
f[y]=x; r[x]=y;
update(y);
update(x);
} void rotate(int x) {
if(l[f[x]]==x) rr(x);
else lr(x);
} void splay(int x,int target=0) {
while(f[x] != target) {
if(f[f[x]]==target) rotate(x);
else if((l[f[x]]==x) == (l[f[f[x]]]==f[x])) {rotate(f[x]); rotate(x);}
else {rotate(x); rotate(x);}
}
} void insert(int pos,int val) {
int x=root,y=0;
while(x) {
y=x;
x=(val<=a[x]?l[x]:r[x]);
}
if(val<=a[y]) l[y]=pos; else r[y]=pos;
f[pos]=y; l[pos]=r[pos]=0;
a[pos]=val; s[pos]=1;
splay(pos);
} void erase(int x) {
splay(x);
if(!r[root]) {
f[l[root]]=0;
root=l[root];
}
else {
int y=r[x];
while(l[y]) y=l[y];
splay(y,root);
l[y]=l[root]; f[l[root]]=y; f[y]=0;
root=r[root];
update(root);
}
} int find(int k) {
int x=root;
while(s[r[x]]+1!=k) {
if(k<s[r[x]]+1) x=r[x];
else {k-=s[r[x]]+1; x=l[x];}
}
return x;
} int rank(int x) {
splay(x);
return s[r[x]];
} void access(int x) {
if(!x) return;
access(r[x]);
res[++sp]=x;
access(l[x]);
} void init() {
r[1]=2; f[2]=1; s[1]=2; s[2]=1; root=1; cnt=2;
a[1]=-INF; a[2]=INF;
}
}splay; int hash(char* s) {
int l=strlen(s),res=0;
for(int i=0;i<l;i++)
res=(res*27+(s[i]-'@'))%mod;
return res;
} int main() {
scanf("%d\n",&n);
splay.init();
memset(h,0,sizeof(h));
while(n--) {
while(op=getchar(),op!='?'&&op!='+');
if(op=='+') {
scanf("%s%d",s,&p);
int t=hash(s),pos=0;
while(1) {
if(!h[t]) {
pos=t; break;
}
else if(!strcmp(name[h[t]],s)) {pos=t; break;}
else {t++; if(t==(mod+1)) t=1;}
}
if(!h[pos]) {
h[pos]=++cnt;
memcpy(name[cnt],s,strlen(s));
splay.insert(cnt,p);
}
else {
splay.erase(h[pos]);
splay.insert(h[pos],p);
}
}
else {
scanf("%s",s);
if(s[0]>='0' && s[0] <='9') {
sscanf(s,"%d",&p);
int x=p+1,y=min(p+10,splay.s[splay.root]-1);
x=splay.find(x-1);
y=splay.find(y+1);
swap(x,y);
splay.splay(x); splay.splay(y,splay.root);
sp=0;
splay.access(splay.l[splay.r[splay.root]]);
for(int j=1;j<sp;j++) printf("%s ",name[res[j]]);
printf("%s\n",name[res[sp]]);
}
else {
int t=hash(s);
while(1) {
if(!strcmp(s,name[h[t]])) {
printf("%d\n",splay.rank(h[t]));
break;
}
else {t++; if(t==(mod+1)) t=1;}
}
}
}
}
return 0;
}

bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统的更多相关文章

  1. BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]

    1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1318  Solved: 498[Submit][ ...

  2. 1056/1862. [ZJOI2006]GameZ游戏排名系统【平衡树-splay】

    Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...

  3. bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 502[Submit][Statu ...

  4. 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  5. bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统

    题目链接:1,2 treap恶心题,不多说 #include<algorithm> #include<iostream> #include<cstdlib> #in ...

  6. bzoj 1862: [Zjoi2006]GameZ游戏排名系统 & bzoj 1056: [HAOI2008]排名系统

    傻叉了一晚上,把t打成x,然后这题神奇在于输出一段数,不足的不用输出,一开始我的是直接找没有后面就退,然后这样会格式错误囧……然后最后zj的还卡了下空间,于是不用string就过了……string毁一 ...

  7. 【pb_ds】bzoj1056 [HAOI2008]排名系统/bzoj1862 [Zjoi2006]GameZ游戏排名系统

    STL裸题,线下AC,bzoj无限RE ing…… #include<cstdio> #include<cctype> #include<iostream> #in ...

  8. BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay

    BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...

  9. bzoj1862: [Zjoi2006]GameZ游戏排名系统

    Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...

随机推荐

  1. WordPress非插件添加文章浏览次数统计功能

    一: 转载:http://www.jiangyangangblog.com/26.html 首先在寻找到functions.php.php文件夹,在最后面  ?> 的前面加入下面的代码 func ...

  2. (转载)MS SQL Server 未公开的加密函数有哪些?

    MS SQL Server 未公开的加密函数有哪些? 以下的文章是对MS SQL Server 未公开的加密函数的具体操作,如果你对其相关的实际操作有兴趣的话,你就可以点击了. MS SQL Serv ...

  3. Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming

    Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming 在Spark2.x中,Spark Streaming获得了比较全面的升级,称为St ...

  4. 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数

    解决办法:添加 using System.Collections:命名空间

  5. web sevice 生成代理类及使用

    一.生成代理类: VS2008下这样写 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebService.asmx VS2010下 ...

  6. 【BZOJ 1563】 [NOI2009]诗人小G

    Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...

  7. ParentChildTest.java

    public class ParentChildTest { public static void main(String[] args) { Parent parent=new Parent(); ...

  8. EL表达式中如何截取字符串

    EL表达式中如何截取字符串 可以截取,用fn函数:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/ ...

  9. DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义

    DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义 今天看到一篇CSS应用的一个友好搜索,我按网页上的代码复制.粘贴后预览时总达不到效果,而直接拷贝他的实例却能达到效果, ...

  10. 查网卡信息(千M o 万M)