您的位置:首页 > 产品设计 > UI/UE

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树了。
#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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: