【BZOJ1056&&1862】排名系统&&GameZ游戏排名系统

1862: [Zjoi2006]GameZ游戏排名系统

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1400  Solved: 525
[Submit][Status][Discuss]

Description

GameZ为他们最新推出的游戏开通了一个网站。世界各地的玩家都可以将自己的游戏得分上传到网站上。这样就可以看到自己在世界上的排名。得分越高,排名就越靠前。当两个玩家的名次相同时,先上传记录者优先。由于新游戏的火爆,网站服务器已经难堪重负。为此GameZ雇用了你来帮他们重新开发一套新的核心。排名系统通常要应付三种请求:上传一条新的得分记录、查询某个玩家的当前排名以及返回某个区段内的排名记录。当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除。为了减轻服务器负担,在返回某个区段内的排名记录时,最多返回10条记录。

Input

第一行是一个整数n(n>=10)表示请求总数目。接下来n行每行包含了一个请求。请求的具体格式如下: +Name Score 上传最新得分记录。Name表示玩家名字,由大写英文字母组成,不超过10个字符。Score为最多8位的正整数。 ?Name 查询玩家排名。该玩家的得分记录必定已经在前面上传。 ?Index 返回自第Index名开始的最多10名玩家名字。Index必定合法,即不小于1,也不大于当前有记录的玩家总数。输入文件总大小不超过2M。 NOTE:用C++的fstream读大规模数据的效率较低

Output

对于每条查询请求,输出相应结果。对于?Name格式的请求,应输出一个整数表示该玩家当前的排名。对于?Index格式的请求,应在一行中依次输出从第Index名开始的最多10名玩家姓名,用一个空格分隔。

Sample Input

20
+ADAM 1000000 加入ADAM的得分记录
+BOB 1000000 加入BOB的得分记录
+TOM 2000000 加入TOM的得分记录
+CATHY 10000000 加入CATHY的得分记录
?TOM 输出TOM目前排名
?1 目前有记录的玩家总数为4,因此应输出第1名到第4名。
+DAM 100000 加入DAM的得分记录
+BOB 1200000 更新BOB的得分记录
+ADAM 900000 更新ADAM的得分记录(即使比原来的差)
+FRANK 12340000 加入FRANK的得分记录
+LEO 9000000 加入LEO的得分记录
+KAINE 9000000 加入KAINE的得分记录
+GRACE 8000000 加入GRACE的得分记录
+WALT 9000000 加入WALT的得分记录
+SANDY 8000000 加入SANDY的得分记录
+MICK 9000000 加入MICK的得分记录
+JACK 7320000 加入JACK的得分记录
?2 目前有记录的玩家总数为12,因此应输出第2名到第11名。
?5 输出第5名到第13名。
?KAINE 输出KAINE的排名

Sample Output

2
CATHY TOM ADAM BOB
CATHY LEO KAINE WALT MICK GRACE SANDY JACK TOM BOB
WALT MICK GRACE SANDY JACK TOM BOB ADAM DAM

HINT

 

Source

Splay

 

 

首先声明一下 1862 如果按照O2优化 本机我绝对是过了的 甚至跑的比我实际贴上去的人(SLYZ的一位学姐的代码)跑的还要快

但是为什么会被卡时呢?我也不知道

(学姐的代码)

(我的)

以上为都不开O2的结果

开O2之后:

学姐的 重新建了个文件夹

我的

然而

我能怎么办?我也很绝望啊

回到正题

这个题还是挺裸的 我一开始想的用pair 然后发现我好像想错了 于是换结构体

然后结构体之后就可以直接上平衡树 

插入、查询、前驱都是挺显然的

输出x个数怎么做?先将那个排名的数转到根 然后输出根中左子树十个元素 自己yy一下应该能yy出来

然后就做完了 强烈安利这个题用hash 不然你会被卡到死

