1. 程式人生 > >洛谷 P3369 【模板】普通平衡樹(Treap/SBT)

洛谷 P3369 【模板】普通平衡樹(Treap/SBT)

題目描述

您需要寫一種資料結構(可參考題目標題),來維護一些數,其中需要提供以下操作:

1.插入x數

2.刪除x數(若有多個相同的數,因只刪除一個)

3.查詢x數的排名(若有多個相同的數,因輸出最小的排名)

4.查詢排名為x的數

5.求x的前驅(前驅定義為小於x,且最大的數)

6.求x的後繼(後繼定義為大於x,且最小的數)

輸入輸出格式
輸入格式:

第一行為n,表示操作的個數,下面n行每行有兩個數opt和x,opt表示操作的序號(1<=opt<=6)

輸出格式:

對於操作3,4,5,6每行輸出一個數,表示對應答案

輸入輸出樣例

輸入樣例#1:
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

輸出樣例#1:
106465
84185
492737

說明

時空限制:1000ms,128M

1.n的資料範圍:n<=100000

2.每個數的資料範圍:[-1e7,1e7]

【分析】
真 · 模板

這個部落格真的很好懂↑

【程式碼】

//tyvj 1728 
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std; const int mxn=100005; int n,m,sz,opt,root; int f[mxn],ch[mxn][2],key[mxn],cnt[mxn],size[mxn]; inline void clear(int x) { f[x]=ch[x][0]=ch[x][1]=key[x]=cnt[x]=size[x]=0; } inline int get(int x) //左子樹or右子樹 { if(ch[f[x]][0]==x) return 0; return 1; } inline void update(int x) //更新size
{ if(!x) return; size[x]=cnt[x]; if(ch[x][0]) size[x]+=size[ch[x][0]]; if(ch[x][1]) size[x]+=size[ch[x][1]]; } inline void rotate(int x) { int old=f[x],elder=f[old],which=get(x); ch[old][which]=ch[x][which^1],f[ch[x][which^1]]=old; ch[x][which^1]=old,f[old]=x; f[x]=elder; if(elder) ch[elder][ch[elder][1]==old]=x; update(old),update(x); } inline void splay(int x) { for(int fa;(fa=f[x]);rotate(x)) if(f[fa]) rotate((get(x)==get(fa)?fa:x)); root=x; } inline void insert(int x) { if(!root) { root=(++sz); ch[sz][0]=ch[sz][1]=f[sz]=0; key[sz]=x,cnt[sz]=1; return; } int now=root,fa=0; while(1) { if(key[now]==x) { cnt[now]++; update(now),update(fa); splay(now); return; } fa=now;now=ch[now][x>key[now]]; if(now==0) { sz++; ch[sz][0]=ch[sz][1]=0,size[sz]=cnt[sz]=1,key[sz]=x; f[sz]=fa,ch[fa][x>key[fa]]=sz; update(fa); splay(sz); return; } } } inline int find(int x) //x的排名 { int ans=1,now=root; while(1) { if(x<key[now]) now=ch[now][0]; else { ans+=(ch[now][0]?size[ch[now][0]]:0); if(x==key[now]) {splay(now);return ans;} ans+=cnt[now]; now=ch[now][1]; } } } inline int number(int x) { int now=root; while(1) { if(ch[now][0] && x<=size[ch[now][0]]) now=ch[now][0]; else { int tmp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now]; if(x<=tmp) return key[now]; x-=tmp;now=ch[now][1]; } } } inline int pre() { int now=ch[root][0]; while(ch[now][1]) now=ch[now][1]; return now; } inline int next() { int now=ch[root][1]; while(ch[now][0]) now=ch[now][0]; return now; } inline void del(int x) { find(x); if(cnt[root]>1) {cnt[root]--;return;} //1.only one point if(!ch[root][0] && !ch[root][1]) {clear(root);root=0;return;} //2.no left child if(!ch[root][0]) {int old=root;root=ch[root][1];clear(old);f[root]=0;return;} //3.no right child if(!ch[root][1]) {int old=root;root=ch[root][0];clear(old);f[root]=0;return;} //4.two children int left=pre(),old=root; splay(left); f[ch[old][1]]=root; ch[root][1]=ch[old][1]; clear(old); update(root); } int main() { int i,j,x; scanf("%d",&n); fo(i,1,n) { scanf("%d%d",&opt,&x); if(opt==1) insert(x); if(opt==2) del(x); if(opt==3) printf("%d\n",find(x)); if(opt==4) printf("%d\n",number(x)); if(opt==5) insert(x),printf("%d\n",key[pre()]),del(x); if(opt==6) insert(x),printf("%d\n",key[next()]),del(x); } // printf("haha\n"); return 0; } //2 //1 106465 //1 317721