#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。

Hash_bzoj1862: [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. BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay

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

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

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

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

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

  5. [ZJOI2006]GameZ游戏排名系统

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

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

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

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

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

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

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

  9. [洛谷P2584][ZJOI2006]GameZ游戏排名系统

    题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...

随机推荐

  1. Delphi项目构成之项目文件DPR

    1 2 3 4 5 6 7 8 9 10 11 12 13 program Project1;                         {关键字program,标准的Pascal源文件格式} ...

  2. java.sql.preparedstatement和java.sql.statement的区别

    本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...

  3. 十大经典排序算法总结——JavaScrip版

    首先,对于评述算法优劣术语的说明: 稳定:如果a原本在b前面,而a=b,排序之后a仍然在b的前面:即排序后2个相等键值的顺序和排序之前它们的顺序相同 不稳定:如果a原本在b的前面,而a=b,排序之后a ...

  4. 苹果iPhone如何区分港版、国行、水货

    要想分辨所购买的苹果产品[iPhone 4.iPod Touch.iPad 2.iMac.MacBook及iPhone 4S]是大陆行货.水货.港货还是其它,其实很简单.今天来教大家如何区分.大陆行货 ...

  5. hadoop:将WordCount打包成独立运行的jar包

    hadoop示例中的WordCount程序,很多教程上都是推荐以下二种运行方式: 1.将生成的jar包,复制到hadoop集群中的节点,然后运行 $HADOOP_HOME/bin/hadoop xxx ...

  6. datahub

    https://help.aliyun.com/document_detail/27854.html

  7. hdu5444Elven Postman(主席树思想的应用)

    主席树这个概念应该不陌生吧!恩?不会, 戳这里. 主席树(函数式线段树)用的是函数思想,一个节点开数组用来保存自己的左右节点,这样节省许多不必要的空间,还可以保存许多历史状态.而这里我们用的是主席树的 ...

  8. WPF ListView和ListBox等双击事件问题

    上两篇文章中说双击行获取不到当前数据对象问题, http://www.cnblogs.com/ligl/p/5636899.html http://www.cnblogs.com/ligl/p/562 ...

  9. 《DOM启蒙》 随笔

    使用 Javascript 字符串创建并向 DOM 中添加元素与文本节点 innerHTML.outerHTML.textContent 及 insertAdjacentHTML() 属性和方法提供了 ...

  10. MVVM小记

    这篇小记源自于codeproject上的一篇文章 http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explai ...