# hdu1890 Robotic Sort (splay+区间翻转单点更新)

Problem Description
Somewhere deep in the Czech Technical University buildings, there are laboratories for examining mechanical and electrical properties of various materials. In one of yesterday’s presentations, you have seen how was one of the laboratories changed into a new multimedia lab. But there are still others, serving to their original purposes.

In this task, you are to write software for a robot that handles samples in such a laboratory. Imagine there are material samples lined up on a running belt. The samples have different heights, which may cause troubles to the next processing unit. To eliminate such troubles, we need to sort the samples by their height into the ascending order.

Reordering is done by a mechanical robot arm, which is able to pick up any number of consecutive samples and turn them round, such that their mutual order is reversed. In other words, one robot operation can reverse the order of samples on positions between A and B.

A possible way to sort the samples is to find the position of the smallest one (P1) and reverse the order between positions 1 and P1, which causes the smallest sample to become first. Then we find the second one on position P and reverse the order between 2 and P2. Then the third sample is located etc. The picture shows a simple example of 6 samples. The smallest one is on the 4th position, therefore, the robot arm reverses the first 4 samples. The second smallest sample is the last one, so the next robot operation will reverse the order of five samples on positions 2–6. The third step will be to reverse the samples 3–4, etc.

Your task is to find the correct sequence of reversal operations that will sort the samples using the above algorithm. If there are more samples with the same height, their mutual order must be preserved: the one that was given first in the initial order must be placed before the others in the final order too.

Input
The input consists of several scenarios. Each scenario is described by two lines. The first line contains one integer number N , the number of samples, 1 ≤ N ≤ 100 000. The second line lists exactly N space-separated positive integers, they specify the heights of individual samples and their initial order.

The last scenario is followed by a line containing zero.

Output
For each scenario, output one line with exactly N integers P1 , P1 , . . . PN ,separated by a space.
Each Pi must be an integer (1 ≤ Pi ≤ N ) giving the position of the i-th sample just before the i-th reversal operation.

Note that if a sample is already on its correct position Pi , you should output the number Pi anyway, indicating that the “interval between Pi and Pi ” (a single sample) should be reversed.

Sample Input
6 3 4 5 1 6 2 4 3 3 2 1 0

Sample Output
4 6 4 5 6 6

4 2 4 4

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lson th<<1
#define rson th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100050
#define Key_value ch[ch[root]]
int n;
struct edge{
int idx,num;
}a[maxn];
int mp[maxn],mp1[maxn];
bool cmp(edge a,edge b){
if(a.num==b.num)return a.idx<b.idx;
return a.num<b.num;
}

int cnt,rt;
int pre[maxn],ch[maxn],sz[maxn],rev[maxn];

void newnode(int &x,int father)
{
x=++cnt;
pre[x]=father;ch[x]=ch[x]=0;sz[x]=1;rev[x]=0;
}
void update_rev(int x)
{
if(x==0)return;       //!!!
rev[x]^=1;
swap(ch[x],ch[x]);
}
void pushdown(int x)
{
int y;
if(rev[x]){
update_rev(ch[x]);
update_rev(ch[x]);
rev[x]=0;
}
}
void pushup(int x)
{
sz[x]=sz[ch[x] ]+sz[ch[x] ]+1;
}

void build(int &x,int l,int r,int father)
{
if(l>r)return;
int mid=(l+r)/2;
newnode(x,father);mp1[mp[mid] ]=cnt;
build(ch[x],l,mid-1,x);
build(ch[x],mid+1,r,x);
pushup(x);
}

void init()
{
cnt=rt=0;
pre[rt]=ch[rt]=ch[rt]=sz[rt]=rev[rt]=0;
build(rt,1,n,0);
}

