原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890

如下:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using std::sort;
using std::swap;
const int Max_N = ;
struct node{
int val, pos;
}rec[];
inline bool cmp(const node &a, const node &b){
if (a.val == b.val) return a.pos < b.pos;
return a.val < b.val;
}
struct Node{
int v, s, rsz, rev;
Node *pre, *ch[];
inline void set(int _v = -, int _s = , Node *p = NULL){
v = _v, s = _s, rev = rsz = ;
pre = ch[] = ch[] = p;
}
inline void update(){
rev ^= ;
swap(ch[], ch[]);
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
rsz = ch[]->rsz + ch[]->rsz;
if (v != -) rsz++;
}
inline void push_down(){
if (rev != ){
rev ^= ;
ch[]->update();
ch[]->update();
}
}
};
struct SplayTree{
int top = ;
Node *tail, *root, *null;
Node stack[Max_N], *store[Max_N], *ptr[Max_N];
void init(int n){
top = ;
tail = &stack[];
null = tail++;
null->set();
root = newNode(-);
root->ch[] = newNode(-);
root->ch[]->pre = root;
Node *x = built(, n);
root->ch[]->ch[] = x;
x->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline Node *built(int l, int r){
Node *p = null;
if (l > r) return null;
int mid = (l + r) >> ;
p = newNode(rec[mid].val), ptr[mid] = p;
p->ch[] = built(l, mid - );
if (p->ch[] != null) p->ch[]->pre = p;
p->ch[] = built(mid + , r);
if (p->ch[] != null) p->ch[]->pre = p;
p->push_up();
return p;
}
inline Node *newNode(int v){
Node *p = null;
if (!top) p = tail++;
else p = store[--top];
p->set(v, , null);
return p;
}
inline void rotate(Node *x, int d){
Node *y = x->pre;
y->push_down(), x->push_down();
y->ch[!d] = x->ch[d];
if (x->ch[d] != null) x->ch[d]->pre = y;
x->pre = y->pre;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[d] = y;
y->pre = x;
y->push_up();
if (y == root) root = x;
}
inline void splay(Node *x, Node *f){
for (; x->pre != f; x->push_down()){
if (x->pre->pre == f){
rotate(x, x->pre->ch[] == x);
} else {
Node *y = x->pre, *z = y->pre;
if (z->ch[] == y){
if (y->ch[] == x) rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
} else {
if (y->ch[] == x) rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
}
}
}
x->push_up();
}
inline Node *select(Node *x, int k){
int t = ;
for (;;){
x->push_down();
t = x->ch[]->s;
if (k == t + ) break;
else if (k < t + ) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline void del(){
Node *x = root;
store[top++] = x;
root = root->ch[];
root->pre = null;
splay(select(root, ), null);
root->ch[] = x->ch[];
root->ch[]->pre = root;
root->push_up();
}
inline void solve(int n) {
const int N = n + ;
for (int i = ; i <= n; ++i) {
scanf("%d", &rec[i].val);
rec[i].pos = i;
}
init(n), sort(rec + , rec + n + , cmp);
splay(root->ch[], null);
for (int i = ; i <= n; i++){
splay(ptr[rec[i].pos], null);
root->ch[]->update();
printf("%d", root->ch[]->rsz + i);
if (i != n) printf(" ");
del();
}
printf("\n");
}
}spt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n) && n) {
spt.solve(n);
}
return ;
}

hdu 1890 Robotic Sort的更多相关文章

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

    题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...

  2. 数据结构(Splay平衡树):HDU 1890 Robotic Sort

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. HDU 1890 Robotic Sort | Splay

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Pr ...

  4. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. HDU 1890 Robotic Sort(splay)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...

  6. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

  7. hdu 1890 Robotic SortI(splay区间旋转操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...

  8. 【HDOJ】1890 Robotic Sort

    伸展树伤不起啊,很容易wa,很容易T,很容易M. /* 1890 */ #include <iostream> #include <string> #include <m ...

  9. HDU 1890 区间反转

    http://acm.hdu.edu.cn/showproblem.php?pid=1890 Robotic Sort Problem Description Somewhere deep in th ...

随机推荐

  1. 全局变量&局部变量

    一.局部变量: 定义在函数内部的变量以及函数的形参成为局部变量 作用于:从定义那一行开始知道与其所在的代码块结束 生命周期:从程序运行到定义那一行开始分配存储空间到程序离开该变量所在的作用域 特点: ...

  2. oracle限制用户连接数

    查看是否启用限制配置 SQL> show parameter resource_limit; 或者 select * from v$parameterwhere name = 'resource ...

  3. AX2012全新的批处理方式

    AX2009 的批处理操作我们是通过RunBaseBatch framework,我们所要做的事情就是继承RunBaseBatch class,实现里面该实现的方法来执行批处理. AX2012 的批处 ...

  4. Android基础总结(6)——内容提供器

    前面学习的数据持久化技术包括文件存储.SharedPreferences存储以及数据库存储技术保存的数据都只能被当前应用程序所访问.虽然文件存储和SharedPreferences存储中提供了MODE ...

  5. SVN 外部引用(svn:externals)处理相似系统的公用代码

    一.创建外部引用 我们常常遇到这样一个场景,我们有两个系统,两个系统用的是同一套框架.如果我们用两套程序 去做,当我们修改这个公共的框架的时候,另外一个还是旧版本的,很容易造成混乱. SVN的外部用就 ...

  6. 百度编辑器Ueditor自动换行,添加<p>的问题

    百度编辑器Ueditor其实蛮好用的,后来使用了一段时间发现,每次打开后又保存,发现都会往内容的前后都增加一个空白的<p></p>.刚开始以后是百度编辑器的问题,找了很长时间也 ...

  7. WWF3XOML方式创建和启动工作流 <第十篇>

    一.XOML使用工作流的好处 通过Xoml方式使用工作流的好处在于,它能够不重新启动程序的情况下,仅仅通过配置xoml就能够实现改变工作流,非常灵活. 创建一个WinForm程序如下: 代码如下: n ...

  8. Glassfish数据源配置详解

    本文环境: Win2003 + myeclipse6.01 + sqlserver2000(sp4) 1. 安装glassfish——启动,在管理控制台下配置如下 首先配置连接池——大家把sqlser ...

  9. s3c6410_uart初始化及读写

    参考: 1)<USER'S MANUAL-S3C6410X>第31章 UART 2)u-boot uart初始化及读写:u-boot-x.x.x/board/samsumg/smdk641 ...

  10. Mysql的cmake编译与安装

    Mysql的cmake编译与安装 实验准备环境: 我的操作系统是centos6.6 编译安装MariaDB之前,我们需要准备一些需要的环境 1.开发包组套件 [root@node19 ~]# yum ...