1552: [Cerc2007]robotic sort

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 806  Solved: 329
[Submit][Status][Discuss]

Description

Input

输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000。第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号。

Output

输出共一行,N个用空格隔开的正整数P1,P2,P3…Pn,Pi表示第i次操作前第i小的物品所在的位置。 注意:如果第i次操作前,第i小的物品己经在正确的位置Pi上,我们将区间[Pi,Pi]反转(单个物品)。

Sample Input

6
3 4 5 1 6 2

Sample Output

4 6 4 5 6 6

HINT

Source

HNOI2009集训Day6

Solution

Splay基本操作...

我们首先对给出的序列排序,第一关键字是编号,第二关键字是时间戳,然后用splay去维护这个有顺序的序列就可以了

或者可以考虑维护min和pos

Splay要常写!调的跟*一样慢

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
int x=; char ch=getchar();
while (ch<'' || ch>'') {ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x;
}
#define MAXN 100010
int N,pos[MAXN];
struct Node{int id,t;}a[MAXN];
inline bool cmp(Node A,Node B) {return A.id==B.id? A.t<B.t:A.id<B.id;}
namespace SplayTree
{
int size[MAXN],root,sz,fa[MAXN],son[MAXN][]; bool rev[MAXN];
#define ls(x) son[x][0]
#define rs(x) son[x][1]
inline void Update(int now) {size[now]=size[ls(now)]+size[rs(now)]+;}
inline void PushDown(int now)
{
if (!rev[now]) return;
rev[ls(now)]^=; rev[rs(now)]^=; swap(ls(now),rs(now)); rev[now]=;
}
inline bool Right(int now) {return son[fa[now]][]==now;}
inline void rotate(int now)
{
PushDown(fa[now]); PushDown(now);
int f=fa[now],gf=fa[f],wh=Right(now);
son[f][wh]=son[now][wh^]; fa[son[f][wh]]=f;
fa[f]=now; son[now][wh^]=f; fa[now]=gf;
if (gf) son[gf][son[gf][]==f]=now;
Update(f); Update(now);
}
inline void splay(int now,int tar)
{
for (int f; (f=fa[now])!=tar; rotate(now))
if (fa[f]!=tar) rotate(Right(now)==Right(f)? f:now);
if (!tar) root=now;
}
inline int GetX(int k)
{
int x=root;
while ()
{
PushDown(x);
if (k<=size[ls(x)]) x=ls(x);
else if (k==size[ls(x)]+) return x;
else k-=size[ls(x)]+,x=rs(x);
}
return -;
}
inline int GetK(int x) {splay(x,); return size[ls(x)];}
inline int BuildTree(int l,int r,int last)
{
if (r<l) return ;
int mid=(l+r)>>,now=++sz;
pos[a[mid].id]=now; fa[now]=last;
int lson=BuildTree(l,mid-,now),rson=BuildTree(mid+,r,now);
son[now][]=lson; son[now][]=rson;
Update(now);
return now;
}
inline void Reverse(int l,int r) {splay(l,),splay(r,l); rev[ls(rs(root))]^=;}
inline void Init() {root=BuildTree(,N+,);}
}
int main()
{
N=read();
for (int i=,t=; i<=N+; i++) a[i].id=read(),a[i].t=++t;
a[].id=; a[N+].id=N+;
stable_sort(a+,a+N+,cmp);
for (int i=; i<=N; i++) a[a[i+].t+].id=i;
// for (int i=1; i<=N+2; i++) printf("%d ",a[i].id); puts("");
SplayTree::Init();
for (int i=; i<=N; i++)
{
int loc=SplayTree::GetK(pos[i]);
int x=SplayTree::GetX(i),y=SplayTree::GetX(loc+);
// printf("%d %d\n",x,y);
SplayTree::Reverse(x,y);
printf("%d%c",loc,i!=N? ' ':'\n');
}
return ;
}

和char哥,龙哥,连坐调splay...感觉智障++

【BZOJ-1552&3506】robotic sort&排序机械臂 Splay的更多相关文章

  1. [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap

    robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...

  2. BZOJ 1552: [Cerc2007]robotic sort( splay )

    kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...

  3. bzoj 1552: [Cerc2007]robotic sort

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1198  Solved: 457[Submit] ...

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

    Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...

  5. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  6. 【BZOJ】【1552】【Cerc2007】robotic sort / 【3506】【CQOI2014】排序机械臂

    Splay 离散化+Splay维护序列…… 好吧主要说一下我做这道题遇到的几个错误点: 1.离散化 2.由于找到的这个数的位置一定是大于等于 i 的,所以其实在把它splay到根以后,i 结点只能sp ...

  7. [BZOJ 1552] 排序机械臂

    Splay大法是坠吼滴! 1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 436  Solved: ...

  8. 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)

    点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...

  9. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

随机推荐

  1. Kubernetes deployed on multiple ubuntu nodes

    This document describes how to deploy kubernetes on multiple ubuntu nodes, including 1 master node a ...

  2. 在ESXi 5.x之间冷迁移虚机

    试过几种不同的方法都不成功, 直接用VMware vCenter Converter Standalone Client迁移, 会出现task中的source与job中的source不一致的情况, 使 ...

  3. 【BZOJ 1001】[BeiJing2006]狼抓兔子

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...

  4. 错题724-java

    1.代码片段: byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); ...

  5. 工作随笔——Intellij_idea-14官方快捷键中文版

    听说Intellij Idea好几年了.因为快捷键的原因,所以一直没有放弃eclipse.上周末抽了点时间,用google翻译+自己实践翻译了一下官方的快捷键. 基本做完的时候在百度文库上突然搜索到一 ...

  6. linux上课

    1. service --status-all 2. service sshd restart 3. service --status-all  | grep ssh 4. chkconfig --l ...

  7. sql 几点记录

      1       With子句 1.1     学习目标 掌握with子句用法,并且了解with子句能够提高查询效率的原因. 1.2     With子句要点 with子句的返回结果存到用户的临时表 ...

  8. MPLS

    Multiprotocol Label Switching From Wikipedia, the free encyclopedia "MPLS" redirects here. ...

  9. No goals have been specified for this build

    在pom.xml文件中build后面加上<defaultGoal>compile</defaultGoal>

  10. 各种图(流程图,思维导图,UML,拓扑图,ER图)简介

    来源于:http://www.cnblogs.com/jiqing9006/p/3344221.html 流程图 1.定义:流程图是对过程.算法.流程的一种图像表示,在技术设计.交流及商业简报等领域有 ...