HDU1890-Robotic Sort-Splay
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; #define Key_value (ch[ch[root][1] ][0]) const int maxn = 2e5+;
const int INF = 0x3f3f3f3f; int pre[maxn],ch[maxn][],key[maxn],size[maxn];
int root,tot1;
int rev[maxn];
int s[maxn],tot2;
int N; struct Node{
int num,id;
bool operator < (const Node &rhs) const
{
if(num == rhs.num) return id < rhs.id;
else return num < rhs.num;
}
}node[maxn]; void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size= %2d val=%2d\n",x,ch[x][],ch[x][],pre[x],size[x],key[x]);
Treavel(ch[x][]);
}
} void debug()
{
printf("root:%d\n",root);
Treavel(root);
} void NewNode(int &o,int father,int k)
{
o = k;
pre[o] = father;
ch[o][] = ch[o][] = ;
rev[o] = ;
size[o] = ;
}
void update_rev(int o)
{
if(!o) return ;
swap(ch[o][],ch[o][]);
rev[o] ^= ;
}
void push_up(int o)
{
int lson = ch[o][],rson = ch[o][];
size[o] = size[lson]+size[rson] + ;
}
void push_down(int o)
{
if(rev[o])
{
update_rev(ch[o][]);
update_rev(ch[o][]);
rev[o] = ;
}
}
void Build(int &x,int l,int r,int father)
{
if(l > r) return ;
int mid = (l+r)>>;
NewNode(x,father,mid);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
void Init()
{
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = size[root] = pre[root] = ;
rev[root] = ;
NewNode(root,,N+);
NewNode(ch[root][],root,N+); Build(Key_value,,N,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind] ] = y;
if(pre[y])
ch[pre[y] ][ch[pre[y]][]==y ] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
void Splay(int x,int goal)
{
push_down(x);
while(pre[x] != goal)
{
if(pre[pre[x] ] == goal)
{
push_down(pre[x]);
push_down(x);
Rotate(x,ch[pre[x]][]==x);
}
else
{
push_down(pre[pre[x] ]);
push_down(pre[x]);
push_down(x);
int y = pre[x];
int kind = ch[pre[y] ][] == y;
if(ch[y][kind] == x)
{
Rotate(x,!kind);
Rotate(x,kind);
}
else
{
Rotate(y,kind);
Rotate(x,kind);
}
}
}
push_up(x);
if(goal == ) root = x;
}
int Get_kth(int x,int k)
{
push_down(x);
int t = size[ch[x][]] + ;
if(t==k) return x;
if(t > k) return Get_kth(ch[x][],k);
else return Get_kth(ch[x][],k-t);
}
int Get_next(int x)
{
push_down(x);
if(ch[x][] == ) return -;
x = ch[x][];
while(ch[x][])
{
x = ch[x][];
push_down(x);
}
return x;
}
int main()
{
while(scanf("%d",&N) && N)
{
for(int i=;i<=N;i++)
{
scanf("%d",&node[i].num);
node[i].id = i;
}
sort(node+,node+N+);
Init();
for(int i=;i<=N;i++)
{
Splay(node[i].id,);
printf("%d%s",size[ch[root][]],i==N?"":" ");
Splay(Get_kth(root,i),);
Splay(Get_next(node[i].id),root);
update_rev(Key_value);
}
printf("\n");
}
}
HDU1890-Robotic Sort-Splay的更多相关文章
- HDU1890 Robotic Sort[splay 序列]
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- hdu1890 Robotic Sort (splay+区间翻转单点更新)
Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...
- HDU 1890 Robotic Sort | Splay
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Pr ...
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- 【BZOJ1552】[Cerc2007]robotic sort Splay
[BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...
- 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值
[bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...
- [BZOJ1552] [Cerc2007] robotic sort (splay)
Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. Output ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
随机推荐
- Linux查看端口
1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 2.netstat -tunlp |grep 端口号 用于查看指定的端口号的进程情况 ...
- Python-正则复习-56
# 正则表达式# 字符组 [字符]# 元字符 # \w \d \s # \W \D \S # . 除了换行符以外的任意字符 # \n \t # \b # ^ $ 匹配字符串的开始和结束 # () 分组 ...
- 二次剩余从csdn
欧拉准则 模\(p\)意义下,\(a\)是二次剩余等价于\(a^{\frac{p-1}{2}}\equiv 1\),\(a\)不是二次剩余等价于\(a^{\frac{p-1}{2}}\equiv -1 ...
- 2017湘潭大学邀请赛G题(贪心+优先队列)
参考博客:http://www.cnblogs.com/chendl111/p/6891770.html 题目链接:https://www.icpc.camp/contests/4mYguiUR8k0 ...
- import导入模块,==和is,浅拷贝和深拷贝,进制转换,位运算,私有化,property装饰器
'''import导入模块'''import sysprint(sys.path) sys.path.append('D://ASoft/Python/PycharmProjects')import ...
- iOS Keychain,SSKeychain,使用 理解 原理
https://www.cnblogs.com/m4abcd/p/5242254.html Keychain 使用? ---为了实用最大化我觉得我应该直接先说使用! 当然是使用第三方库啦:sskeyc ...
- Vmware由于centos升级内核不可运行(C header files matching your running kernel were not found)的解决方案
C header files matching your running kernel were not found. Refer to your distribution's documentati ...
- IdentityServer4【Topic】之StartUp中的配置
Startup 身份服务器是中间件和服务的组合.所有的配置都是在启动类中完成的. Configuring services 通过调用如下代码在DI(dependency inject,依赖注入)中添加 ...
- C#设计模式之3:观察者模式
C#中已经实现了观察者模式,那就是事件,事件封装了委托,使得委托的封装性更好,在类的内部定义事件,然后在客户端对事件进行注册: public class Subject { public event ...
- java不同的包下相同的类名的问题与解决办法
Java中的类以包进行分类组织,当程序中需要用到某个包下的类时,可以以该类的全限定名进行引用.这样,不同的包中的类就可以同名,不会产生混淆. 但是这样就可能导致引用的时候会产生一些问题. 第一个问题, ...