HLG 2120Ranking System【树状数组+map+二分】

来源:互联网 发布:游戏显示器推荐 知乎 编辑:程序博客网 时间:2024/06/08 10:38

Description

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.


Input

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 .


Output

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.


Sample Input
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 Output
31OS3ZZJFSNEG6ZZ8

代码:


#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;
}


0 0