HDU 3436 Queue-jumpers Splay+离散化
2016-07-12 09:32
537 查看
有n个人从小到大排成一列,分别记为1,2...,m次询问,3种操作:
1.把x这个人放到队首。
2.求x这个人在哪个位置。
3.求x这个位置是那个人。
虽然最多有1亿个人,但是操作最多只有10w次,那就离散化,把连续一段没有出现过的数压缩成一个点,然后就是普通的Splay树了。
1.把x这个人放到队首。
2.求x这个人在哪个位置。
3.求x这个位置是那个人。
虽然最多有1亿个人,但是操作最多只有10w次,那就离散化,把连续一段没有出现过的数压缩成一个点,然后就是普通的Splay树了。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 200010; int ch[maxn][2], pre[maxn], sz[maxn], val[maxn], sum[maxn], map[maxn]; int root, top, tol; struct node { int type, x; }q[maxn]; int num[maxn], cnt; int s[maxn], e[maxn]; void NewNode(int &x, int f, int c) { x = ++top; ch[x][0] = ch[x][1] = 0; pre[x] = f; sz[x] = e[c]-s[c]+1; val[x] = c; sum[x] = e[c]-s[c]+1; map[c] = x; } void pushup(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + sum[x]; } void build(int &x, int l, int r, int f) { if(l > r) return; int m = (l + r) >> 1; NewNode(x, f, m); build(ch[x][0], l, m-1, x); build(ch[x][1], m+1, r, x); pushup(x); } void init() { ch[0][0] = ch[0][1] = sum[0] = pre[0] = val[0] = sz[0] = 0; root = top = 0; build(root, 1, tol, 0); } void rotate(int x, int d) { int y = pre[x]; ch[y][d^1] = ch[x][d]; pre[ch[x][d]] = y; if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x; pre[x] = pre[y]; ch[x][d] = y; pre[y] = x; pushup(y); } void Splay(int x, int goal) { 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 d = (ch[z][0] == y); if(ch[y][d] == x) { rotate(x, d^1); rotate(x, d); } else { rotate(y, d); rotate(x, d); } } } pushup(x); if(goal == 0) root = x; } int get_min(int x) { if(!x) return 0; while(ch[x][0]) { x = ch[x][0]; } return x; } int get_max(int x) { if(!x) return 0; while(ch[x][1]) { x = ch[x][1]; } return x; } void insert(int& rt, int f, int k) { if(rt == 0) { NewNode(rt, f, k); return; } insert(ch[rt][0], rt, k); pushup(rt); } int Bin(int x) { int l = 1, r = tol; while(l <= r) { int mid = (l+r) >> 1; if(x >= s[mid] && x <= e[mid]) return mid; if(x > e[mid]) l = mid+1; else r = mid-1; } } void remove(int x) { Splay(x, 0); if(!ch[root][0] && !ch[root][1]) { root = 0; return; } if(!ch[root][0]) { root = ch[root][1]; pre[root] = 0; } else { int m = get_max(ch[root][0]); Splay(m, root); ch[m][1] = ch[root][1]; pre[ch[root][1]] = m; root = m; pre[root] = 0; pushup(root); } } void Top(int x) { int k = Bin(x); int y = map[k]; remove(y); insert(root, 0, k); Splay(top, 0); } int get_kth(int rt, int k) { int t = sz[ch[rt][0]]; if(k <= t) return get_kth(ch[rt][0], k); else if(k <= t+sum[rt]) return s[val[rt]]+k-t-1; else return get_kth(ch[rt][1], k-t-sum[rt]); } int get_rank(int x) { int k = Bin(x); int y = map[k]; Splay(y, 0); //printf("+++++%d %d\n", sz[root], sz[ch[root][0]]); return sz[ch[root][0]]+1; } int main() { int T, cas = 1; scanf("%d", &T); while(T--) { printf("Case %d:\n", cas++); int n, m; scanf("%d %d", &n, &m); cnt = 0; num[++cnt] = 0; for(int i = 0; i < m; i++) { char s[10]; scanf("%s %d", s, &q[i].x); if(s[0] == 'T') { q[i].type = 1; num[++cnt] = q[i].x; } else if(s[0] == 'R') q[i].type = 2; else { q[i].type = 3; num[++cnt] = q[i].x; } } num[++cnt] = n; sort(num+1, num+cnt+1); tol = 0; for(int i = 2; i <= cnt; i++) { if(num[i] != num[i-1]) { if(num[i] - num[i-1] > 1) { tol++; s[tol] = num[i-1]+1; e[tol] = num[i]-1; } tol++; s[tol] = num[i]; e[tol] = num[i]; } } init(); for(int i = 0; i < m; i++) { if(q[i].type == 1) { Top(q[i].x); } else if(q[i].type == 2) { int x = get_kth(root, q[i].x); printf("%d\n", x); } else { int x = get_rank(q[i].x); printf("%d\n", x); } } } return 0; } /* 3 9 5 Rank 3 Top 7 Rank 6 Rank 8 Top 1 */
相关文章推荐
- uiUIlabel 自适应宽度和高度
- require.js配合插件text.js实现最简单的单页应用程序
- leetCode_Unique Binary Search Trees II
- bigbluebutton中文社区 / 开放API / bbb API
- OpenStack Grizzly Install Guide(master Single)
- easyui下拉框动态级联加载
- 第43课:Spark Streaming中UI内幕实现彻底解密
- Closest Binary Search Tree Value
- 修改UITextField的placeholder颜色
- iOS开发UINavigation——导航栏UINavigtionBar
- 设置控件背景透明
- 设置EditText背景图片
- iOS开发UI篇—CAlayer(自定义layer)
- Vue.js学习 Item16 – 各种Web流行MV*框架对比
- Vue.js学习 Item15 – 构建大型web应用
- Vue.js学习 Item14 – 过滤器与自定义过滤器
- Vue.js学习 Item13 – 指令系统与自定义指令
- Vue.js学习 Item13 – 指令系统与自定义指令
- 你的SQL Server碎片真的需要重新构建(Rebuild)或者重新组织(Recognize)吗?
- Xcode7下用xib布局UIScrollView的约束