HYSBZ 1862 GameZ游戏排名系统

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

将相等的值放到左子树下 自然就维护了先上传排名高的条件

维护名字可以用hash 这里为了省事直接用的map

因为这里的排名是从大到小 所以要选择后序遍历

这题有剧毒 数据跟题目描述不符 我开始inf=0x3f3f3f3f wa了好久 改成2e9就a了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define lson i<<1
#define rson i<<1|1
using namespace std;
const int N=3e5+5;
const int inf=2e9;
map<string,int>score,number;
map<int,string>name;
int f[N],ch[N][2],sz[N],key[N];
int totplayer,tot,root;
inline void Clear(int x)
{
    f[x]=ch[x][0]=ch[x][1]=sz[x]=key[x]=0;
}
inline int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    if (x)
    {
        sz[x]=1;
        if (ch[x][0]) sz[x]+=sz[ch[x][0]];
        if (ch[x][1]) sz[x]+=sz[ch[x][1]];
    }
}
void Rotate(int x)
{
    int fa=f[x],ff=f[fa],kind=get(x);
    ch[fa][kind]=ch[x][kind^1];f[ch[x][kind^1]]=fa;
    ch[x][kind^1]=fa;f[fa]=x;
    f[x]=ff;
    if (ff)
        ch[ff][ch[ff][1]==fa]=x;
    update(fa);
    update(x);
}
void splay(int x,int y)
{
    for(int fa;(fa=f[x])!=y;Rotate(x))
        if (f[fa]!=y)
            Rotate((get(fa)==get(x))?fa:x);
    if (y==0) root=x;
}
int Insert(int x)
{
    if (root==0)
    {
        root=++tot;
        ch[tot][0]=ch[tot][1]=f[tot]=0;
        key[tot]=x;sz[tot]=1;
        return tot;
    }
    int now=root,fa=0;
    while(1)
    {
        fa=now;
        now=ch[now][key[now]<x];
        if (now==0)
        {
            tot++;
            ch[tot][0]=ch[tot][1]=0;
            f[tot]=fa;ch[fa][key[fa]<x]=tot;
            key[tot]=x;sz[tot]=1;
            update(fa);
            splay(tot,0);
            return tot;
        }
    }
}
int pre()
{
    int now=ch[root][0];
    while(ch[now][1])
        now=ch[now][1];
    return now;
}
void del(int x)
{
    splay(x,0);
    int leftbig=pre(),oldroot=root;
    splay(leftbig,0);
    ch[root][1]=ch[oldroot][1];
    f[ch[oldroot][1]]=root;
    Clear(oldroot);
    update(root);
}
int Find(int x)
{
    int now=root;
    while(1)
    {
        if (ch[now][0]&&x<=sz[ch[now][0]])
            now=ch[now][0];
        else
        {
            int tem=1;
            if (ch[now][0])
                tem+=sz[ch[now][0]];
            if (x<=tem) return now;
            x-=tem;
            now=ch[now][1];
        }
    }
}
int st,ed;
void out(int x)
{
    if (ch[x][1]) out(ch[x][1]);
    st++;
    string s=name[x];
    int len=s.length();
    for(int i=0;i<len;i++)
        putchar(s[i]);
    if (st!=ed) putchar(' ');
    if (ch[x][0]) out(ch[x][0]);
}
void query(int l,int len)
{
    int y=totplayer-l+1;
    int x=totplayer-(l+len-1)+1;
    // x-1 y+1 还有-inf 所以就是x-1+1 y+1+1
    int aa=Find(x);
    int bb=Find(y+2);
    splay(aa,0);
    splay(bb,aa);
    st=0;
    ed=len;
    out(ch[ch[root][1]][0]);
}
void init()
{
    totplayer=tot=root=0;
    score.clear();
    number.clear();
    name.clear();
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        Insert(-inf);
        Insert(inf);
        string s;
        while(n--)
        {
            char c;
            scanf(" %c",&c);
            if(c=='+')
            {
                s="";
                while(1)
                {
                    c=getchar();
                    if (c==' ') break;
                    s+=c;
                }
                int t;
                scanf("%d",&t);
                if (number[s])
                {
                    del(number[s]);
                    score[s]=t;
                    int tem=Insert(t);
                    number[s]=tem;
                    name[tem]=s;
                }
                else
                {
                    ++totplayer;
                    score[s]=t;
                    int tem=Insert(t);
                    number[s]=tem;
                    name[tem]=s;
                }
            }
            else
            {
                s="";
                while(1)
                {
                    c=getchar();
                    if (c=='
') break;
                    s+=c;
                }
                if (s[0]>='0'&&s[0]<='9')
                {
                    int ans=0;
                    int len=s.length();
                    for(int i=0;i<len;i++)
                        ans=ans*10+s[i]-'0';
                    query(ans,min(10,totplayer-ans+1));
                    printf("
");
                }
                else
                {
                    splay(number[s],0);
                    printf("%d
",totplayer-sz[ch[root][0]]+1);
                }
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/bk-201/p/7401043.html