zoukankan      html  css  js  c++  java
  • bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统

    题目链接:1,2

    treap恶心题,不多说

      1 #include<algorithm>
      2 #include<iostream>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<cstdio>
      6 #include<string>
      7 #include<cmath>
      8 #include<queue>
      9 #include<stack>
     10 #include<map>
     11 #include<set>
     12 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
     13 #define re(i,l,r) for(int i=(l);i<=(r);i++)
     14 #define Clear(a,b) memset(a,b,sizeof(a))
     15 #define inout(x) printf("%d",(x))
     16 #define douin(x) scanf("%lf",&x)
     17 #define strin(x) scanf("%s",(x))
     18 #define LLin(x) scanf("%lld",&x)
     19 #define op operator
     20 #define CSC main
     21 typedef unsigned long long ULL;
     22 typedef const int cint;
     23 typedef long long LL;
     24 using namespace std;
     25 cint mod=985003;int n,root,sz,tot,head[mod+1];
     26 struct data1
     27 {
     28     int v,t,next;
     29     char ch[10];
     30 }hash[250001];
     31 struct data2
     32 {
     33     int l,r,v,s,rr,t;
     34     char ch[10];
     35 }tp[250001];
     36 void maintain(int k){tp[k].s=tp[tp[k].l].s+tp[tp[k].r].s+1;}
     37 void rotate2(int &k)
     38 {
     39     int t=tp[k].l;
     40     tp[k].l=tp[t].r;
     41     tp[t].r=k;tp[t].s=tp[k].s;
     42     maintain(k);k=t;
     43 }
     44 void rotate1(int &k)
     45 {
     46     int t=tp[k].r;
     47     tp[k].r=tp[t].l;
     48     tp[t].l=k;
     49     tp[t].s=tp[k].s;
     50     maintain(k);k=t;
     51 }
     52 bool com(char *a,char *b)
     53 {
     54     for(int i=1;i<(int)max(strlen(a),strlen(b));i++)
     55         if(a[i]!=b[i])return 0;
     56     return 1;
     57 }
     58 int HASH(char *ch)
     59 {
     60     int s=0; 
     61     for(int i=1;i<(int)strlen(ch);i++)
     62     {
     63         s*=27;s+=(ch[i]-'A'+1);
     64         s%=mod;
     65     }
     66     return s;
     67 }
     68 void del(int &k,int x,int t)
     69 {
     70     if(tp[k].v==x)
     71     {
     72         if(tp[k].t==t)
     73         {
     74             if(tp[k].l*tp[k].r==0)k=tp[k].l+tp[k].r;
     75             else if(tp[tp[k].l].rr<tp[tp[k].r].rr){rotate2(k);del(k,x,t);}
     76             else {rotate1(k);del(k,x,t);}
     77         }
     78         else if(t>tp[k].t){tp[k].s--;del(tp[k].l,x,t);}
     79         else {tp[k].s--;del(tp[k].r,x,t);}
     80     }
     81     else if(x<tp[k].v){tp[k].s--;del(tp[k].l,x,t);}
     82     else {tp[k].s--;del(tp[k].r,x,t);}
     83  }
     84 void add(int &k,char *ch,int x,int t)
     85 {
     86     if(k==0)
     87     {
     88         sz++;k=sz;tp[k].v=x;tp[k].s=1;tp[k].rr=rand();
     89         memcpy(tp[k].ch,ch,strlen(ch));tp[k].t=t;
     90         return;
     91     }
     92     tp[k].s++;
     93     if(x<=tp[k].v){add(tp[k].l,ch,x,t);if(tp[tp[k].l].rr<tp[k].rr)rotate2(k);}
     94     else{add(tp[k].r,ch,x,t);if(tp[tp[k].r].rr<tp[k].rr)rotate1(k);}
     95  }
     96 void add(char *ch,int x,int t)
     97 {
     98     int k=HASH(ch);int i=head[k];
     99     while(i)
    100     {
    101         if(com(hash[i].ch,ch))
    102         {
    103             del(root,hash[i].v,hash[i].t);
    104             hash[i].t=t;hash[i].v=x;
    105             add(root,ch,x,t);
    106             return;
    107         }
    108         i=hash[i].next;
    109     }
    110     tot++;
    111     hash[tot].t=t;hash[tot].v=x;
    112     memcpy(hash[tot].ch,ch,strlen(ch));
    113     hash[tot].next=head[k];head[k]=tot;
    114     add(root,ch,x,t);
    115 }
    116 int hh(char *ch)
    117 {
    118     int k=HASH(ch);int i=head[k];
    119     while(i)
    120     {
    121         if(com(hash[i].ch,ch))return i;
    122         i=hash[i].next;
    123     }return 0;
    124 }
    125 int findrank(int k,int x,int t)
    126 {
    127     if(k==0)return 0;
    128     if(tp[k].v==x)
    129     {
    130         if(tp[k].t>t)return findrank(tp[k].r,x,t);
    131         else if(tp[k].t<t)return 1+tp[tp[k].r].s+findrank(tp[k].l,x,t);
    132         else return tp[tp[k].r].s+1;
    133     }
    134     else if(tp[k].v<x)return findrank(tp[k].r,x,t);
    135     else return 1+tp[tp[k].r].s+findrank(tp[k].l,x,t);
    136 }
    137 void query1(char *ch)
    138 {
    139     int t=hh(ch);
    140     printf("%d
    ",findrank(root,hash[t].v,hash[t].t));
    141 }
    142 int findwei(int k,int x)
    143 {
    144     if(tp[tp[k].r].s+1==x)return k;
    145     else if(x<=tp[tp[k].r].s)return findwei(tp[k].r,x);
    146     else return findwei(tp[k].l,x-tp[tp[k].r].s-1);
    147 }
    148 void query2(char *ch)
    149 {
    150     int s=0;
    151     for(int i=1;i<(int)strlen(ch);i++){s*=10;s+=ch[i]-'0';}
    152     for(int i=s;i<=tot&&i<=s+9;i++)
    153     {
    154         printf("%s",tp[findwei(root,i)].ch+1);
    155         if(i<tot&&i<s+9)printf(" ");
    156     }
    157     printf("
    ");
    158 }
    159 int CSC()
    160 {
    161     scanf("%d",&n);char ch[11];int x;
    162     for(int i=1;i<=n;i++)
    163     {
    164         scanf("%s",ch);
    165         if(ch[0]=='+'){scanf("%d",&x);add(ch,x,i);}
    166         else if(ch[1]>='A'&&ch[1]<='Z')query1(ch);
    167         else query2(ch);
    168     }
    169     return 0;
    170 }
  • 相关阅读:
    消息中间件(一)MQ详解及四大MQ比较
    SIP协议
    PAT (Basic Level) Practice 1008 数组元素循环右移问题
    LeetCode-Algorithms 1. 两数之和
    PAT (Basic Level) Practice 1040 有几个PAT
    PAT (Basic Level) Practice 1023 组个最小数
    PAT (Basic Level) Practice 1021 个位数统计
    PAT (Basic Level) Practice 1007 素数对猜想
    PAT (Basic Level) Practice 1006 换个格式输出整数
    PAT (Basic Level) Practice 1004 成绩排名
  • 原文地址:https://www.cnblogs.com/HugeGun/p/5201945.html
Copyright © 2011-2022 走看看