#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. AngularJS中的控制器和作用域

    欢迎大家指导与讨论 : ) 一. 作用域的事件传播 一 . 1 修改的传播   关于作用域最重要的一点是修改会通过事件传播下去,自动更新所以依赖的数据值,即使是通过行为产生的.简而言之,就是即时您只修 ...

  2. Castle.ActiveRecord 多对多关系 引发的错误处理

    在Castle.ActiveRecord 实体类中,如果两个对象有 “多对多” 关系,一般的做法是将其分解为 两个“一对多”关系,但有时引发了 “您要删除 或 引用 的对象#2在数据库中不存在”的异常 ...

  3. Dell 服务器做Raid

    Dell 服务器做Raid DELL R720 服务器 RAID阵列卡配置介绍 (H310) 关于 RAID 5 与热备份(Hot Spare) 在不同RAID组间使用热备盘——Global Hot ...

  4. QT 记事本小程序

    //mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <Q ...

  5. Android的媒体管理框架:Glide

    Glide是一个高效.开源. Android设备上的媒体管理框架,它遵循BSD.MIT以及Apache 2.0协议发布.Glide具有获取.解码和展示视频剧照.图片.动画等功能,它还有灵活的API,这 ...

  6. iOS搜索附近的位置(类似微博朋友圈位置)

    说什么都是苍白的,直接上图~ 在某些情况下,我们需要获取用户周边的位置,来让用户选取.例如微信的朋友圈,在发一条朋友圈时可以选择地点,就是使用这样的功能. 基于以上的情况(其实也就是为了模仿微信),有 ...

  7. C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...

  8. How to create a batch of VMs with PowerShell

    Foreword When we do some test that need several VMs, we can use PowerShell script or CmdLets to impl ...

  9. C#操作Excel时的格式设定(转)

    Excel报表打印的格式设定 1.     表头的设置 Excel._Worksheet myWorksheet; myWorksheet.PageSetup.Orientation = Excel. ...

  10. Spring MVC 相关资料整理

    来源于:http://www.cnblogs.com/ylhssn/p/4062757.html 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即 ...