HLG 2120Ranking System【树状数组+map+二分】
来源:互联网 发布:游戏显示器推荐 知乎 编辑:程序博客网 时间:2024/06/08 10:38
Wangwang recently playing a game .
The game has a simple ranking system , only three requests :
1. Upload a new score record ;
2. Query a player's current rank ;
3. Ask who is ranked x.
When a player uploads his latest score record, his original record will be overwritten.
There are multiple test cases , process to the end of the file .
For each test , the first line is an integer n (1 <= n <= 250,000), represents the total number of requests. The next n lines, each line contains a request.
The specific format of the request is as follows :
ADD Name Score: uploads the latest score. Name represents a player's name, only contains uppercase letters and it's length is no more than 10 characters. 0 < Score < = 1000,000,000, all scores are unique.
RANK Name: returns a player's rank who's name is Name. The game player's score must have been upload before query his rank.
NAME Index: returns a player's name who's rank is Index. Index must be valid, that is not less than 1, nor more than the total number of players.
The sum of all n is no more than 500,000 .
For each test case:
For requests "RANK Name", output one line contains an integer indicating the player's current rank.
For requests "NAME Index", output one line contains the player's name who's rank is Index.
After the end of each test case outputs a blank line.
20ADD ZZ 606ADD EA 201ADD OS 342RANK EARANK ZZNAME 2RANK EAADD EA 179ADD JF 827NAME 2NAME 1ADD SN 540NAME 3ADD EG 611NAME 2ADD YJ 198ADD QA 556RANK OSNAME 3RANK EA
Sample Output31OS3ZZJFSNEG6ZZ8
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mem(a) memset(a, 0, sizeof(a))
#define INF 0x1f1f1f1f
#define M 250005
using namespace std;
int Case = 1;
int a[M], id[M], n, cnt;
struct Xue {
char s;
int id, v;
char name[15];
}q[M];
int lowbit(int t) {
return t & (-t);
}
void insert(int t, int d) {
while(t <= cnt) {
a[t] += d;
t += lowbit(t);
}
}
int getsum(int t) {
int res = 0;
while(t > 0) {
res += a[t];
t -= lowbit(t);
}
return res;
}
int cmp(int a, int b) {
return q[a].v > q[b].v;
}
int find(int k) {
int l = 1, r = cnt, mid;
while(l < r) {
mid = (r-l)/2+l;
if(getsum(mid) < k) {
l = mid+1;
}
else {
r = mid;
}
}
return l;
}
int main()
{
while(~scanf("%d", &n)) {
mem(a);
char s[8];
cnt = 1;
for(int i = 0; i < n; i++) {
scanf("%s", s);
q[i].s = s[0];
if(s[0] == 'A') {
scanf("%s%d", q[i].name, &q[i].v);
id[cnt++] = i;
}
else if(s[0] == 'R') {
scanf("%s", q[i].name);
}
else {
scanf("%d", &q[i].v);
}
}
sort(id+1, id+cnt+1, cmp);
for(int i = 1; i <= cnt; i++) {
q[id[i]].id = i;
}
map <string, int> mp;
for(int i = 0; i < n; i++) {
if(q[i].s == 'A') {
int x = mp[q[i].name];
if(x > 0) {
insert(x, -1);
}
mp[q[i].name] = q[i].id;
insert(q[i].id, 1);
}
else if(q[i].s == 'R') {
printf("%d\n", getsum(mp[q[i].name]));
}
else {
int x = find(q[i].v);
printf("%s\n", q[id[x]].name);
}
}
printf("\n");
}
return 0;
}
- HLG 2120Ranking System【树状数组+map+二分】
- HDU 1166 && HLG 1794 敌兵布阵(树状数组)
- Ranking System
- Ranking System
- poj2892 树状数组+二分
- poj2182(二分+树状数组)
- hdu5493 树状数组+二分
- hdu5493Queue 树状数组+二分
- hdoj5493【树状数组+二分】
- HDU2852【树状数组+二分】
- Codeforces846D(二分+树状数组)
- CodeForces540E【树状数组+二分】
- ZOJ2112 Dynamic Ranking (主席树+树状数组)
- 洛谷P2617 Dynamic Ranking 树状数组套主席树
- 【BZOJ1901】【Luogu2617】Dynamic Ranking(主席树,树状数组)
- HLG 1159 MAGI System
- HLG 1039 修路 二分
- zoj 3279【树状数组+二分】
- Linux下安装python及更改系统原有python
- IOS之数据持久化
- Learning Python
- mysql设置外键(主键依赖)
- 高性能Mysql数据库表设计原则
- HLG 2120Ranking System【树状数组+map+二分】
- oracle分区交换
- 张小姐,欢迎你回来
- JS实现的动态显示时间
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- flare-1.0.18 Centos 源码安装记录
- 垃圾见风使舵
- dir stat dirent
- 回收器在32位环境下有内存泄漏