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 ...
随机推荐
- 【程序与资源】linux程序与资源管理
程序与资源管理:ps.top.free.sar.kill.uname ①ps语法: [root @test /root ]# ps -aux 参数说明: a :选择所有的程序列出 u :所有使 ...
- SQL增加,删除,更改表中字段
1. 向表中添加新的字段 alter table table_name add column_name varchar2(20) not null 2. 删除表中的一个字段 delete t ...
- Are Landing Pages Killing Your Conversion Rate?
http://searchenginewatch.com/sew/how-to/2411253/are-landing-pages-killing-your-conversion-rate
- Java之注解
package com.demo.test; import java.lang.annotation.Documented; import java.lang.annotation.ElementTy ...
- Code Sign error: No unexpired provisioning profiles found that contain any of the keychain's signing certificates
最近离职了,刚好在离职之际有人叫我帮做个项目,简直了,没有mac电脑,没有真ji设备,简直了.接项目那哥们,暂且叫做J,大哥说我给你想办法,then,给借了个mac pro.刚拿到电脑真是喜出望外啊, ...
- line-height的小技巧
CSS中的line-height属性控制着文字的行间距离.通常被设置为一个无单位的值(例如:line-height:1.4),与文字尺寸是成比例的.它是排版中的一个重要的属性.太低了文字会挤在一起,太 ...
- nagios架构及windows,linux客户端配置
Linux下Nagios的安装与配置 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等. ...
- 转载 《AngularJS》5个实例详解Directive(指令)机制
<AngularJS>5个实例详解Directive(指令)机制 大漠穷秋 本文整理并扩展了<AngularJS>这本书第六章里面的内容,此书近期即将由电子工业出版社出版,敬请 ...
- sql 截取字符串第一次出现字符之前的数据
截取sql 第一次出现字符之前的数据 (select left( a.ChangeProductName,charindex(',', ChangeProductName)-1)) as Chang ...
- VS2013 试用版到期 解决办法
摘自:http://jingyan.baidu.com/article/fec7a1e5100b481190b4e7d9.html 输入密钥:BWG7X-J98B3-W34RT-33B3R-JVYW9