void rotate(int x,int p)
{
int y=pre[x];
pushdown(y);pushdown(x);
ch[y][!p]=ch[x][p];
pre[ch[x][p] ]=y;
if(pre[y])ch[pre[y] ][ch[pre[y] ]==y ]=x;
pre[x]=pre[y];
ch[x][p]=y;
pre[y]=x;
pushup(y);pushup(x);
}
void splay(int x,int goal)
{
pushdown(x);
while(pre[x]!=goal){
if(pre[pre[x] ]==goal){
pushdown(pre[x]);pushdown(x);
rotate(x,ch[pre[x]]==x);
}
else{
int y=pre[x];int z=pre[y];
pushdown(z);pushdown(y);pushdown(x);
int p=ch[pre[y] ]==y;
if(ch[y][p]==x )rotate(x,!p);
else rotate(y,p);
rotate(x,p);
}
}
if(goal==0)rt=x;
pushup(x);
}
void del()
{
if(ch[rt]==0 ){
rt=ch[rt];
pre[rt]=0;
}
else{
int y=ch[rt];
int x=ch[rt];
pushdown(y);
while(ch[y]){
y=ch[y];pushdown(y);
}
splay(y,rt);
ch[y]=x;
pre[x]=y;
rt=y;
pre[rt]=0;
pushup(rt);
}
}

int main()
{
int m,i,j;
while(scanf("%d",&n)!=EOF && n!=0)
{
for(i=1;i<=n;i++){
scanf("%d",&a[i].num);
a[i].idx=i;
}
sort(a+1,a+1+n,cmp);
for(i=1;i<=n;i++)mp[a[i].idx ]=i;

init();
for(i=1;i<n;i++){
splay(mp1[i],0);
update_rev(ch[rt]);
printf("%d ",i+sz[ch[rt]]);
del();
}
printf("%d\n",n);
}
return 0;
} 07-14 386

#### HDU 1890 Robotic Sort [平衡树splay 区间翻转]

12-29 1104

#### [hdu1890 Robotic Sort]Splay Tree

09-27 292

#### hdu 1890 Robotic Sort （Splay区间翻转）

08-24 397

#### Hdu 1890 Robotic Sort(Spaly 区间翻转)

01-14 27

#### hdu 1890 Robotic Sort(splay 区间反转+删点)

07-02 782

#### Splay树（区间翻转）—— HDU 1890 Robotic Sort

08-09 12

#### HDU 1890 Robotic Sort (Splay 区间翻转)

07-17 1685

#### HDU 1890 Robotic Sort

07-28 45

#### hdu 1890 Robotic Sort （splay树 翻转 +删点）

02-07 14

#### HDU 1890 Robotic Sort Splay

02-20 9

#### 最新微信小程序源码

04-26 805

#### 微信小程序源码-合集1

08-21 1424

#### hdu 1890 Robotic Sort (splay tree伸展树)

05-05 1220

#### HDU 1890 Robotic Sort 单点查找区间翻转 Splay裸题

04-22 877

#### HDU1890 Robotic Sort SPLAY

03-19 82万+

#### 大学四年自学走来，这些私藏的实用工具/学习网站我贡献出来了

04-14 60万+

#### 在中国程序员是青春饭吗？

02-28 9167

#### springboot+jwt实现token登陆权限认证

03-01 14万+

#### 技术大佬：我去，你写的 switch 语句也太老土了吧

03-08 7万+

#### 女程序员，为什么比男程序员少？？？

04-25 7万+

#### 总结了 150 余个神奇网站，你不来瞅瞅吗？

03-10 13万+

#### 副业收入是我做程序媛的3倍，工作外的B面人生是怎样的？

03-10 19万+

#### MySQL数据库面试题（2020最新版）

03-12 11万+

#### 如果你是老板，你会不会踢了这样的员工？

03-13 12万+

#### 我入职阿里后，才知道原来简历这么写

03-13 1万+

#### 程序员写出这样的代码，能不挨骂吗？

03-14 1万+

#### 外包程序员的幸福生活 ©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客 点击重新获取   扫码支付