bzoj 1862 [Zjoi2006]GameZ游戏排名系统

来源:互联网 发布:真正是不是淘宝违禁词 编辑:程序博客网 时间:2024/05/05 17:16

1862: [Zjoi2006]GameZ游戏排名系统

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1134  Solved: 429
[Submit][Status][Discuss]

Description

GameZ为他们最新推出的游戏开通了一个网站。世界各地的玩家都可以将自己的游戏得分上传到网站上。这样就可以看到自己在世界上的排名。得分越高,排名就越靠前。当两个玩家的名次相同时,先上传记录者优先。由于新游戏的火爆,网站服务器已经难堪重负。为此GameZ雇用了你来帮他们重新开发一套新的核心。排名系统通常要应付三种请求:上传一条新的得分记录、查询某个玩家的当前排名以及返回某个区段内的排名记录。当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除。为了减轻服务器负担,在返回某个区段内的排名记录时,最多返回10条记录。

Input

第一行是一个整数n(n>=10)表示请求总数目。接下来n行每行包含了一个请求。请求的具体格式如下: +Name Score 上传最新得分记录。Name表示玩家名字,由大写英文字母组成,不超过10个字符。Score为最多8位的正整数。 ?Name 查询玩家排名。该玩家的得分记录必定已经在前面上传。 ?Index 返回自第Index名开始的最多10名玩家名字。Index必定合法,即不小于1,也不大于当前有记录的玩家总数。输入文件总大小不超过2M。 NOTE:用C++的fstream读大规模数据的效率较低

Output

对于每条查询请求,输出相应结果。对于?Name格式的请求,应输出一个整数表示该玩家当前的排名。对于?Index格式的请求,应在一行中依次输出从第Index名开始的最多10名玩家姓名,用一个空格分隔。

Sample Input

20
+ADAM 1000000 加入ADAM的得分记录
+BOB 1000000 加入BOB的得分记录
+TOM 2000000 加入TOM的得分记录
+CATHY 10000000 加入CATHY的得分记录
?TOM 输出TOM目前排名
?1 目前有记录的玩家总数为4,因此应输出第1名到第4名。
+DAM 100000 加入DAM的得分记录
+BOB 1200000 更新BOB的得分记录
+ADAM 900000 更新ADAM的得分记录(即使比原来的差)
+FRANK 12340000 加入FRANK的得分记录
+LEO 9000000 加入LEO的得分记录
+KAINE 9000000 加入KAINE的得分记录
+GRACE 8000000 加入GRACE的得分记录
+WALT 9000000 加入WALT的得分记录
+SANDY 8000000 加入SANDY的得分记录
+MICK 9000000 加入MICK的得分记录
+JACK 7320000 加入JACK的得分记录
?2 目前有记录的玩家总数为12,因此应输出第2名到第11名。
?5 输出第5名到第13名。
?KAINE 输出KAINE的排名

Sample Output

2
CATHY TOM ADAM BOB
CATHY LEO KAINE WALT MICK GRACE SANDY JACK TOM BOB
WALT MICK GRACE SANDY JACK TOM BOB ADAM DAM

HINT

Source

Splay


题解:平衡树splay+map映射+简单字符串处理

注意输出最后一个名字是不要加空格,否则在bzoj上会出格式错误

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<string>#define N 250003using namespace std;int m,n,ch[N][3],fa[N],size[N],key[N],l[N],root,sz,num;string s[N];const long long  inf=2147483647;map<string,int> mp;void clear(int x){  ch[x][1]=ch[x][0]=fa[x]=size[x]=key[x]=l[x]=0;}void update(int x){  size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}int get(int x){  return ch[fa[x]][1]==x;}void rotate(int x){  int y=fa[x]; int z=fa[y]; int which=get(x);  ch[y][which]=ch[x][which^1]; fa[ch[y][which]]=y;  ch[x][which^1]=y; fa[y]=x; fa[x]=z;  if (z) ch[z][ch[z][1]==y]=x;  update(y); update(x);}void splay(int x,int tar){ for (int f;(f=fa[x])!=tar;rotate(x))  if (fa[f]!=tar)  rotate(get(x)==get(f)?f:x); if(!tar) root=x;}void insert(int x){ if (!root)  {   sz++; root=sz; clear(sz); key[sz]=x; size[sz]=1; return;  } int now=root; int f; while(true) {  f=now;  now=ch[now][x<=key[now]];  if (!now)   {    sz++; key[sz]=x; size[sz]=1; fa[sz]=f;    ch[f][x<=key[f]]=sz; update(f); splay(sz,0); return ;   } }}int find(int x){  int now=root; int ans=0;  while(true)  {   if (x<=size[ch[now][0]])    now=ch[now][0];   else   {    ans=(ch[now][0]?size[ch[now][0]]:0)+1;    if (x==ans)  return now;    x-=ans; now=ch[now][1];   }  }}int pre(){ int now=ch[root][0]; while (ch[now][1]) now=ch[now][1]; return now;}int pre1(int x){ int now=ch[x][0]; while (ch[now][1]) now=ch[now][1]; return now;}void del(int x){  splay(x,0);  if (!ch[root][0]&&!ch[root][1])   {    clear(root); root=0; return;   }  if (!ch[root][1])   {   int old=root; root=ch[root][0]; fa[root]=0; clear(old); return;   }  if (!ch[root][0])   {   int old=root; root=ch[root][1]; fa[root]=0; clear(old); return;   }  int k=pre();   int old=root; splay(k,0);  ch[k][1]=ch[old][1]; fa[ch[k][1]]=k; clear(old);  update(k);}void dfs(int x,int k){  if (ch[x][0]) dfs(ch[x][0],k);  cout<<s[x]; if (x!=k)  printf(" ");  if (ch[x][1]) dfs(ch[x][1],k);}int main(){  scanf("%d\n",&n);  mp.clear();  insert(-inf); insert(inf);  for (int i=1;i<=n;i++)  {    char c=getchar();     if (c=='+'){    int k=0; string s1; char c1=getchar();    while(c1>='A'&&c1<='Z') s1=s1+c1,c1=getchar();    int x; scanf("%d\n",&x);    if (!mp[s1]){  insert(x);  s[root]=s1; mp[s1]=root; l[root]=s1.size();    }    else      {     int t=mp[s1]; del(t);  insert(x); s[root]=s1,mp[s1]=root,l[root]=s1.size();     }    }    else    {      int k=0; string s1; char c1=getchar();      while(c1>='A'&&c1<='Z') s1=s1+c1,c1=getchar();      int x=0;      while (c1>='0'&&c1<='9') x=x*10+c1-'0',c1=getchar();      if (x)  {      int l=find(x);   int r=find(size[root]>=x+11?x+11:size[root]);      splay(l,0); splay(r,root); int t=pre1(r);      dfs(ch[ch[root][1]][0],t);      printf("\n");      }      else      {       int t=mp[s1];        splay(t,0);       printf("%d\n",size[ch[root][0]]);      }    }  }}




0 0
原创粉丝点击