标签: 数据结构
[HDU4441] Queue Sequence
Queue Sequence Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description There’s a queue obeying the first in first out rule. Each time you…
Read More »Splay[转]
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <cstdlib> #include <vector> using namespace std; #define ll ch[x][0] #define rr ch[x][1] #define KT (ch[ch[rt][1]][0]) const int maxn = 300100; int a[maxn], flage[maxn]; int n, m; struct SplayTree{ ///基本数据定义 int ch[maxn][2]; int sz[maxn], pre[maxn]; int rt, tot; ///题意数据定义 int val[maxn], flg[maxn], GCD[maxn][2]; ///Splay树的基本旋转操作函数 void Rotate(int x, int f) { int y = pre[x]; // down(y); down(x);/// ch[y][!f] = ch[x][f]; pre[ch[x][f]] = y; pre[x] = pre[y]; if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x; ch[x][f] = y; pre[y] = x; up(y);/// } void Splay(int x, int goal) { // down(x);/// while (pre[x] != goal) { if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x); else { int y = pre[x], z = pre[y]; int f = ( ch[z][0] == y ); if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f); else Rotate(y, f), Rotate(x, f); } } up(x);/// if (goal == 0) rt = x; } void RTO(int k, int goal) { int x = rt; while (sz[ll] + 1 != k) { if (k < sz[ll] + 1) x = ll; else { k -= (sz[ll] + 1); x = rr; } } Splay(x, goal); } ///Splay树的生成函数 void newnode(int &x, int c, int state, int f)/// { x = ++tot; ll = rr = 0; sz[x] = 1; pre[x] = f; val[x] = c; flg[x] = state; GCD[x][state] = c; GCD[x][!state] = -1; } void build(int &x, int l, int r, int f)/// { if (l > r) return ; int m = (l + r) >> 1; newnode(x, a[m], flage[m], f); build(ll, l, m - 1, x); build(rr, m + 1, r, x); up(x); } void Init(int n) { ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0; GCD[0][0] = GCD[0][1] = -1; flg[0] = val[0] = 0;///???? rt = tot = 0; newnode(rt, -1, 0, 0); newnode(ch[rt][1], -1, 0, rt); build(KT, 1, n, ch[rt][1]);/// up(ch[rt][1]); up(rt);/// } ///区间操作相关up(), down() void up(int x)///!!! { sz[x] = sz[ll] + sz[rr] + 1;///!!! GCD[x][flg[x]] = val[x];///!!!! GCD[x][!flg[x]] = -1; for (int i = 0; i < 2; i++) { if (GCD[ll][i] != -1) { if (GCD[x][i] != -1) GCD[x][i] = __gcd(GCD[x][i], GCD[ll][i]); else GCD[x][i] = GCD[ll][i]; } if (GCD[rr][i] != -1) { if (GCD[x][i] != -1) GCD[x][i] = __gcd(GCD[x][i], GCD[rr][i]); else GCD[x][i] = GCD[rr][i]; } } } ///区间查询 void solve_Q(int L, int R, int state) { RTO(L, 0); RTO(R + 2, rt); printf("%d\n", GCD[KT][state]); } ///插入一个节点 void solve_I(int L, int value, int state) { RTO(L + 1, 0); RTO(L + 2, rt); int x; newnode(x, value, state, ch[rt][1]);///!!! KT = x; up(ch[rt][1]); up(rt); } ///删除一个节点 void solve_D(int L) { RTO(L, 0); RTO(L + 2, rt); KT = 0; up(ch[rt][1]); up(rt); } ///更改节点信息 void solve_R(int L) { RTO(L + 1, 0); flg[rt] ^= 1; up(rt); } void solve_M(int L, int value) { RTO(L + 1, 0); val[rt] = value; up(rt); } ///题意相关函数 void solve(int m) { char op; int L, R, state, value; while (m--) { scanf(" %c", &op); if (op == 'Q') { scanf("%d%d%d", &L, &R, &state); solve_Q(L, R, state); } else if (op == 'I') { scanf("%d%d%d", &L, &value, &state); solve_I(L, value, state); } else if (op == 'D') { scanf("%d", &L); solve_D(L); } else if (op == 'R') { scanf("%d", &L); solve_R(L); } else if (op == 'M') { scanf("%d%d", &L, &value); solve_M(L, value); } } } }sp; int main() { while (cin >> n >> m) { for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &flage[i]); sp.Init(n); sp.solve(m); } } |
感觉很不错的样子,转过来看看。
Read More »splay?
1 |
可能我写了假的splay<img src="http://zhuyeye.cn/wp-content/plugins/kindeditor-for-wordpress/plugins/emoticons/images/17.gif" alt="" border="0" /> |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=10+2; int f[maxn],ch[maxn][2],key[maxn],cnt[maxn],Size[maxn]; int root,sz; void Clear(int x) { ch[x][0]=ch[x][1]=f[x]=key[x]=cnt[x]=Size[x]=0; return; } int get(int x) { return ch[f[x]][1]==x; } void update(int x) { if (x) { Size[x]=cnt[x]; if (ch[x][0]) Size[x]+=Size[ch[x][0]]; if (ch[x][1]) Size[x]+=Size[ch[x][1]]; } } void Rotate(int x) { int old=f[x],oldf=f[old],which=get(x); ch[old][which]=ch[x][which^1]; f[ch[old][which]]=old; f[0]=0; f[old]=x; ch[x][which^1]=old; f[x]=oldf; if (oldf) { ch[oldf][ch[oldf][1]==old]=x; } update(old); update(x); } void splay(int x) { for (int fa;(fa=f[x]);Rotate(x)) { if (f[fa]) { Rotate((get(x)==get(fa))?fa:x); } } root=x; } void Insert(int v) { if (root==0) { sz++; ch[sz][0]=ch[sz][1]=f[sz]=0; cnt[sz]=1; key[sz]=v; Size[sz]=1; root=sz; return; } int now=root,fa=0; while (true) { if (key[now]==v) { cnt[now]++; update(now); update(fa); splay(now); break; } fa=now; now=ch[now][key[now]<v]; if (now==0) { sz++; key[sz]=v; cnt[sz]=Size[sz]=1; ch[sz][0]=ch[sz][1]=0; f[sz]=fa; ch[fa][key[fa]<v]=sz; update(fa); splay(sz); break; } } } int Find(int v) { int ans=0,now=root; while(true) { if (v<key[now]) { now=ch[now][0]; } else { ans+=(ch[now][0]?Size[ch[now][0]]:0); if (key[now]==v) { splay(now); return ans+1; } ans+=cnt[now]; now=ch[now][1]; } if (now==0) return -1; } } int findx(int x) { if (x>sz) return -1; int now=root; while (true) { if (ch[now][0]&&Size[ch[now][0]]>=x) { now=ch[now][0]; } else { int temp=(ch[now][0]?Size[ch[now][0]]:0)+cnt[now]; if (x<=temp) return key[now]; x-=temp;now=ch[now][1]; } } } int pre() { int now =ch[root][0]; while(ch[now][1]) now=ch[now][1]; return now; } int next() { int now=ch[root][1]; while(ch[now][0]) now=ch[now][0]; return now; } void del(int x){ int whatever=Find(x); if (cnt[root]>1) {cnt[root]–;return;} //Only One Point if (!ch[root][0]&&!ch[root][1]) {Clear(root);root=0;return;} //Only One Child if (!ch[root][0]){ int oldroot=root;root=ch[root][1];f[root]=0;Clear(oldroot);return; } else if (!ch[root][1]){ int oldroot=root;root=ch[root][0];f[root]=0;Clear(oldroot);return; } //Two Children int leftbig=pre(),oldroot=root; splay(leftbig); f[ch[oldroot][1]]=root; ch[root][1]=ch[oldroot][1]; Clear(oldroot); update(root); return; } int main() { int n; root=0; sz=0; scanf(“%d”,&n); for (int i=0;i<n;i++) { int opt,x; scanf(“%d%d”,&opt,&x); switch (opt) { case 1: { Insert(x); break; } case 2:{ del(x); break; } case 3:{ printf(“%dn”,Find(x)); break; } case 4:{ printf(“%dn”,findx(x)); break; } case 5:{ Insert(x); printf(“%dn”,key[pre()]); del(x); break; } case 6:{ Insert(x); printf(“%dn”,key[next()]); del(x); break; } } } return 0; } |