[BZOJ3506] [Cqoi2014] 排序机械臂 (splay)

Description

  同OJ1552

Input

Output

Sample Input

Sample Output

HINT

Source

Solution

  Q:哎不是同一道题吗为什么分两篇博客来写?

  A:当然是来骗浏览量了233333333,这里还是会放代码的,当然你左转BZOJ1552的题解也行。

 #include <bits/stdc++.h>
using namespace std;
struct spaly
{
int c[], fa, siz, rev;
}a[];
pair<int, int> b[]; void push_up(int k)
{
a[k].siz = a[a[k].c[]].siz + a[a[k].c[]].siz + ;
} void push_down(int k)
{
if(a[k].rev)
{
swap(a[k].c[], a[k].c[]), a[k].rev = ;
a[a[k].c[]].rev ^= , a[a[k].c[]].rev ^= ;
}
} void rotate(int &k, int x)
{
int y = a[x].fa, z = a[y].fa;
int dy = a[y].c[] == x, dz = a[z].c[] == y;
if(k == y) k = x, a[x].fa = z;
else a[z].c[dz] = x, a[x].fa = z;
a[y].c[dy] = a[x].c[!dy], a[a[x].c[!dy]].fa = y;
a[x].c[!dy] = y, a[y].fa = x;
push_up(y);
} void splay(int &k, int x)
{
while(k != x)
{
int y = a[x].fa, z = a[y].fa;
push_down(z), push_down(y), push_down(x);
if(k != y)
if(a[y].c[] == x ^ a[z].c[] == y) rotate(k, x);
else rotate(k, y);
rotate(k, x);
}
push_up(x);
} int find(int k, int x)
{
if(!k) return ;
push_down(k);
if(x <= a[a[k].c[]].siz) return find(a[k].c[], x);
if(x == a[a[k].c[]].siz + ) return k;
return find(a[k].c[], x - a[a[k].c[]].siz - );
} int main()
{
int n, root, pos;
while(~scanf("%d", &n) && n)
{
for(int i = ; i <= n; i++)
{
scanf("%d", &b[i].first);
b[i].second = i + ;
}
sort(b + , b + n + );
for(int i = ; i <= n + ; i++)
{
a[i].fa = i + , a[i].c[] = i - ;
a[i].siz = i, a[i].c[] = a[i].rev = ;
}
a[n + ].fa = , root = n + ;
for(int i = ; i <= n; i++)
{
splay(root, b[i].second);
pos = a[a[root].c[]].siz;
printf("%d", pos);
if(i != n) printf(" ");
splay(root, find(root, i));
splay(a[root].c[], find(root, pos + ));
a[a[a[root].c[]].c[]].rev ^= ;
}
puts("");
}
return ;
}
上一篇:bzoj 1552: [Cerc2007]robotic sort


下一篇:[BZOJ 1552] 排序机械臂