洛谷 P2596 [ZJOI2006]书架 (splay)
題目描述
小T有一個(gè)很大的書(shū)柜。這個(gè)書(shū)柜的構(gòu)造有些獨(dú)特,即書(shū)柜里的書(shū)是從上至下堆放成一列。她用1到n的正整數(shù)給每本書(shū)都編了號(hào)。
小T在看書(shū)的時(shí)候,每次取出一本書(shū),看完后放回書(shū)柜然后再拿下一本。由于這些書(shū)太有吸引力了,所以她看完后常常會(huì)忘記原來(lái)是放在書(shū)柜的什么位置。不過(guò)小T的記憶力是非常好的,所以每次放書(shū)的時(shí)候至少能夠?qū)⒛潜緯?shū)放在拿出來(lái)時(shí)的位置附近,比如說(shuō)她拿的時(shí)候這本書(shū)上面有X本書(shū),那么放回去時(shí)這本書(shū)上面就只可能有X-1、X或X+1本書(shū)。
當(dāng)然也有特殊情況,比如在看書(shū)的時(shí)候突然電話響了或者有朋友來(lái)訪。這時(shí)候粗心的小T會(huì)隨手把書(shū)放在書(shū)柜里所有書(shū)的最上面或者最下面,然后轉(zhuǎn)身離開(kāi)。
久而久之,小T的書(shū)柜里的書(shū)的順序就會(huì)越來(lái)越亂,找到特定的編號(hào)的書(shū)就變得越來(lái)越困難。于是她想請(qǐng)你幫她編寫(xiě)一個(gè)圖書(shū)管理程序,處理她看書(shū)時(shí)的一些操作,以及回答她的兩個(gè)提問(wèn):(1)編號(hào)為X的書(shū)在書(shū)柜的什么位置;(2)從上到下第i本書(shū)的編號(hào)是多少。
輸入輸出格式
輸入格式:
?
第一行有兩個(gè)數(shù)n,m,分別表示書(shū)的個(gè)數(shù)以及命令的條數(shù);第二行為n個(gè)正整數(shù):第i個(gè)數(shù)表示初始時(shí)從上至下第i個(gè)位置放置的書(shū)的編號(hào);第三行到m+2行,每行一條命令。命令有5種形式:
1. Top S——表示把編號(hào)為S的書(shū)放在最上面。
2. Bottom S——表示把編號(hào)為S的書(shū)放在最下面。
3. Insert S T——T∈{-1,0,1},若編號(hào)為S的書(shū)上面有X本書(shū),則這條命令表示把這本書(shū)放回去后它的上面有X+T本書(shū);
4. Ask S——詢問(wèn)編號(hào)為S的書(shū)的上面目前有多少本書(shū)。
5. Query S——詢問(wèn)從上面數(shù)起的第S本書(shū)的編號(hào)。
?
輸出格式:
?
對(duì)于每一條Ask或Query語(yǔ)句你應(yīng)該輸出一行,一個(gè)數(shù),代表詢問(wèn)的答案。
?
輸入輸出樣例
輸入樣例#1:?復(fù)制 10 10 1 3 2 7 5 8 10 4 9 6 Query 3 Top 5 Ask 6 Bottom 3 Ask 3 Top 6 Insert 4 -1 Query 5 Query 2 Ask 2 輸出樣例#1:?復(fù)制 2 9 9 7 5 3說(shuō)明
100%的數(shù)據(jù),n,m <= 80000
?
題解:先依次將點(diǎn)插入splay,權(quán)值為x[i]的第i個(gè)插入,splay維護(hù)的是順序
顯然插入到第x+1本或者第x-1本書(shū)的操作是將x的權(quán)值與他的后繼或者前驅(qū)交換
插到底或者頂就是將x的左子樹(shù)換到他的后繼的左子樹(shù)或者將x的右子樹(shù)換到他的前驅(qū)的右子樹(shù)
然后就可以用splay瞎搞了
?
代碼如下:
#include<set> #include<map> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #define lson ch[x][0] #define rson ch[x][1] #define mod 1000000007 using namespace std;int n,m; int pos[100010],sz[100010],key[100010],f[100010],ch[100010][3],cnt,rt;int push_up(int x) {sz[x]=sz[lson]+sz[rson]+1;pos[key[lson]]=lson;pos[key[rson]]=rson; }int rotate(int x) {int y=f[x],z=f[y],kd=(ch[y][1]==x),xs=ch[x][!kd];if(z) ch[z][ch[z][1]==y]=x;ch[x][!kd]=y;ch[y][kd]=xs;if(xs) f[xs]=y;f[x]=z;f[y]=x;push_up(y); }int splay(int goal,int x) {int y,z;while(f[x]!=goal){y=f[x],z=f[y];if(z!=goal){(ch[y][0]==x)^(ch[z][0]==y)?rotate(x):rotate(y);}rotate(x);}push_up(x);if(!goal) rt=x; }int insert(int x) {key[++cnt]=x;sz[cnt]=1;pos[x]=cnt;ch[cnt][0]=ch[cnt][1]=0;if(cnt>1){ch[cnt-1][1]=cnt;f[cnt]=cnt-1;splay(0,cnt);} }int find(int x,int tot) {if(sz[lson]+1==tot){return x;}else{if(sz[lson]+1<tot){return find(rson,tot-sz[lson]-1);}else return find(lson,tot);} }int get(int x,int kd) {int y=ch[x][kd];while(ch[y][!kd]){y=ch[y][!kd];}return y; }int make_top(int x) {x=pos[x];splay(0,x);if(!lson) return 0;if(!rson) swap(lson,rson);else{int y=get(x,1);f[lson]=y;ch[y][0]=lson;lson=0;splay(0,ch[y][0]);} }int make_buttom(int x) {x=pos[x];splay(0,x);if(!rson) return 0;if(!lson) swap(lson,rson);else{int y=get(x,0);f[rson]=y;ch[y][1]=rson;rson=0;splay(0,ch[y][1]);} }int change(int x,int kd) {if(!kd) return 0;splay(0,pos[x]);int y=(kd==1)?get(pos[x],1):get(pos[x],0);int tmp1=key[y],tmp2=pos[x];swap(pos[x],pos[tmp1]);swap(key[tmp2],key[y]); }void print(int x) {splay(0,x);printf("%d\n",sz[lson]); }void dayin(int x) {printf("%d %d %d\n",x,lson,rson);if(lson) dayin(lson);if(rson) dayin(rson); }int main() {ch[0][0]=ch[0][1]=sz[0]=f[0]=key[0]=pos[0]=0;scanf("%d%d",&n,&m);int tmp;for(int i=1; i<=n; i++){scanf("%d",&tmp);insert(tmp);}string op;while(m--){cin>>op;int tmp1,tmp2;if(op[0]=='T'){scanf("%d",&tmp1);make_top(tmp1);}if(op[0]=='B'){scanf("%d",&tmp2);make_buttom(tmp2);}if(op[0]=='I'){scanf("%d%d",&tmp1,&tmp2);change(tmp1,tmp2);}if(op[0]=='A'){scanf("%d",&tmp1);print(pos[tmp1]);}if(op[0]=='Q'){scanf("%d",&tmp1);printf("%d\n",key[find(rt,tmp1)]);}} }?
轉(zhuǎn)載于:https://www.cnblogs.com/stxy-ferryman/p/9588119.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的洛谷 P2596 [ZJOI2006]书架 (splay)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL语句中的select高级用法
- 下一篇: ASP NET Core --- HTT