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

Description

  排名系统通常要应付三种请求:上传一条新的得分记录、查询某个玩家的当前排名以及返回某个区段内的排名
记录。当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除。为了减轻服务器负担,在返回某个区
段内的排名记录时,最多返回10条记录。

Input

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

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
4

HINT

N<=250000


一开始写差了,按从小到大比较的。

输出时从后往前遍历,查询第K大的排名什么的都要改。

然后就变成了平衡树模板题。

名字用两个map存,然而发现char*不能直接做map的关键字,string不能printf输出。

只能这样:printf("%s",map[x].c_str())

然后各种一个字符一个字符那么读。

注意数据有问题,输入的数字最大有10位。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
inline char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
int x=0; char s=nc();
while(s<'0'||s>'9') s=nc();
while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 300050
int n,ch[N][2],f[N],siz[N],val[N],rt,cnt;
map<string,int>mp1;
map<int,string>mp2;
char name[20];
inline void pushup(int p) {
siz[p]=siz[ls]+siz[rs]+1;
}
void rotate(int x) {
int y=f[x],z=f[y],k=get(x);
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y); pushup(x);
if(rt==y) rt=x;
}
void splay(int x,int y) {
for(int d;(d=f[x])!=y;rotate(x)) if(f[d]!=y) rotate(get(x)==get(d)?d:x);
}
void insert(int v,int x) {
int l,r,p=rt;
while(p) {
if(val[p]>=v) r=p,p=ls;
else l=p,p=rs;
}
splay(l,0); splay(r,rt); ch[r][0]=x; f[x]=r; val[x]=v; siz[x]=1; pushup(r); pushup(l);
}
void del(int x) {
splay(x,0);
int l=ch[x][0],r=ch[x][1]; while(ch[l][1]) l=ch[l][1]; while(ch[r][0]) r=ch[r][0];
splay(l,0); splay(r,rt); ch[r][0]=0; f[x]=0; siz[x]=0; pushup(r); pushup(l);
}
int get_rank(int x) {
splay(x,0); return siz[ch[x][0]];
}
int find(int x) {
int p=rt;
while(1) {
if(siz[ls]>=x) p=ls;
else {
x-=siz[ls]+1; if(!x) return p; p=rs;
}
}
}
void print(int p) {
if(rs) print(rs);
printf("%s ",mp2[p].c_str());
if(ls) print(ls);
}
void output(int x,int y) {
x=cnt-x-1; y=cnt-y-1; swap(x,y);
x=find(x); y=find(y+2);
splay(x,0); splay(y,rt);
print(ch[y][0]);
}
int tot;
void BUG() {
int i; tot++;
printf("---------------test:%d----------------\n",tot);
char s[5]; s[0]='N'; s[1]='O'; s[2]='N'; s[3]='E';
for(i=1;i<=cnt;i++) {
int p=find(i);
printf("i=%d,p=%d,val[p]=%d,name=%s\n",i,p,val[p],p<3?s:mp2[p].c_str());
}
printf("---------------test:%d----------------\n",tot);
}
int main() {
n=rd();
char opt1;
val[1]=-1000000000; val[2]=2147483647;
ch[1][1]=2; f[2]=1; rt=1; siz[1]=2; siz[2]=1; cnt=2;
while(n--) {
int la=0,num;
memset(name,0,sizeof(name));
opt1=nc(); while(opt1!='+'&&opt1!='?') opt1=nc();
if(opt1=='+') {
opt1=nc(); while(opt1>='A'&&opt1<='Z') name[la++]=opt1,opt1=nc();
num=rd();
if(mp1.count(name)) {
int idx=mp1[name]; del(idx); insert(num,idx);
}else {
cnt++; insert(num,cnt); mp1[name]=cnt; mp2[cnt]=name;
}
}else {
opt1=nc();
if(opt1>='0'&&opt1<='9') {
num=0;
while(opt1>='0'&&opt1<='9') num=(num<<3)+(num<<1)+opt1-'0',opt1=nc();
// printf("%d %d\n",num,min(num+9,cnt-2));
// BUG();
output(num,min(num+9,cnt-2));
puts("");
}else {
while(opt1>='A'&&opt1<='Z') name[la++]=opt1,opt1=nc();
printf("%d\n",cnt-get_rank(mp1[name])-1);
}
}
}
}

BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

    hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...

  7. [HAOI2008]排名系统 & [Zjoi2006]GameZ游戏排名系统 BZOJ1862&BZOJ1056

    分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...

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

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

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

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

随机推荐

  1. awk输出指定列

    awk '{print $0} file' #打印所有列awk '{print $1}' file #打印第一列 awk '{print $1, $3}' file #打印第一和第三列 cat fil ...

  2. PHP面向对象设计五大原则(SOLID)梳理总结

    PHP设计原则梳理,参考<PHP核心技术与最佳实践>.<敏捷开发原则.模式与实践>,文章PHP面向对象设计的五大原则.设计模式原则SOLID 单一职责原则(Single Res ...

  3. 第一节:python提取PDF文档中的图片

    由于项目需要将PDF文档当中的图片转换成图片,所以参考了这篇文章https://blog.csdn.net/qq_15969343/article/details/81673302后项目得以解决. 1 ...

  4. 89-Relative Vigor Index 相对活力指数指标.(2015.7.4)

    Relative Vigor Index 相对活力指数指标 ~计算: RVI = (CLOSE-OPEN)/(HIGH-LOW) RVIsig=SMA(RVI,N) ~思想: 牛市中,收盘>开盘 ...

  5. ExecutorService 线程池 (转发)

    1.ExecutorService java.util.concurrent.ExecutorService 接口.用来设置线程池并执行多线程任务.它有以下几个方法. Future<?> ...

  6. Nginx学习总结(4)——负载均衡session会话保持方法

    负载均衡时,为了保证同一用户session会被分配到同一台服务器上,可以使用以下方法: 1.使用cookie 将用户的session存入cookie里,当用户分配到不同的服务器时,先判断服务器是否存在 ...

  7. ZOJ 3329 期望DP

    题目大意: 给定3个已经规定好k1,k2,k3面的3个色子,如果扔到a,b,c则重新开始从1 计数,否则不断叠加所有面的数字之和,直到超过n,输出丢的次数的数学期望 我们在此令dp[]数组记录从当前数 ...

  8. android开发里跳过的坑——android studio 错误Error:Execution failed for task ':processDebugManifest'. > Manifest merger failed with multiple errors, see logs

    使用AS在gradle里配置了多个定制版本,发现在编译版本切换时,会出现错误: Error:Execution failed for task ':processDebugManifest'.> ...

  9. linux程序分析工具

    ldd和nm是Linux下两个非常实用的程序分析工具.ldd是用来分析程序运行时需要依赖的动态链接库的工具,nm是用来查看指定程序中的符号表信息的工具,objdump用来查看源代码与汇编代码,-d只查 ...

  10. [bzoj2443][Usaco2011 Open]奇数度数_树形dp_生成树_并查集

    奇数度数 bzoj-2443 Usaco-2011 Open 题目大意:给定一个n个点m条便有向图,问是否有一种选出一些边的方式使得所有点的度数都是奇数. 注释:$1\le n \le 5\cdot ...