bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统
treap恶心题,不多说
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
cint mod=;int n,root,sz,tot,head[mod+];
struct data1
{
int v,t,next;
char ch[];
}hash[];
struct data2
{
int l,r,v,s,rr,t;
char ch[];
}tp[];
void maintain(int k){tp[k].s=tp[tp[k].l].s+tp[tp[k].r].s+;}
void rotate2(int &k)
{
int t=tp[k].l;
tp[k].l=tp[t].r;
tp[t].r=k;tp[t].s=tp[k].s;
maintain(k);k=t;
}
void rotate1(int &k)
{
int t=tp[k].r;
tp[k].r=tp[t].l;
tp[t].l=k;
tp[t].s=tp[k].s;
maintain(k);k=t;
}
bool com(char *a,char *b)
{
for(int i=;i<(int)max(strlen(a),strlen(b));i++)
if(a[i]!=b[i])return ;
return ;
}
int HASH(char *ch)
{
int s=;
for(int i=;i<(int)strlen(ch);i++)
{
s*=;s+=(ch[i]-'A'+);
s%=mod;
}
return s;
}
void del(int &k,int x,int t)
{
if(tp[k].v==x)
{
if(tp[k].t==t)
{
if(tp[k].l*tp[k].r==)k=tp[k].l+tp[k].r;
else if(tp[tp[k].l].rr<tp[tp[k].r].rr){rotate2(k);del(k,x,t);}
else {rotate1(k);del(k,x,t);}
}
else if(t>tp[k].t){tp[k].s--;del(tp[k].l,x,t);}
else {tp[k].s--;del(tp[k].r,x,t);}
}
else if(x<tp[k].v){tp[k].s--;del(tp[k].l,x,t);}
else {tp[k].s--;del(tp[k].r,x,t);}
}
void add(int &k,char *ch,int x,int t)
{
if(k==)
{
sz++;k=sz;tp[k].v=x;tp[k].s=;tp[k].rr=rand();
memcpy(tp[k].ch,ch,strlen(ch));tp[k].t=t;
return;
}
tp[k].s++;
if(x<=tp[k].v){add(tp[k].l,ch,x,t);if(tp[tp[k].l].rr<tp[k].rr)rotate2(k);}
else{add(tp[k].r,ch,x,t);if(tp[tp[k].r].rr<tp[k].rr)rotate1(k);}
}
void add(char *ch,int x,int t)
{
int k=HASH(ch);int i=head[k];
while(i)
{
if(com(hash[i].ch,ch))
{
del(root,hash[i].v,hash[i].t);
hash[i].t=t;hash[i].v=x;
add(root,ch,x,t);
return;
}
i=hash[i].next;
}
tot++;
hash[tot].t=t;hash[tot].v=x;
memcpy(hash[tot].ch,ch,strlen(ch));
hash[tot].next=head[k];head[k]=tot;
add(root,ch,x,t);
}
int hh(char *ch)
{
int k=HASH(ch);int i=head[k];
while(i)
{
if(com(hash[i].ch,ch))return i;
i=hash[i].next;
}return ;
}
int findrank(int k,int x,int t)
{
if(k==)return ;
if(tp[k].v==x)
{
if(tp[k].t>t)return findrank(tp[k].r,x,t);
else if(tp[k].t<t)return +tp[tp[k].r].s+findrank(tp[k].l,x,t);
else return tp[tp[k].r].s+;
}
else if(tp[k].v<x)return findrank(tp[k].r,x,t);
else return +tp[tp[k].r].s+findrank(tp[k].l,x,t);
}
void query1(char *ch)
{
int t=hh(ch);
printf("%d\n",findrank(root,hash[t].v,hash[t].t));
}
int findwei(int k,int x)
{
if(tp[tp[k].r].s+==x)return k;
else if(x<=tp[tp[k].r].s)return findwei(tp[k].r,x);
else return findwei(tp[k].l,x-tp[tp[k].r].s-);
}
void query2(char *ch)
{
int s=;
for(int i=;i<(int)strlen(ch);i++){s*=;s+=ch[i]-'';}
for(int i=s;i<=tot&&i<=s+;i++)
{
printf("%s",tp[findwei(root,i)].ch+);
if(i<tot&&i<s+)printf(" ");
}
printf("\n");
}
int CSC()
{
scanf("%d",&n);char ch[];int x;
for(int i=;i<=n;i++)
{
scanf("%s",ch);
if(ch[]=='+'){scanf("%d",&x);add(ch,x,i);}
else if(ch[]>='A'&&ch[]<='Z')query1(ch);
else query2(ch);
}
return ;
}
bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统的更多相关文章
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- 1056/1862. [ZJOI2006]GameZ游戏排名系统【平衡树-splay】
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 502[Submit][Statu ...
- bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统
hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...
- bzoj 1862: [Zjoi2006]GameZ游戏排名系统 & bzoj 1056: [HAOI2008]排名系统
傻叉了一晚上,把t打成x,然后这题神奇在于输出一段数,不足的不用输出,一开始我的是直接找没有后面就退,然后这样会格式错误囧……然后最后zj的还卡了下空间,于是不用string就过了……string毁一 ...
- BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay
BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...
- bzoj1862: [Zjoi2006]GameZ游戏排名系统
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- [ZJOI2006]GameZ游戏排名系统
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
随机推荐
- Flask 学习篇一: 搭建Python虚拟环境,安装flask,并设计RESTful API。
前些日子,老师给我看了这本书,于是便开始了Flask的学习 GitHub上的大神,于是我也在GitHub上建了一个Flask的项目. 有兴趣可以看看: https://github.com/Silen ...
- ubuntu16.04配置tensorflow-gpu环境
1.安装驱动 参考: 史上最全的ubuntu16.04安装nvidia驱动+cuda9.0+cuDnn7.0 https://blog.csdn.net/qq_31215157/article/det ...
- 【转】(翻译)从底层了解ASP.NET体系结构
原文地址:http://www.cnblogs.com/rijing2004/archive/2007/09/14/howaspnetwork.html 前言关于ASP.NET的底层的工作机制,最近园 ...
- HDU 2993 - MAX Average Problem - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Consider a simple sequence which only contains p ...
- Rotate Image(二位数组顺时针旋转)
问题描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- rem布局,flexible.js
//author:caibaojian //website:http://caibaojian.com //weibo:http:weibo.com/kujian //这段js的最后面有两个参数记得要 ...
- function $(id) {}表示什么函数
function $(id) {}表示什么函数 一.总结 1.就是简写,不然每次打document.getElementById很烦 二.问题 function $(id) {return docum ...
- MySQL中死锁
1 .死锁的概念 是指两个或两个以上的事务在执行过程中,因争夺资源而造成的一种互相等待的现象.若无外力作用,事务都将无法推进下去,解决死锁的最简单问题是不要有等待,任何的等待都转换为回滚,并且事务重新 ...
- 我读过的最好的epoll讲解(转)
原文:http://zhihu.com/question/20122137/answer/14049112 作者:蓝形参来源:知乎 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等 ...
- /etc/redhat-release 查看centos 版本
查看centos 版本 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final)