Gym 101572G Galactic Collegiate Programming Contest(离线 树状数组)

来源:互联网 发布:系统数据流程图 编辑:程序博客网 时间:2024/05/17 21:47
题意:ACM比赛每个队伍的分数由(a, b)组成, a是做出题数, b是总罚时, 出题多的排名前, 相同题量罚时少排名前, 如果题量罚时都相同, 排名一样, 例如, (1, 1), (1, 1), (0, 0)三支队伍排名为1, 1, 2 现在有n支队伍, m个事件, 每个事件由(t, p)组成, 代表队伍t以罚时p做出了一题让你输出每个事件后, 队伍1的排名


思路:先预处理出所有可能的(a, b), 离散化后就能利用树状数组维护了。分数(a, b)可以看作是一维的。


代码:

#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5;int n, m;struct node{    int num, pea;    node() {}    node(int nn, int pp): num(nn), pea(pp) {}    bool operator < (const node &a)const    {        if(num == a.num) return pea < a.pea;        else return num > a.num;    }}Hash[maxn], a[maxn];int t[maxn], p[maxn], tree[maxn];int lowbit(int x){    return x&(-x);}void update(int pos, int val){    while(pos < maxn)    {        tree[pos] += val;        pos += lowbit(pos);    }}int query(int pos){    int res = 0;    while(pos)    {        res += tree[pos];        pos -= lowbit(pos);    }    return res;}int main(void){    while(cin >> n >> m)    {        memset(tree, 0, sizeof(tree));        memset(a, 0, sizeof(a));        int cnt = 0;        for(int i = 1; i <= m; i++)        {            scanf("%d%d", &t[i], &p[i]);            a[t[i]].num++, a[t[i]].pea += p[i];            Hash[cnt++] = node(a[t[i]].num, a[t[i]].pea);        }        sort(Hash, Hash+m);        update(m+1, n);        memset(a, 0, sizeof(a));        for(int i = 1; i <= m; i++)        {            int tmp = lower_bound(Hash, Hash+m, a[t[i]])-Hash+1;            update(tmp, -1);            a[t[i]].num++;            a[t[i]].pea += p[i];            tmp = lower_bound(Hash, Hash+m, a[t[i]])-Hash+1;            update(tmp, 1);            int pos = lower_bound(Hash, Hash+m, a[1])-Hash+1;            printf("%d\n", query(pos-1)+1);        }    }    return 0;}


阅读全文
1 0
原创粉丝点击