Rokua P3165 [CQOI2014] Sort Picker "splay"

Source: Internet
Author: User

Title Description
In order to rank items from low to high in the factory, engineers invented a sort of robotic arm. It follows a simple collation, where the first operation finds the position of the lowest-height item $p _1$, and $p_1$ the item from the first item on the left to the object between the $[1,p_1]$ (that is, the item between the intervals), and second finds the position of the second-lowest item $p_2$, and puts the second from the left to the $p_2$ (i.e., the items between the interval $[2,p_2]$) are reversed ... Eventually all items will be sorted.

Give an example of six items, before the first operation, the height of the lowest item in position 4, so the first to the fourth of the object is reversed; before the second operation, the second low item in the reign of Move place six, so the second to sixth item reverse order ...

Your task is to write a program that determines the sequence of operations, that is, the position of the item $p_i$ low before each operation $p_i$, so that the manipulator can work. It is important to note that if you have items of the same height, you must ensure that the relative position of the order is the same as the original.

Input Format:

The first line contains the positive integer n, which indicates the number of items that need to be sorted.

The second line contains n space-delimited integer AI, which represents the height of each item.

output Format:

The output line contains n spaces separated by an integer pi.

Input Sample

6
3 4 5 1 6 2

Output Sample

4 6 4 5 6 6

Description

n<=100000
Pi<=10^7
************************

Problem Analysis

Comparison of conventional splay interval rollover problems
Join Sentinel Node Achievements
Height sort after query rank and flip

There are two points to note
1. each time you have to rotate the element to the root to get his rank in the reversal interval
So in this splay, you have to push down the inside .
and z, y, X are going to push.

2. The same order of objects with the same height
In the read -in to the height and number of the structure and double-keyword sorting
If you are directly to the height sort, there may be instability
Handwriting Sort Please ignore

#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <    Cstring> #include <cstdio>using namespace Std;int read () {int f=1,x=0;    Char Ss=getchar (); while (ss< ' 0 ' | |    Ss> ' 9 ') {if (ss== '-') F=-1;ss=getchar ();}    while (ss>= ' 0 ' &&ss<= ' 9 ') {x=x*10+ss-' 0 '; Ss=getchar ();} return f*x;} const int Maxn=200010;int n;struct node{int v,pos;} A[maxn];int ch[maxn][2],fa[maxn];int size[maxn],sz,rt;int Lzy[maxn];bool cmp (node A,node b) {if (A.V==B.V) return a.pos&    Lt;b.pos; return A.V&LT;B.V;} void Update (int p) {size[p]=size[ch[p][0]]+size[ch[p][1]]+1;}        inline void push (int p) {if (lzy[p]) {swap (ch[p][0],ch[p][1]); Lzy[ch[p][0]]^=1;        Lzy[ch[p][1]]^=1;    lzy[p]=0;    }}void rotate (int &p,int x) {int y=fa[x],z=fa[y];    int t= (CH[Y][0]==X);    if (y==p) p=x;    else if (ch[z][0]==y) ch[z][0]=x;    else ch[z][1]=x; Fa[y]=x; Fa[ch[x][t]]=y;    Fa[x]=z; CH[Y][T^1]=CH[X][T];    Ch[x][t]=y; Update (Y); Update (x);}        void splay (int &p,int x) {while (x!=p) {int y=fa[x],z=fa[y];        Push (z);p ush (y);p ush (x);            if (y!=p) {if ((ch[z][0]==y) ^ (ch[y][0]==x)) rotate (p,x);        else rotate (p,y);    } rotate (p,x);    }}void Build (int p,int Ll,int RR) {if (LL&GT;RR) return;    int mid=ll+rr>>1; Fa[mid]=p;    Size[mid]=1;    ch[p][mid>p]=mid;    if (LL==RR) return;    Build (mid,ll,mid-1); build (MID,MID+1,RR); Update (mid);      }int Find (int p,int k) {push (P);     int ss=size[ch[p][0]];        if (k==ss+1) return p;    if (K&LT;=SS) return find (CH[P][0],K);  else return find (ch[p][1],k-ss-1);    } void Rev (int ll,int rr) {int x=find (RT,LL-1), Y=find (rt,rr+1);    Splay (RT,X);     Splay (Ch[rt][1],y); Lzy[ch[y][0]]^=1;}         void Solve () {for (int i=2;i<=n;++i) {int x=a[i].pos;        Splay (RT,X);        printf ("%d", size[ch[rt][0]]);        int ll=i-1,rr=size[ch[rt][0]];    Rev (Ll+1,rr+1); } printf ("%d",n);//The last element's rank must be N, so do not query}int main () {n=read ();    for (int i=2;i<=n+1;++i) {a[i].v=read (); a[i].pos=i;} A[1].v=-1e8; a[1].pos=1;//read into the altitude information a[n+2].v=1e8;        a[n+2].pos=n+2;    rt=n+3>>1;        Build (rt,1,n+2);    Sort (a+1,a+n+3,cmp);//double-keyword sorting solve (); return 0;}

Rokua P3165 [CQOI2014] Sort Picker "splay"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.