luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define N 300002
#define mod 1000000009
#define ll long long
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define setIO(s) freopen(s".in", "r", stdin)
using namespace std;
map<int, int> id;
char S[N][12];
int sta[N], pos[N], tot , root, cc = 0, splay_cnt;
struct Node
{
int ch[2] , siz, f;
ll val;
}t[N];
inline int decode(char str[])
{
int k = strlen(str + 1), i , j ;
ll h = 1;
for(i = 1; i <= k ; ++i) h = (h * 26 + str[i] - 'A') % mod;
return (int) h ;
}
inline int get(char str[])
{
int k = strlen(str + 1), i , j = 0;
for(i = 1; i <= k ; ++i) j = j * 10 + str[i] - '0';
return j ;
}
inline void pushup(int x)
{
t[x].siz = t[lson].siz + t[rson].siz + 1;
}
inline int get(int x)
{
return t[t[x].f].ch[1] == x;
}
inline void rotate(int x)
{
int old = t[x].f, fold = t[old].f , which = get(x);
t[old].ch[which] = t[x].ch[which ^ 1], t[t[old].ch[which]].f = old;
t[x].ch[which ^ 1] = old, t[old].f = x, t[x].f = fold;
if(fold) t[fold].ch[t[fold].ch[1] == old] = x;
pushup(old), pushup(x);
}
inline void splay(int x, int &tar)
{
int u = t[tar].f;
for(int fa ; (fa = t[x].f) ^ u ; rotate(x))
if(t[fa].f ^ u)
rotate(get(fa) == get(x) ? fa : x);
tar = x;
}
inline void insert(int &x, int ff, ll v)
{
if(!x)
{
x = ++ tot;
t[x].f = ff, t[x].val = v;
pushup(x);
return ;
}
insert(t[x].ch[v <= t[x].val], x , v), pushup(x);
}
inline int get_kth(int k)
{
int x = root;
while(1)
{
if(k > t[lson].siz)
{
k -= (t[lson].siz + 1);
if(!k) return x ;
else x = rson;
}
else x = lson;
}
}
inline void print(int i)
{
int L = strlen(S[i] + 1), j ;
for(j = 1; j <= L; ++j) printf("%c", S[i][j]);
}
inline void dfs(int u)
{
if(!u) return ;
if(t[u].ch[0]) dfs(t[u].ch[0]);
print(pos[u]), printf(" ");
if(t[u].ch[1]) dfs(t[u].ch[1]);
}
inline void del(int x)
{
splay(x, root);
if(!lson) root = rson, t[root].f = 0;
else if(!rson) root = lson, t[root].f = 0;
else
{
int p = lson;
while(t[p].ch[1]) p = t[p].ch[1];
splay(p, t[root].ch[0]), t[p].ch[1] = t[root].ch[1], t[p].f = 0, t[t[root].ch[1]].f = p, pushup(p), root = p;
}
t[x].ch[0] = t[x].ch[1] = 0;
}
int main()
{
// setIO("input");
int T, i , j ;
scanf("%d", &T);
for(int cas = 1; cas <= T; ++cas)
{
char str[20];
scanf("%s", str);
if(str[0] == '+')
{
j = strlen(str + 1);
for(i = 1; i <= j ; ++i) S[cas][i] = str[i];
int h = decode(str);
ll v;
scanf("%lld", &v), ++cc, ++splay_cnt;
if(id[h]) del(id[h]), --cc;
insert(root , 0 , v), id[h] = tot, pos[tot] = cas;
if(splay_cnt % 6 == 0) splay(tot, root);
}
else
{
if(str[1] >= '0' && str[1] <= '9') // (好头疼qaq)
{
int kth = get(str);
int p = get_kth(kth);
int now, c;
splay(p, root), print(pos[p]), printf(" ");
if(kth + 10 - 1 < cc) c = get_kth(kth + 10), splay(c, t[root].ch[1]), now = t[t[root].ch[1]].ch[0];
else now = t[root].ch[1];
dfs(now), printf("\n");
}
else
{
int h = decode(str);
splay(id[h], root), printf("%d\n", t[t[root].ch[0]].siz + 1);
}
}
}
return 0;
}
luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay的更多相关文章
- [洛谷P2584][ZJOI2006]GameZ游戏排名系统
题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...
- BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay
BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...
- 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为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- bzoj1862: [Zjoi2006]GameZ游戏排名系统
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- [ZJOI2006]GameZ游戏排名系统
Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...
- [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2487 Solved: 711[Submit][Statu ...
- 【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 ...
随机推荐
- eclipse中的maven项目部署到tomcat中
http://www.cnblogs.com/guodefu909/p/4874549.html
- 网站出现502 bad getway
最近项目之余,领导叫解决下系统网站经常出现502的问题,作为小头头的我,怎能不顶上. 流程开始走起,先查nginx,嗯,配置是大众的.是不是缓存溢出了呢.调节buffer的值 .貌似也没什么影响啊.5 ...
- Nginx部署静态资源(及root与alias区别)
root目录与alias目录的区别Nginx路径location配置中,使用root目录与alias目录的区别 1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是 ...
- django创建第一个子文件夹
1.首先安装python, 我安装了Python2.7的版本,安装在了D:\Python25\2.接着安装django,我下载了django 1.6的版本.3. 安装完了之后,打开cmd,然后可以检验 ...
- keepalived+mysql主主实现高可用
mysql主从配置参考我的上篇博客 主主配置就是互为主从 环境 准备应用 keepalived-2.0.2.tar.gz openssl-devel-1.0.2k-12.el7.x86_64.rpm ...
- Bonjour/Zeroconf with Arduino
转自:http://gkaindl.com/software/arduino-ethernet/bonjour Bonjour/Zeroconf with Arduino DownloadVersio ...
- 【JavaScript框架封装】实现一个类似于JQuery的缓存框架的封装
// 缓存框架 (function (xframe) { /** * 实现了缓存框架的临时存储功能(内存存储) * @type {{data: Array, get: (function(*): *) ...
- 2、链接数据库+mongodb基础命令行+小demo
链接数据库并且打印出数据的流程:1.在CMD里面输入 mongod 2.在CMD里面输入 mongo 3.在输入mongodb命令行里面进行操作,首先输入 show dbs 来查看是否能够链接得上库4 ...
- CodeForcesGym 100676G Training Camp
G. Training Camp Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 产品经理---- idea good idea
想问题有不同的思想. 1.先去娶老婆再生娃. 我有啥,我能用这些做啥? [大概这就是泯然众人的思想吧] 2.先做衣服后长肉 我想要做个啥?然后我需要怎么做?怎么做的什么? 好的产品经理我认为是第二种. ...