hdu 1890 Robotic Sort
原题链接: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的更多相关文章
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- 数据结构(Splay平衡树):HDU 1890 Robotic Sort
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort | Splay
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Pr ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
- 【HDOJ】1890 Robotic Sort
伸展树伤不起啊,很容易wa,很容易T,很容易M. /* 1890 */ #include <iostream> #include <string> #include <m ...
- HDU 1890 区间反转
http://acm.hdu.edu.cn/showproblem.php?pid=1890 Robotic Sort Problem Description Somewhere deep in th ...
随机推荐
- 【练习】显示MySQLadmin 库户籍选项
[oracle@enmo ~]$ mysqladmin -V mysqladmin Ver , for Linux on x86_64
- 学习记录 java泛型资料
java泛型资料: 1. 概述在引入范型之前,Java类型分为原始类型.复杂类型,其中复杂类型分为数组和类.引入范型后,一个复杂类型就可以在细分成更多的类型.例如原先的类型List,现在在细分成Lis ...
- XML Namespace 命名空间
根据 Namespaces in XML W3C 推荐标准的定义,XML 命名空间 是由国际化资源标识符 (IRI) 标识的 XML 元素和属性集合:该集合通常称作 XML“词汇”. 定义 XML 命 ...
- MySQL学习笔记(二)
二.SQL基本知识 SQL 是一种典型的非过程化程序设计语言,这种语言的特点是:只指定哪些数据被操纵,至于对这些数据要执行哪些操作,以及这些操作是如何执行的,则未被指定.非过程化程序设计语言的优点在于 ...
- java学习之(接口)
使用接口 接口不能用于创建实例,但接口可以用于声明引用类型变量.当使用接口来声明引用类型变量时,这个引用类型变量必须引用到其实现类的对象.除此之外,接口的主要用途就是被实现类实现.归纳起来,接口主要有 ...
- 从零开始安装Hadoop视频教程
从零开始安装Hadoop视频教程 Hadoop 是一个能够对大量数据进行分布式处理的软件框架,用这种技术使得普通的PC服务器甚至一些近过时的服务器也能够发挥余热,组成大型集群系统,由于它的可伸缩性能够 ...
- yiStack平台维护
<一.> yiStack 二次封装 Iso通用发行版相关配置属性值替换1.1.yiStack AIO一体机环境根据部署子网替换修改示例如下:主要修改替换isolinux/ks-aio.cf ...
- 分区的4k对齐
4k对齐的原理 4k对齐的磁盘性能比非对齐的大致提升在5%-10%左右. fdisk -H 224 -S 56 /dev/sdx #创建分区 fdisk -lu /dev/sdx #验证对齐
- WWF3动态修改工作流<第九篇>
一.动态添加或移除工作流活动 首先添加一个顺序的空白工作流. 然后添加一个Winform程序,界面如下: 代码如下: namespace WinForm { public partial class ...
- linux .net mono方案测试记录与报告(一)
第一阶段 linux .net 方案测试 硬件为4核8线程的笔记本i7-4710mq 分配了4个线程 情况下 1.方案一 nginx+fastcgi-mono-server4方式 性能为每秒处理140 ...