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 ...
随机推荐
- Python爬取豆瓣指定书籍的短评
Python爬取豆瓣指定书籍的短评 #!/usr/bin/python # coding=utf-8 import re import sys import time import random im ...
- 我去年码了个表(WPF MvvM)
又快个把月没写博客了(最近忙着学JAVA去了,都是被逼的/(ㄒoㄒ)/~~),然后用WPF码了个表,其实想加上那种提醒功能什么的,额,就这样了吧,主要是感受一下数据驱动的思想. 效果如下: 前端XAM ...
- NewZealand。。。
秀个存在感...
- 自定义分页及Cookie、Session机制
分页 自定义分页 data = [] , ): tmp = {"id": i, "name": "alex-{}".format(i)} d ...
- Krpano教程tour.xml详解
<krpano version="1.18" //版本号 onstart="" //网页启动时调用的函数 basedir="%FIRSTXML% ...
- D1. Great Vova Wall (Version 1)
链接 [https://codeforces.com/contest/1092/problem/D1] 题意 给你n个位置墙的高度,现在你有2×1 砖块,你可以竖直或者水平放置 问你是否可以使得所有位 ...
- 亲测可以永久破解2018版本的pycharm
pycharm是很强大的开发工具,但是每次注册着实让人头疼.网络上很多注册码.注册服务器等等.但都只是一年或者不能用:为次有如下解决方案.亲测有效!!! 如果想让pycharm永久被激活,比如截止日到 ...
- p9半幺群
如何不理解划红线的地方?第二个划红线地方,请举一个例子 1.0不是幺元 2.f(1)=2, f(2)=1, f(3)=3, g(1)=2, g(2)=3, g(3)=1 fg不等于gf
- C#设计模式之2:单例模式
在程序的设计过程中很多时候系统会要求对于某个类型在一个应用程序域中只出现一次,或者是因为性能的考虑,或者是由于逻辑的要求,总之是有这样的需求的存在,那在设计模式中正好有这么一种模式可以来满足这样的要求 ...
- gulp项目和webpack项目在浏览器中查看的方式
在存在.git的目录下,按住shift+左键,打开命令行或者使用git Bash Gulp: 输入gulp dev 本地起一个服务器,在项目中找到gulp.js,然后找本地服务器,找到host和por ...