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

来源:互联网 发布:sql数据修复工具 编辑:程序博客网 时间:2024/05/18 22:11


Splay的基本操作,比较繁琐.....

有个一坑点,sorce会超int

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 913  Solved: 343
[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

[Submit][Status][Discuss]


/* ***********************************************Author        :CKbossCreated Time  :2015年05月18日 星期一 21时58分40秒File Name     :BZOJ1862.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;typedef long long int LL;const int maxn=620020;const LL INF=1LL<<50;#define Key_Value ch[ch[root][1]][0]int ch[maxn][2],sz[maxn],pre[maxn];LL key[maxn];int root,tot1;int s[maxn],tot2;/*********************************************/map<string,int> msi;map<int,string> mis;int tot;int getID(string name){if(msi[name]==0) msi[name]=++tot;return msi[name];}/*********************************************/void NewNode(int& x,int fa,LL k){if(tot2) x=s[tot2--];else x=++tot1;ch[x][0]=ch[x][1]=0;sz[x]=1; pre[x]=fa; key[x]=k;}void Push_Up(int x){sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+1;}void init(){root=tot1=tot2=0;ch[root][0]=ch[root][1]=pre[root]=sz[root]=0;key[root]=INF;NewNode(root,0,INF);NewNode(ch[root][1],root,-INF);Push_Up(ch[root][1]);Push_Up(root);}void Rotate(int x,int kind){int y=pre[x];ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;Push_Up(y);}void Splay(int r,int goal){while(pre[r]!=goal){if(pre[pre[r]]==goal) {Rotate(r,ch[pre[r]][0]==r);}else{int y=pre[r];int kind=(ch[pre[y]][0]==y);if(ch[y][kind]==r) Rotate(r,!kind);else Rotate(y,kind);Rotate(r,kind);}}Push_Up(r);if(goal==0) root=r;}int INSERT(LL p) /// 将数字p插入到splay中{int now=root;int up=now;int dir=0;while(now){up=now;if(key[now]<p) { dir=0; now=ch[now][0]; }else { dir=1; now=ch[now][1]; }}int nid;NewNode(nid,up,p);ch[up][dir]=nid;Splay(nid,root);Push_Up(root);return nid;}int getRank(int p){Splay(p,0);return sz[ch[root][0]];}int Get_Kth(int r,int k){int t=sz[ch[r][0]]+1;if(k==t) return r;if(t<k) return Get_Kth(ch[r][1],k-t);else return Get_Kth(ch[r][0],k);}int Get_Min(int r){while(ch[r][0]) r=ch[r][0];return r;}void Remove_Root(){s[++tot2]=root;if(ch[root][1]==0||ch[root][0]==0){root=ch[root][0]+ch[root][1];pre[root]=0;return ;}int k=Get_Min(ch[root][1]);Splay(k,root);ch[ch[root][1]][0]=ch[root][0];root=ch[root][1];pre[ch[root][0]]=root;pre[root]=0;Push_Up(root);}void Debug();void ERASE(int r){Splay(r,0);Remove_Root();}vector<string> names;void dfs(int u){if(ch[u][0]) dfs(ch[u][0]);names.push_back(mis[u]);if(ch[u][1]) dfs(ch[u][1]);}void getRange(int rk){int msz=sz[root];int left=Get_Kth(root,rk);int right=Get_Kth(root,min(msz,rk+12));Splay(left,0); Splay(right,root);names.clear();dfs(Key_Value);int sss=min((int)names.size(),10);for(int i=0;i<sss;i++)printf("%s%c",names[i].c_str(),(i==sss-1)?'\n':' ');}/**********debug*************/void showit(int x){if(x){showit(ch[x][0]);printf("node: %2d name %10s left: %2d right: %2d fa: %2d size: %2d key: %2lld\n",x,mis[x].c_str(),ch[x][0],ch[x][1],pre[x],sz[x],key[x]);showit(ch[x][1]);}}void Debug(){cout<<"-------------------\n";cout<<"root: "<<root<<endl;showit(root);}int T_T;char op[5],name[20];int main(){scanf("%d",&T_T);init();while(T_T--){scanf("%1s%s",op,name);if(op[0]=='+'){LL score;scanf("%lld",&score);if(msi[name]) ERASE(msi[name]);int nid=INSERT(score);msi[name]=nid;mis[nid]=name;}else if(op[0]=='?'){if(name[0]>='A'&&name[0]<='Z'){int p=msi[name];printf("%d\n",getRank(p));}else{int id;sscanf(name,"%d",&id);getRange(id);}}}    return 0;}


1 0
原创粉丝点击