zoukankan      html  css  js  c++  java
  • bzoj 3224 Tyvj 1728 普通平衡树

    不知道为什么TLE了,%%yzy(yy2333)

    留个坑吧,等着再改改233

      1 /*#include<bits/stdc++.h>
      2 #define N 100005
      3 #define LL long long
      4 #define inf 0x3f3f3f3f
      5 #define ls tr[x][0]
      6 #define rs tr[x][1]
      7 using namespace std;
      8 inline int ra()
      9 {
     10     int x=0,f=1; char ch=getchar();
     11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
     12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
     13     return x*f;
     14 }
     15 int n,cnt;
     16 struct SPLAY{
     17     int fa[N],tr[N][2],root,v[N],size[N],w[N];
     18     bool which(int x){return tr[fa[x]][1]==x;}
     19     void update(int x){size[x]=size[ls]+size[rs]+w[x];}
     20     void rotate(int x)
     21     {
     22         int y=fa[x],z=fa[y]; bool nx=which(x),ny=which(y); 
     23         tr[y][nx]=tr[x][!nx]; fa[tr[x][!nx]]=y;
     24         tr[x][!nx]=y; fa[y]=x; fa[x]=z;
     25         if (z) tr[z][ny]=x; update(y);
     26     }
     27     void splay(int x, int aim)
     28     {
     29         while (fa[x]!=aim)
     30         {
     31             int y=fa[x],z=fa[y];
     32             if (z==aim) rotate(x);
     33             else if (which(x)==which(y)) rotate(y),rotate(x);
     34             else rotate(x),rotate(x);
     35         }
     36         if (!aim) root=x; update(x);
     37     }
     38     void insert(int val)
     39     {
     40         int x=root;
     41         while (1)
     42         {
     43             if (val==v[x]) {w[x]++;return;}
     44             else if (val<v[x])   {
     45                 if (!ls) {ls=++cnt,fa[cnt]=x,size[cnt]=1,splay(cnt,0),v[cnt]=val,w[cnt]=1; return;} else x=ls;}
     46             else {
     47                 if (!rs) {rs=++cnt,fa[cnt]=x,size[cnt]=1,splay(cnt,0),v[cnt]=val,w[cnt]=1; return;} else x=rs;}
     48         }
     49     }
     50     int getpre(int val) 
     51     {
     52         int x=root,pos,ans=-inf;
     53         while (1)
     54         { 
     55             if ((ls==0 && rs==0) || x==0) break;
     56             if (v[x]>=val) x=ls;
     57             else pos=x,x=rs;
     58         }
     59         return pos;
     60     }
     61     int getlst(int val)
     62     {
     63         int x=root,ans=inf,pos;
     64         while (1)
     65         {
     66             if ((ls==0 && rs==0) || x==0) break;
     67             if (v[x]<=val) x=rs;
     68             else pos=x,x=ls;
     69         }
     70         return pos;
     71     }
     72     int getrank(int val)
     73     {
     74         int x=root,rk=0;
     75         while (1)
     76         {       
     77             if (val==v[x]) {rk+=size[ls]+1; break;}
     78             else if (val>v[x]) {rk+=size[ls]+w[x]; rs=x;}
     79             else x=ls; 
     80         }
     81         return rk;
     82     }
     83     void del(int val)
     84     {
     85         int x=getrank(val),y;
     86         while (ls || rs)
     87         {
     88             if (size[ls]>size[rs]) y=ls;
     89             else y=rs;
     90             rotate(y);
     91         }
     92         if (w[tr[fa[x]][which(x)]]==1) tr[fa[x]][which(x)]=0;
     93             else w[tr[fa[x]][which(x)]]--,size[tr[fa[x]][which(x)]]--;
     94         while (x=fa[x]) update(x);
     95     }
     96     int rank(int k)
     97     {
     98         int x=root; 
     99         while (1)
    100         {
    101             if (k>=size[ls]+1 && k<=size[ls]+w[x]) break;
    102             else if (k<=size[ls]) x=ls;
    103             else k-=(size[ls]+w[x]),x=rs;
    104         }
    105         return v[x];
    106     }
    107 }T;
    108 int main()
    109 {
    110     n=ra();
    111     T.insert(-inf); T.insert(inf);
    112     for (int i=1; i<=n; i++)
    113     {
    114         int opt=ra(),x=ra();
    115         if (opt==1) T.insert(x);
    116         if (opt==2) T.del(x);
    117         if (opt==3) printf("%d
    ",T.getrank(x)-1);
    118         if (opt==4) printf("%d
    ",T.rank(x+1));
    119         if (opt==5) printf("%d
    ",T.v[T.getpre(x)]);
    120         if (opt==6) printf("%d
    ",T.v[T.getlst(x)]);
    121     }
    122     return 0;
    123 }*/
    124 #include<algorithm>
    125 #include<iostream>
    126 #include<cstdlib>
    127 #include<cstring>
    128 #include<cstdio>
    129 #include<vector>
    130 #include<queue>
    131 #include<cmath>
    132 #include<ctime>
    133 #include<set>
    134 #include<map>
    135 using namespace std;
    136 const int N=100005;
    137 int fa[N],ch[N][2],size[N],v[N],mx[N];
    138 int n,root,cnt;
    139 void push_up(int x)
    140 {
    141     size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    142     mx[x]=max(v[x],max(mx[ch[x][0]],mx[ch[x][1]]));
    143 }
    144 void rotate(int x)
    145 {
    146     int y=fa[x],z=fa[y],l,r;
    147     l=(ch[y][1]==x); r=l^1;
    148     if(z) ch[z][ch[z][1]==y]=x;
    149     ch[y][l]=ch[x][r]; ch[x][r]=y;
    150     fa[ch[y][l]]=y; fa[y]=x; fa[x]=z;
    151     push_up(y); push_up(x);
    152     if (root==y) root=x;
    153 }
    154 void splay(int x)
    155 {
    156     while(fa[x])
    157     {
    158         int y=fa[x],z=fa[y];
    159         if(z)
    160             if(ch[z][0]==y^ch[y][0]==x) rotate(x); else rotate(y);
    161         rotate(x);
    162     }
    163     root=x;
    164 }
    165 void insert(int &x,int f,int F)
    166 {
    167     //cout<<x<<" "<<f<<endl;
    168     if(!x)
    169     {
    170         v[x=++cnt]=f;
    171         size[x]=1;
    172         mx[x]=f;
    173         fa[x]=F;
    174         splay(x);
    175         //cout<<x<<" "<<size[x]<<" "<<ch[x][0]<<" "<<ch[x][1]<<endl;
    176         return;
    177     }
    178     if(f<v[x]) insert(ch[x][0],f,x);
    179     else insert(ch[x][1],f,x);
    180 }
    181 int find(int x,int f)
    182 {
    183     if(v[x]==f) return x;
    184     else if(v[x]<f) return find(ch[x][1],f);
    185     else return find(ch[x][0],f);
    186 }
    187 /*void del(int x)
    188 {
    189     int pos=find(root,x),y;
    190     while(ch[pos][0]&&ch[pos][1])
    191     {
    192         if(size[ch[pos][0]]>size[ch[pos][1]])
    193             y=ch[pos][0];
    194         else y=ch[pos][1];
    195         rotate(y);
    196         if(pos==root) root=y;
    197     }
    198     y=ch[pos][0]+ch[pos][1];
    199     if(y)
    200     {
    201         if(pos==root)
    202         {
    203             root=y;
    204             fa[root]=0;
    205         }
    206         else
    207         {
    208             fa[y]=fa[pos];
    209             ch[fa[pos]][ch[pos][1]==pos]=y;
    210         }
    211     }
    212     else ch[fa[pos]][ch[pos][1]==pos]=0;
    213     while(pos=fa[pos]) push_up(pos);
    214 }*/
    215  
    216 void del(int x)
    217 {
    218     int pos=find(root,x),y;
    219     while(ch[pos][0]||ch[pos][1])
    220     {
    221         if(size[ch[pos][0]]>size[ch[pos][1]])
    222             y=ch[pos][0];
    223         else y=ch[pos][1];
    224         rotate(y);
    225     }
    226     ch[fa[pos]][ch[fa[pos]][1]==pos]=0;
    227     while(pos=fa[pos])
    228     {
    229         push_up(pos);
    230         //cout<<pos<<" "<<size[pos]<<" "<<mx[pos]<<endl;
    231     }
    232 }
    233 int rank(int x,int f,int y)
    234 {
    235     //cout<<v[x]<<" "<<f<<" "<<size[x]<<endl;system("pause");
    236     if(v[x]==f)
    237     {
    238         if(mx[ch[x][0]]==f) return rank(ch[x][0],f,y);
    239         return size[ch[x][0]]+1+y;
    240     }
    241     else if(v[x]>f) return rank(ch[x][0],f,y);
    242     else return rank(ch[x][1],f,y+size[ch[x][0]]+1);
    243 }
    244 int num(int x,int f)
    245 {
    246     //cout<<x<<" "<<size[x]<<" "<<v[x]<<" "<<f<<endl; system("pause");
    247     if(size[ch[x][0]]+1==f) return v[x];
    248     else if(size[ch[x][0]]>=f) return num(ch[x][0],f);
    249     else return num(ch[x][1],f-size[ch[x][0]]-1);
    250 }
    251 int pre(int x,int f)
    252 {
    253     if(!x) return -1e9;
    254     if(v[x]<f) return max(v[x],pre(ch[x][1],f));
    255     else return pre(ch[x][0],f);
    256 }
    257 int nxt(int x,int f)
    258 {
    259     if(!x) return 1e9;
    260     if(v[x]>f) return min(v[x],nxt(ch[x][0],f));
    261     else return nxt(ch[x][1],f);
    262 }
    263   
    264 int main()
    265 {
    266     mx[0]=-1e9;
    267     scanf("%d",&n);
    268     for(int i=1;i<=n;i++)
    269     {
    270         int opt,x; 
    271         scanf("%d%d",&opt,&x);
    272         if(opt==1) insert(root,x,0);
    273         else if(opt==2) del(x);
    274         else if(opt==3) printf("%d
    ",rank(root,x,0));
    275         else if(opt==4) printf("%d
    ",num(root,x));
    276         else if(opt==5) printf("%d
    ",pre(root,x));
    277         else if(opt==6) printf("%d
    ",nxt(root,x));
    278     }
    279     return 0;
    280 }
  • 相关阅读:
    Linux中使用expect脚本实现远程机器自动登录_linux shell
    Linux: ssh命令 远程登录
    总结:PgSql备份pg_dump与还原pg_restore
    停电后,在UPS电源下服务器自动关机脚本
    总结:修改相关postgres用户密码
    问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
    delphi 播放mp3
    delphi 获取文件路径函数
    Delphi XE2 使用dbExpress连接MySQL数据库
    kbmmemsql 查询
  • 原文地址:https://www.cnblogs.com/ccd2333/p/6392034.html
Copyright © 2011-2022 走看看