1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include <iostream> #include <string> #include <cstdio>
#define INF 100000
using namespace std; int n,m;
namespace Splay { struct Node { int val,num,siz,fa,ch[2]; }t[1000001];
int rt,cnt,loc[1000001];
void upd(int x) { t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+t[x].num; }
int get(int x) { return x==t[t[x].fa].ch[1]; }
void rotate(int x) { int y=t[x].fa,z=t[y].fa,tmp=get(x); t[y].ch[tmp]=t[x].ch[tmp^1]; if (t[x].ch[tmp^1]) t[t[x].ch[tmp^1]].fa=y; t[x].ch[tmp^1]=y; t[y].fa=x; t[x].fa=z; if (z) t[z].ch[y==t[z].ch[1]]=x; upd(y); upd(x); }
void splay(int x,int goal) { for (int f=t[x].fa;f=t[x].fa,f!=goal;rotate(x)) if (t[f].fa!=goal) rotate(get(f)==get(x)?f:x); loc[t[x].val]=x; if (!goal) rt=x; }
int find_max() { int y=t[rt].ch[1]; if (!y) return rt; while (t[y].ch[1]) y=t[y].ch[1]; splay(y,0); return y; }
int find_min() { int y=t[rt].ch[0]; if (!y) return rt; while (t[y].ch[0]) y=t[y].ch[0]; splay(y,0); return y; }
int rk(int x) { splay(x,0); return t[t[x].ch[0]].siz; }
int kth(int x) { int y=rt; while (1) { if (x<=t[t[y].ch[0]].siz) y=t[y].ch[0]; else { x-=t[y].num+t[t[y].ch[0]].siz; if (x<=0) { splay(y,0); return y; } y=t[y].ch[1]; } } }
int pre(int x,int k) { int y=t[x].ch[k]; if (!y) return y; while (t[y].ch[k^1]) y=t[y].ch[k^1]; splay(y,0); return y; }
void ins(int x) { if (!rt) { t[rt=++cnt].val=x; t[cnt].num++; loc[x]=cnt; upd(cnt); return; } find_max(); t[++cnt].val=x; t[cnt].num++; t[cnt].fa=rt; loc[x]=cnt; t[rt].ch[1]=cnt; upd(cnt); upd(rt); splay(cnt,0); } } using namespace Splay;
void work(int x) { if (!x) return; work(t[x].ch[0]); printf("%d ",t[x].val); work(t[x].ch[1]); }
void work1(int x) { if (!x) return; work1(t[x].ch[0]); printf("%d %d ",loc[t[x].val],x); work1(t[x].ch[1]); }
int main() { scanf("%d%d",&n,&m); for (int i=1,x;i<=n;i++) { scanf("%d",&x); ins(x); } string s; for (int i=1,x,tt;i<=m;i++) { cin>>s; scanf("%d",&x); if (s=="Top") { splay(loc[x],0); if (!t[rt].ch[0]) continue; if (!t[rt].ch[1]) { t[rt].ch[1]=t[rt].ch[0]; t[rt].ch[0]=0; continue; } int y=t[rt].ch[1]; while (t[y].ch[0]) y=t[y].ch[0]; t[y].ch[0]=t[rt].ch[0]; if (t[y].ch[0]) t[t[y].ch[0]].fa=y; t[rt].ch[0]=0; upd(y); splay(t[y].ch[0],0); } else if (s=="Bottom") { splay(loc[x],0); if (!t[rt].ch[1]) continue; if (!t[rt].ch[0]) { t[rt].ch[0]=t[rt].ch[1]; t[rt].ch[1]=0; continue; } int y=t[rt].ch[0]; while (t[y].ch[1]) y=t[y].ch[1]; t[y].ch[1]=t[rt].ch[1]; if (t[y].ch[1]) t[t[y].ch[1]].fa=y; t[rt].ch[1]=0; upd(y); splay(t[y].ch[1],0); } else if (s=="Insert") { splay(loc[x],0); scanf("%d",&tt); if (!tt) continue; int t1=pre(loc[x],(tt==-1?0:1)); if (!t1) continue; swap(t[loc[x]].val,t[rt].val); swap(loc[x],loc[t[loc[x]].val]); } else if (s=="Ask") { printf("%d\n",rk(loc[x])); } else if (s=="Query") { printf("%d\n",t[kth(x)].val); } } return 0; }
|