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 ...
随机推荐
- BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...
- 常用css和js内容
1.让一个200x200的div在不同分辨率屏幕上下左右居中. <div class="box"></div> <style type="t ...
- Linux 程序包管理-YUM
前端工具YUM管理程序包: rpm管理软件虽然方便,但是需要手工解决软件包的依赖关系:很多时候安装一个软件需要首先安装一个或多个(有时多达上百个)其它软件,手工解决很复杂:使用yum可以解决这个问题 ...
- 《使用CSLA 2019:CSLA .NET概述》原版和机译文档下载
自己从作者官方网站上(http://www.cslanet.com/)下载的免费版.PDF文档,又使用有道付款翻译的,供大家下载学习,文档中是对CSLA.NET4.9版本的介绍. 下载链接:http: ...
- Python - 采用 contextmanage 简化代码
contextlib.contextmanage Python 2.7 documents: https://docs.python.org/2.7/library/contextlib.html?h ...
- String String s = new String("asd") 涉及对象数目
问题·:.String str = new String("abc")创建了多少个对象? 这个问题在很多书籍上都有说到比如<Java程序员面试宝典>,包括很多国内大公司 ...
- oracle 解除锁表sql
select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b ...
- HDU 3003
找规律吧.可以快速幂模 #include <iostream> #include <cstdio> using namespace std; __int64 Power(__i ...
- Grace Hopper 葛丽丝 霍普
Grace Murray Hopper(1906-1992), COBOL之母, Debug之母, A ship in port is safe, but that is not what ships ...
- java中File的delete()方法删除文件失败的原因
java中File的delete()方法删除文件失败的原因 学习了:http://hujinfan.iteye.com/blog/1266387 的确是忘记关闭了: 引用原文膜拜一下: 一般来说 ja ...