/*To The End Of The Galaxy*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
struct node
{
    ll val;int index;
};
inline bool operator < (node a,node b)
{
    if(a.val!=b.val)
    {
        return a.val<b.val;
    }
    else return a.index>b.index;
}
inline bool operator == (node a,node b)
{
    if(a.val==b.val&&a.index==b.index)
    {
        return true;
    }
    return false;
}
inline bool operator != (node a,node b)
{
    return !(a==b);
} 
struct String
{
    int length;
    char name[15];
};
inline bool operator < (String a,String b)
{
    int minn;
    minn=min(a.length,b.length);
    for(int i=1;i<=minn;i++)
    {
        if(a.name[i]<b.name[i])return true;
        else if(a.name[i]>b.name[i])return false;
    }
    return a.length<b.length;
}
int son[250005][2],size[250005],cnt[250005],fa[250005],root,id;
node val[250005],a[250005];
#define lson son[now][0]
#define rson son[now][1]
map<String,int> mp;
String name[250005];
inline void clear(int now)
{
    fa[now]=lson=rson=cnt[now]=size[now]=0;
    val[now]=((node){0,0});
    return;
}
inline bool lr(int now)
{
    return son[fa[now]][1]==now;
}
inline void update(int now)
{
    if(now)
    {
        size[now]=cnt[now];
        if(lson)size[now]+=size[lson];
        if(rson)size[now]+=size[rson];
    }
}
inline void rotate(int now)
{
    int nowfa=fa[now],par=fa[nowfa];bool which=lr(now);
    son[nowfa][which]=son[now][which^1];
    fa[son[nowfa][which]]=nowfa;
    son[now][which^1]=nowfa;
    fa[nowfa]=now;
    if(par)
    {
        son[par][son[par][1]==nowfa]=now;
    }
    fa[now]=par;
    update(nowfa);update(now);
}
inline void splay(int now,int tar)
{
    for(int f;(f=fa[now])!=tar;rotate(now))
    {
        if(fa[f]!=tar)
        {
            rotate(lr(now)==lr(f)?f:now);
        }
    }
    if(!tar)root=now;
}
inline void insert(int x)
{
    if(!root)
    {
        root=++id;
        cnt[id]++;val[id]=a[x];size[id]=1;
        return;
    }
    else
    {
        int now=root,nowfa=now;
        while(1)
        {
            nowfa=now;
            now=son[now][val[now]<a[x]];
            if(now==0)
            {
                fa[++id]=nowfa;
                son[nowfa][val[nowfa]<a[x]]=id;
                size[id]=1;cnt[id]=1;val[id]=a[x];update(nowfa);splay(id,0);
                return;
            }
        }
    }
}
inline int find(int x)
{
    int now=root,ans=0;
    while(1)
    {
        if(val[now]==a[x])
        {
            ans+=size[rson]+cnt[now];
            splay(now,0);return ans;
        }
        if(val[now]<a[x]&&rson)
        {
            now=rson;
        }
        else
        {
            ans+=size[rson]+cnt[now];
            now=lson;
        }
    }
}
inline int findx(int k)
{
    int now=root;
    while(1)
    {
        if(rson&&size[rson]>=k)
        {
            now=rson;
        }
        else
        {
            int temp=cnt[now]+size[rson];
            if(k<=temp)
            {
                splay(now,0);
                return now;
            }
            k-=temp;now=lson;
        }
    }
}
inline int pre()
{
    int now=son[root][0];
    while(rson)now=rson;
    return now;
}
inline void del(int x)
{
    int tmp=0,id=0;
    id=find(x);
    if(!son[root][0])
    {
        tmp=root;root=son[root][1];fa[root]=0;clear(tmp);return;
    }
    if(!son[root][1])
    {
        tmp=root;root=son[root][0];fa[root]=0;clear(tmp);return;
    }
    tmp=root;id=pre();
    splay(id,0);
    fa[son[tmp][1]]=root;
    son[root][1]=son[tmp][1];
    clear(tmp);update(root);
    return;
}
int tot=0,cou=0;
inline void print(int now,int k)
{
    int tmp;
    if(k==0)return;
    if(rson)
    {
        tmp=min(k,size[rson]);
        print(rson,tmp);
        k-=tmp;
    }
    if(k==0)return;
    String ou=name[val[now].index];
    k--;int len=ou.length;//printf("%s ",name[val[now].index]+1);
    for(int j=1;j<=len;j++)putchar(ou.name[j]); 
    cou++;
    if(tot!=cou)putchar(' ');
    print(lson,k);
    return;
}
int n;
char c,cc;
int main()
{
    ll x;
    register int maxlen;
    node tmp;
    n=init();
    String temp;
    for(register int i=1;i<=n;i++)
    {
        temp.length=0;
        memset(temp.name,0,sizeof(temp.name));
        while(c=getchar(),c!='?'&&c!='+');
        if(c=='+')
        {
            while(1)
            {
                cc=getchar();
                if(cc>'Z'||cc<'A')break;
                temp.length++;
                temp.name[temp.length]=cc;
            }
            x=llinit();
            tmp=a[mp[temp]];
            if(tmp!=((node){0,0}))
            {
                del(mp[temp]);
                a[i]=((node){x,i});
                insert(i);
                mp[temp]=a[i].index;
                name[i]=temp;
            }            
            else
            {
                a[i].val=x;a[i].index=i;
                insert(i);
                mp[temp]=a[i].index;
                name[i]=temp;
            }   
        }
        else
        {
            cc=getchar();
            if(cc>='0'&&cc<='9')
            {
                x=cc-'0';
                while(1)
                {
                    cc=getchar();
                    if(cc>='0'&&cc<='9')x=x*10+(cc-'0');
                    else break;
                }
                cou=0;
                tmp=val[findx(x)];
                temp=name[val[root].index];
                maxlen=temp.length;
                for(register int j=1;j<=maxlen;j++)putchar(temp.name[j]);
                tot=min(10,size[son[root][0]]+1);cou++;   
                if(tot!=cou)putchar(' ');            
                print(son[root][0],min(size[son[root][0]],9));
                printf("\n");
            }
            else
            {
                temp.name[++temp.length]=cc;
                while(1)
                {
                    cc=getchar();
                    if(cc>'Z'||cc<'A')break;
                    temp.length++;
                    temp.name[temp.length]=cc;
                }
                printf("%d\n",find(mp[temp]));
            }
        }
    }
    return 0;
}
/*
srO xudyh davidlee1999WTK linkct1999 Orz
compiler TDM-GCC 5.9.2
*/
/*
8
+ADDM 1000000a
+BOB 1000000
+TOM 2000000
+CATHY 10000000
?TOM
?1
+ADDM 800000
?TOM
*/
View Code

那么你们就当我把这两道题都A了吧 开新坑

留坑:

某知名神对我说:“你可以试试SBT”

于是留好坑 以后写SBT和Treap都很字词

posted @ 2017-03-20 01:44  redwind  阅读(574)  评论(0编辑  收藏  举报