山东济南彤昌机械科技有限公司 山东济南江鹏工贸游有限公司

bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)

1056: [HAOI2008]排名系统

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1854  Solved: 502
[Submit][Status][Discuss]

Description

排名系统通常要应付三种请求:上传一条新的得分记录、查询某个玩家的当前排名以及返回某个区段内的排名记录。当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除。为了减轻服务器负担,在返回某个区段内的排名记录时,最多返回10条记录。

Input

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

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
4

HINT

100%数据满足N<=250000

 

【题解】

       名次树+哈希表+处理相同结点。

       总的思路就是利用名次树维护排名系统,利用插入时间clock标识每个节点,利用哈希表实现由name到插入时间t以及键值v的映射,这样就可以根据(v,t)在ranktree中实现查找。

       其中?name操作可以如下完成:

 

 
int ans_cnt,ans[maxn];
void query(Node* o,int pos,int pre) {
    if(o==NULL) return ;
    int s=(o->ch[1]==NULL? 0:o->ch[1]->s);
    int rank=pre+s+1;
    if(rank>=pos && rank<=pos+9) {
        query(o->ch[1],pos,pre);
        ans[ans_cnt++]=o->c;
        query(o->ch[0],pos,pre+s+1);
    }
    else {
        if(rank<pos) query(o->ch[0],pos,pre+s+1);
        else  query(o->ch[1],pos,pre);
    }
}

 

       需要注意的有:

              1)插入时将相同结点插入右子树,虽然可能会出现通过rotate旋转上来的情况,但其t的相对顺序不会发生改变,因此在遍历中如果遇到相等需要通过t判断所处子树的位置。

              2) 行末无空格。

     3) 不能以通过不旋转相同结点即破坏堆性质的方法处理相同,刚开始实(zuo)验(si)性试了一下,导致Treap直接退化。

              4)bzoj上内存超了就是tle,另外如果要过1862我的代码还需要优化一下hash表以减少空间的浪费。

 

【代码】

 

  1 #include<ctime>
  2 #include<vector>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<iostream>
  6 #include<cstdlib>
  7 using namespace std;
  8  
  9 const int maxn = 1000000+10;
 10 const int MOD = 985003;
 11  
 12 struct Node {
 13     Node *ch[2];
 14     int v,r,s,c;
 15     Node(int w,int t) :v(w),c(t) { ch[0]=ch[1]=NULL; s=1; r=rand(); }
 16     int cmp(int x) const {
 17         if(v==x) return -1;
 18         return x<v? 0:1;
 19     }
 20     int maintain() {
 21         s=1;
 22         if(ch[0]!=NULL) s+=ch[0]->s;
 23         if(ch[1]!=NULL) s+=ch[1]->s;
 24     }
 25 };
 26 Node *root;
 27 void rotate(Node* &o,int d) {
 28     Node* k=o->ch[d^1]; o->ch[d^1]=k->ch[d],k->ch[d]=o;
 29     o->maintain(),k->maintain(),o=k;
 30 }
 31 void insert(Node* &o,int x,int t) {
 32     if(o==NULL) o=new Node(x,t);
 33     else {
 34         int d=x<=o->v? 0:1;
 35         insert(o->ch[d],x,t);
 36         if(o->ch[d]->r > o->r) rotate(o,d^1);
 37     }
 38     o->maintain();
 39 }
 40 void remove(Node* &o,int x,int t) {
 41     int d=o->cmp(x);
 42     if(d==-1 && o->c==t) {
 43         Node *u=o;
 44         if(o->ch[0]!=NULL && o->ch[1]!=NULL) {
 45             int d2=o->ch[0]->r > o->ch[1]->r? 1:0;
 46             rotate(o,d2); remove(o->ch[d2],x,t);
 47         }
 48         else {
 49             if(o->ch[0]!=NULL) o=o->ch[0];
 50             else o=o->ch[1];     //ch[0]==NULL && ch[1]==NULL
 51             delete u;
 52         }
 53     }
 54     else {
 55         if(d==-1) { if(t<o->c) d=1; else d=0; }
 56         remove(o->ch[d],x,t);
 57     }
 58     if(o!=NULL) o->maintain();
 59 }
 60 int rank(Node* o,int x,int t) {
 61     if(o==NULL) return -1; 
 62     int s=o->ch[1]==NULL? 0:o->ch[1]->s;
 63     int d=o->cmp(x);
 64     if(d==-1 && t==o->c) return s+1;
 65     else {
 66         if(d==-1)  { if(t<o->c) d=1; else d=0; }
 67         if(d==0) return s+1+rank(o->ch[0],x,t);
 68         else return rank(o->ch[1],x,t);
 69     }
 70 }
 71  
 72 int n,score,iclock;
 73 char tname[maxn][15],s[15];
 74 int tt[MOD][5],tv[MOD][5],size[MOD]; char flag[MOD][5][15];
 75  
 76 int ans_cnt,ans[maxn];
 77 void query(Node* o,int pos,int pre) {
 78     if(o==NULL) return ;
 79     int s=(o->ch[1]==NULL? 0:o->ch[1]->s);
 80     int rank=pre+s+1;
 81     if(rank>=pos && rank<=pos+9) {
 82         query(o->ch[1],pos,pre);
 83         ans[ans_cnt++]=o->c;
 84         query(o->ch[0],pos,pre+s+1);
 85     }
 86     else {
 87         if(rank<pos) query(o->ch[0],pos,pre+s+1);
 88         else  query(o->ch[1],pos,pre);
 89     }
 90 }
 91 int hash(char* s) {
 92     int h=0;
 93     for(int i=0;i<strlen(s);i++) h=h*27+s[i]-'A'+1,h%=MOD;
 94     return h;
 95 }
 96 int find(char *s) {
 97     int h=hash(s),i=0;
 98     while(i<size[h]) { if(strcmp(flag[h][i],s)==0) return i;  i++;
 99     }
100     size[h]++; return i;
101     //cout<<"sz: "<<size[h]<<endl;
102 }
103  
104 int main() {
105     freopen("rank.in","r",stdin);
106     freopen("rank.out","w",stdout);
107     scanf("%d",&n);
108     while(n--) {
109         scanf("%s",s);
110         int h,r;
111         if(s[0]=='+') {
112             scanf("%d",&score);
113             h=hash(s+1),r=find(s+1);
114             if(tv[h][r]) remove(root,tv[h][r],tt[h][r]);
115             insert(root,score,++iclock);
116             strcpy(tname[iclock],s);
117             strcpy(flag[h][r],s+1);
118             tt[h][r]=iclock,tv[h][r]=score;
119         }
120         else {
121             if(isalpha(s[1])) {
122                 h=hash(s+1),r=find(s+1);
123                 printf("%d\n",rank(root,tv[h][r],tt[h][r]));
124             }else {
125                 int pos=0;
126                 for(int i=1;i<strlen(s);i++) pos=pos*10+s[i]-'0';
127                 ans_cnt=0;
128                 query(root,pos,0);
129                 for(int i=0;i<ans_cnt;i++) {
130                     printf("%s",tname[ans[i]]+1);
131                     if(i<ans_cnt-1) putchar(' ');
132                 }
133                 putchar('\n');
134             }
135         }
136     }
137     return 0;
138 }

 

posted on 2015-12-16 17:50  hahalidaxin  阅读(427)  评论(0编辑  收藏  举报