1. 程式人生 > 其它 >部落格測試

部落格測試

構建play
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define MAX 500100
int root=0,N,tot=0;

inline int read()
{
       register int x=0,t=1;
       register char ch=getchar();
       while
((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-'){t=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*t; } struct Node { int ch[2];//子節點 int ff;//父節點 int cnt;//數量 int val;// int son;//兒子數量 }t[MAX];
void push_up(int u)//計算兒子數 { t[u].son=t[t[u].ch[0]].son+t[t[u].ch[1]].son+t[u].cnt; } void rotate(int x)//旋轉 { register int y=t[x].ff; register int z=t[y].ff; register int k=t[y].ch[1]==x;//x是y的左或右兒子 t[z].ch[t[z].ch[1]==y]=x; t[x].ff=z; t[y].ch[k]=t[x].ch[k^1]; t[t[x].ch[k^1
]].ff=y; t[x].ch[k^1]=y; t[y].ff=x; push_up(y);push_up(x); } void Splay(int x,int goal)//把x節點旋轉到目標位置 { while(t[x].ff!=goal) { int y=t[x].ff; int z=t[y].ff; if(z!=goal)//旋轉 (t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y); rotate(x); } if(goal==0) root=x;//當前的根節點 } void insert(int x)//插入x { int u=root,ff=0; while(u&&t[u].val!=x) { ff=u; u=t[u].ch[x>t[u].val]; } if(u)//已經有這個數字了 t[u].cnt++;//計算數字個數 else//不存在這個數字,加入新的節點 { u=++tot;//總的節點數 if(ff) t[ff].ch[x>t[ff].val]=u; t[tot].ch[0]=0; t[tot].ch[1]=0; t[tot].ff=ff; t[tot].val=x; t[tot].cnt=1; t[tot].son=1; } Splay(u,0); } void Find(int x)//查詢x的位置 { int u=root; if(!u)return;//不存在節點,無法查詢排名 while(t[u].ch[x>t[u].val]&&x!=t[u].val)//找到x所在的位置 u=t[u].ch[x>t[u].val]; Splay(u,0); } int Next(int x,int f)//查詢前驅/後繼 { Find(x);//查詢x的位置(Splay操作到根節點) int u=root; if((t[u].val>x&&f)||(t[u].val<x&&!f))return u;//返回結果 u=t[u].ch[f]; while(t[u].ch[f^1])u=t[u].ch[f^1]; return u; } void Delete(int x)//刪除x { int last=Next(x,0);//查詢前驅 int next=Next(x,1);//查詢後繼 Splay(last,0);Splay(next,last); int del=t[next].ch[0]; if(t[del].cnt>1) { t[del].cnt--;//存在多個這個數字,直接減去一個 Splay(del,0); } else t[next].ch[0]=0;//清除掉節點 } int K_th(int x)//查詢排名為x的值 { int u=root; if(t[u].son<x)//不存在這麼多個數 return false; while(2333) { int y=t[u].ch[0]; if(x>t[y].son+t[u].cnt)//在排名在u的後面 { x-=t[y].son+t[u].cnt;//直接減掉這麼多 u=t[u].ch[1];//在右子樹中繼續找 } else if(t[y].son>=x)//如果y的節點數多於x u=y; //在左子樹中繼續查詢 else return t[u].val;//否則找到了結果,直接返回 } } int main() { insert(-2147483647); insert(+2147483647); N=read(); while(N--) { int opt=read(); if(opt==1) insert(read()); else if(opt==2) Delete(read()); else if(opt==3) { Find(read()); printf("%d\n",t[t[root].ch[0]].son); } else if(opt==4) printf("%d\n",K_th(read()+1)); else if(opt==5) printf("%d\n",t[Next(read(),0)].val); else if(opt==6) printf("%d\n",t[Next(read(),1)].val); } return 0; }