[BZOJ 1056][BZOJ 1862][HAOI 2008][ZJOI 2006]GameZ游戏排名系统(排名系统)(Treap+哈希表)

来源:互联网 发布:wemall商业版java源码 编辑:程序博客网 时间:2024/05/16 18:09
题目链接:


http://www.lydsy.com:808/JudgeOnline/problem.php?id=1862

http://www.lydsy.com:808/JudgeOnline/problem.php?id=1056

ps:HAOI抄ZJOI题是闹哪样。。。

treap裸题,只是预处理太恶心,为了避免同一分数有多个人,要用一个数据加入时间进行标记,作为排序参考的第二关键字,建立三个哈希表映射关系:

1、time->name 数据加入时间->姓名

2、name->score 姓名->分数

3、name->time 姓名->数据加入时间

建议搞个结构体info表示每个数据的详细信息,然后运算符重载,就能直接套treap模板乱搞了。

另外此题有坑点:

1、每行输出最后不能有空格,否则PE

2、排序问题,按分数第一关键字降序,加入时间作为第二关键字升序。

代码太长实在坑爹,好在这题做一题得双倍经验,还是蛮爽的。

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <ctime>#include <climits>#include <map>#define MAXN 250010#define LC (tree[o].l)#define RC (tree[o].r)using namespace std;map<string,int>hash;map<string,int>fenshu;map<int,string>name;int tot=0; //已经加入玩家个数struct info{    int num,time;    bool operator<(const info &b)const    {        if(num!=b.num) return num>b.num;        else return time<b.time;    }    bool operator>(const info &b)const    {        if(num!=b.num) return num<b.num;        else return time>b.time;    }    bool operator==(const info &b)const    {        return num==b.num&&time==b.time;    }};struct node{    int s; //该点下的结点个数(包含该点)    int cnt; //该点的数字个数    info v; //结点键值    int l,r; //左右子树    int rnd; //结点的随机权值}tree[MAXN];int nCount=0;int ans;int root=0;void update(int o) //维护结点o{    tree[o].s=tree[LC].s+tree[RC].s+tree[o].cnt;}void rotateR(int &o) //右旋结点o{    int t=LC;    LC=tree[t].r;    tree[t].r=o;    tree[t].s=tree[o].s;    update(o);    o=t;}void rotateL(int &o) //左旋结点o{    int t=RC;    RC=tree[t].l;    tree[t].l=o;    tree[t].s=tree[o].s;    update(o);    o=t;}void insert(int &o,info x) //在根节点为o的树中插入键值为x的结点,若存在键值为x的结点,那么增加该结点对应数字个数{    if(o==0) //空树,建立新的根结点    {        o=++nCount;        tree[o].s=tree[o].cnt=1;        tree[o].rnd=rand()*rand();        tree[o].v=x;        return;    }    tree[o].s++;    if(x<tree[o].v) //x比根结点键值小,往左子树插    {        insert(LC,x);        if(tree[o].rnd>tree[LC].rnd) //注意维护堆的性质            rotateR(o);    }    else if(x>tree[o].v) //x比根结点键值大,往右子树插,下面操作类似上面    {        insert(RC,x);        if(tree[o].rnd>tree[RC].rnd)            rotateL(o);    }    else //x和根节点键值一样,那么增加根节点对数字个数        tree[o].cnt++;}void del(int &o,info x) //在根节点为o的树中删除<一个>键值为x的点{    if(o==0) return; //找不到可以删除的结点,退出    if(tree[o].v==x)    {        if(tree[o].cnt>1) tree[o].s--,tree[o].cnt--; //该点对应数字不止1个,不能删除这个结点        else if(LC==0||RC==0) o=LC+RC; //该点下只含一个儿子,删完这个点用它儿子来代替        else if(tree[LC].rnd<tree[RC].rnd) rotateR(o),del(o,x); //该点的左儿子权值比右儿子小,要维护堆的性质,右旋根结点o        else rotateL(o),del(o,x);        return;    }    tree[o].s--;    if(tree[o].v<x) del(RC,x);    else del(LC,x);}int getrank(int o,info x) //求键值为x的结点在树o中的排名{    if(o==0) return 0;    if(tree[o].v==x) return tree[LC].s+1;    else if(tree[o].v<x) return tree[LC].s+tree[o].cnt+getrank(RC,x);    else getrank(LC,x);}info getnum(int o,int x) //求树o中第x小的数{    info xx;    if(o==0) return xx;    if(x<=tree[LC].s) return getnum(LC,x);    else if(x>tree[LC].s+tree[o].cnt) return getnum(RC,x-tree[LC].s-tree[o].cnt);    else return tree[o].v;}int main(){    int n;    int time=0;    root=0;    char in[20];    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        scanf("%s",in);        string str(in+1);        if(in[0]=='+')        {            int score;            scanf("%d",&score);            if(!hash[str])            {                tot++;                info x;                x.num=score;                x.time=++time;                hash[str]=x.time;                fenshu[str]=x.num;                name[x.time]=str;                insert(root,x);            }            else            {                info x;                x.num=fenshu[str];                x.time=hash[str];                name[x.time]="0";                del(root,x);                x.num=score;                fenshu[str]=score;                x.time=++time;                hash[str]=time;                name[x.time]=str;                insert(root,x);            }        }        else        {            int ranker=0;            if(in[1]>='0'&&in[1]<='9')            {                int len=strlen(in+1);                for(int i=1;i<=len;i++)                {                    ranker*=10;                    ranker+=in[i]-'0';                }                for(int i=ranker;i<=tot&&i<=ranker+9;i++)                {                    if(i!=ranker) cout<<' ';                    cout<<name[getnum(root,i).time];                }                printf("\n");            }            else            {                info x;                x.time=hash[str];                x.num=fenshu[str];                printf("%d\n",getrank(root,x));            }        }    }    return 0;}


0 0