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

      

  • 相关阅读:
    一个帖子掌握android所有控件、ProgressBar 、Android 动画效果、SQLite、四大组件、Android多媒体(转
    Android开发交流群
    我的程序里 《我的歌声里》程序员版
    《老罗Android开发视频教程安卓巴士》(Android 开发)
    #百度360大战# 我为什么要支持360
    安卓巴士移动开发者周刊第九期
    水杯题的非常好的解释
    [LeetCode] Jump Game
    [LeetCode] Longest Common Prefix
    [CareerCup][Google Interview] 寻找动态的中位数
  • 原文地址:https://www.cnblogs.com/bk-201/p/7401043.html
Copyright © 2020-2023  润新知