原题链接: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-5 如何命名Java变量

    来源:http://www.imooc.com/code/1221 如同酒店会给每个房间起个性化的名字一样,程序中的变量也需要用合理的名字进行管理---变量名! 需要注意,给酒店房间起名字时可以是数字 ...

  2. PMP考试--价值工程法

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 ValueEngineering,简称VE,是降低成本提高经济效益的有效方法,价值工 ...

  3. Swift一些语法

    1.函数权限 public : 最大权限, 可以在当前framework和其他framework中访问internal : 默认的权限, 可以在当前framework中随意访问private : 私有 ...

  4. matlab读取txt文档中的数据

    ps:文件中只有数字! format long fp=fopen('文件路径','打开方式(r)') [num,count]=fscnaf(fp,'%f')

  5. 在 mvc 中使用下拉列表

    在mvc中经常会使用到下拉列表,以下以两种方式来实现,一种是以  @Html.DropDownList 扩展方法,一种是以 <select><option></optio ...

  6. 学习联系 Java阶乘相关练习

    题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度 double hou = 0.00008; for (int i = 1; i > 0; i++) { hou = ...

  7. CSS3 圆形时钟式网页进度条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 读取Jar包中的资源问题探究

    最近在写一个可执行jar的程序,程序中包含了2个资源包,一个是images,一个是files.问题来了,在Eclipse里开发的时候,当用File类来获取files下面的文件时,没有任何问题.但是当程 ...

  9. ngrok逆向代理服务器搭建微信公众号本地开发环境

    一条命令解决的外网访问内网问题 本地WEB外网访问.本地开发微信.TCP端口转发 平台登陆地址:http://www.ngrok.cc/login 新版本上线启动方式更简单使用视频教程 在路由器上面的 ...

  10. Ubuntu 14.04 – How to install xrdp in Ubuntu 14.04

    http://c-nergy.be/blog/?p=5305 Hello World, Ubuntu 14.04 has been released on April 17th 2014 and we ...