[洛谷P4291][HAOI2008]排名系统
题目大意:三种操作:
- $+Name\;Socore:$上传最新得分记录,把以前的记录删除。
- $?Name:$ 查询玩家排名。如果两个玩家的得分相同,则先得到该得分的玩家排在前面。
- $?Index:$ 返回自第$Index$名开始的最多$10$名玩家名字。
题解:平衡树,支持删除,插入,查询第$k$名,查询一个玩家的排名。
卡点:新建节点时忘记赋$size$
C++ Code:
#include <cstdio>
#include <map>
#include <iostream>
#define maxn 250010
std::map<std::string, int> name;
int n, namenum;
struct node {
int v, p;
inline node(int __v = 0, int __p = 0) {v = __v, p = __p;}
inline friend bool operator > (const node &lhs, const node &rhs) {
if (lhs.v == rhs.v) return lhs.p < rhs.p;
return lhs.v > rhs.v;
}
inline friend bool operator == (const node &lhs, const node &rhs) {return lhs.v == rhs.v && lhs.p == rhs.p;}
inline friend bool operator >= (const node &lhs, const node &rhs) {return lhs > rhs || lhs == rhs;}
}; namespace Treap {
int lc[maxn], rc[maxn], num[maxn], sz[maxn];
node val[maxn];
int root, idx;
int ta, tb, tmp, res;
int seed = 20040826;
inline int rand() {return seed *= 48271;} inline int update(int rt) {
sz[rt] = sz[lc[rt]] + sz[rc[rt]] + 1;
return rt;
}
inline int nw(node x) {
val[++idx] = x;
num[idx] = rand();
sz[idx] = 1;
return idx;
}
void split(int rt, node k, int &x, int &y) {
if (!rt) x = y = 0;
else {
if (val[rt] >= k) split(rc[rt], k, rc[rt], y), x = update(rt);
else split(lc[rt], k, x, lc[rt]), y = update(rt);
}
}
void split(int rt, int k, int &x, int &y) {
if (!rt) x = y = 0;
else {
if (sz[lc[rt]] < k) split(rc[rt], k - sz[lc[rt]] - 1, rc[rt], y), x = update(rt);
else split(lc[rt], k, x, lc[rt]), y = update(rt);
}
}
int merge(int x, int y) {
if (!x || !y) return x | y;
if (num[x] < num[y]) {rc[x] = merge(rc[x], y); return update(x);}
else {lc[y] = merge(x, lc[y]); return update(y);}
} inline int gtrnk(node x) {
split(root, x, ta, tb);
res = sz[ta];
root = merge(ta, tb);
return res;
}
inline node gtkth(int k) {
tmp = root;
while (true) {
if (sz[lc[tmp]] >= k) tmp = lc[tmp];
else {
if (sz[lc[tmp]] + 1 == k) return val[tmp];
else k -= sz[lc[tmp]] + 1, tmp = rc[tmp];
}
}
} void insert(node x) {
if (!root) root = nw(x);
else {
split(root, x, ta, tb);
root = merge(merge(ta, nw(x)), tb);
}
}
void erase(node x) {
split(root, x, ta, tb);
split(ta, sz[ta] - 1, ta, tmp);
root = merge(ta, tb);
}
} int val[maxn];
std::string retname[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
char op;
std::string s;
std::cin >> op >> s;
if (op == '+') {
int x;
std::cin >> x;
if (name.count(s)) {
int pos = name[s];
Treap::erase(node(val[pos], pos));
namenum--;
}
namenum++;
name[s] = i;
retname[i] = s;
val[i] = x;
Treap::insert(node(x, i));
} else if (isdigit(s[0])) {
int pos = 0, posend;
for (std::string::iterator it = s.begin(); it != s.end(); it++) pos = pos * 10 + (*it & 15);
posend = std::min(pos + 9, namenum);
for (int i = pos; i <= posend; i++) {
std::cout << retname[Treap::gtkth(i).p];
putchar(i == posend ? '\n' : ' ');
}
} else {
int pos = name[s];
std::cout << Treap::gtrnk(node(val[pos], pos)) << std::endl;
} }
return 0;
}
[洛谷P4291][HAOI2008]排名系统的更多相关文章
- 2021.12.07 P4291 [HAOI2008]排名系统(Treap)
2021.12.07 P4291 [HAOI2008]排名系统(Treap) https://www.luogu.com.cn/problem/P4291 双倍经验: https://www.luog ...
- P4291 [HAOI2008]排名系统
题目描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务器负担,在返回 ...
- 【BZOJ1056】[HAOI2008]排名系统(Splay)
[BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...
- 数据结构(Splay平衡树):HAOI2008 排名系统
[HAOI2008] 排名系统 [题目描述] 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录 ...
- bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 502[Submit][Statu ...
- [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2487 Solved: 711[Submit][Statu ...
- BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay
BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...
- 洛谷 P4290 [HAOI2008]玩具取名
传送门 思路 博客半年没更新了,来更新个博文吧 在\(dsr\)聚聚博客的帮助下,我用半个上午和一个中午的时间苟延残喘地完成了这道题 先是读题目读大半天,最后连个样例都看不懂 之后又是想思路,实在想不 ...
- 【洛谷 P4291】 [HAOI2008]排名系统(Splay,Trie)
题目链接 不是双倍经验我会去\(debug\)一上午? 一开始我是用的\(map+string\),跑的太慢了,T了4个点. 后来我手写了\(string\),重载了小于号,依然用的\(map\),T ...
随机推荐
- Ajax之eval()函数
Ajax之eval()函数 <!DOCTYPE html> <html> <head lang="en"> <meta charset=& ...
- 学习photoshop心得
简要的学习了ps的三大功能p图,抠图,作图, p图主要是学了换脸这一效果,用到套索工具,把范冰冰的脸接到郭德纲身上, 首先使用套索工具把脸圈起来 然后移动到 另一个人脸上 再然后混合图层,自动混合 差 ...
- Intellij 出现“Usage of API documented as @since 1.4+”的解决办法
https://blog.csdn.net/wust_lh/article/details/73277185
- 动态规划----FatMouse’s Speed(HDU 1160)
参考:https://blog.csdn.net/u012655441/article/details/64920825 https://blog.csdn.net/wy19910326/articl ...
- STL 入门 (17 暑假集训第一周)
快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...
- Spark Streaming实时处理应用
1 框架一览 事件处理的架构图如下所示. 2 优化总结 当我们第一次部署整个方案时,kafka和flume组件都执行得非常好,但是spark streaming应用需要花费4-8分钟来处理单个 ...
- Delphi中ModalResult的使用
Delphi中ModalResult的功能非常实用. 在自己设计的Dialog界面中,选择相应的按钮,设置按钮的 ModalResult属性为mrOK .mrCancel 等.这样的设置,当按下该按钮 ...
- javascript-es6学习笔记
es6技术培训文档 第一阶段:1.let与const用法2.变量的解构赋值3.字符串的扩展4.正则的扩展5.数组的扩展6.函数的扩展7.对象的扩展8.Symbol9.Set和Map数据结构 第二阶段: ...
- 第十九章 Python os模块,pathlib 判断文件是目录还是文件
OS模块 os.path.abspath() :返回path规范化的绝对路径 import sys import os BASE_DIR = os.path.dirname(os.path.dirna ...
- Python面试315题
感谢老男孩的武沛齐老师辛苦整理和分享,本文是转自他的博客. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C# ...