伸展树伤不起啊,很容易wa,很容易T,很容易M。

 /* 1890 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define grandlson ch[ch[root][1]][0] typedef struct node_t {
int id, val;
node_t() {}
friend bool operator<(const node_t& a, const node_t& b) {
if (a.val != b.val)
return a.val < b.val;
return a.id < b.id;
}
} node_t; const int maxn = 1e5+;
int pre[maxn], ch[maxn][], size[maxn], rev[maxn];
node_t nd[maxn];
int root, tot, n;
int ans[maxn]; void newNode(int& r, int k, int fa) {
r = k;
pre[r] = fa;
ch[r][] = ch[r][] = ;
size[r] = ;
rev[r] = ;
} void PushUp(int r) {
size[r] = size[ch[r][]] + size[ch[r][]] + ;
} void UpdateRev(int r) {
if (r == ) return ; swap(ch[r][], ch[r][]);
rev[r] ^= ;
} void PushDown(int r) {
if (rev[r]) {
UpdateRev(ch[r][]);
UpdateRev(ch[r][]);
rev[r] = ;
}
} void Build(int& rt, int l, int r, int fa) {
if (l > r) return ; int mid = (l + r) >> ;
newNode(rt, mid, fa);
Build(ch[rt][], l, mid-, rt);
Build(ch[rt][], mid+, r, rt);
PushUp(rt);
} void Rotate(int x, int d) {
int y = pre[x];
PushDown(y);
PushDown(x);
ch[y][d^] = ch[x][d];
pre[ch[x][d]] = y;
if (pre[y])
ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x] = pre[y];
ch[x][d] = y;
pre[y] = x;
PushUp(y);
} void Splay(int r, int goal) { PushDown(r);
while (pre[r] != goal) {
if (pre[pre[r]] == goal) {
PushDown(pre[r]);
PushDown(r);
Rotate(r, ch[pre[r]][]==r);
} else {
PushDown(pre[pre[r]]);
PushDown(pre[r]);
PushDown(r);
int y = pre[r];
int d = ch[pre[y]][]==y;
if (ch[y][d] == r) {
Rotate(r, d^);
Rotate(r, d);
} else {
Rotate(y, d);
Rotate(r, d);
}
}
} PushUp(r);
if (goal == )
root = r;
} int kth(int r, int k) {
int sz = size[ch[r][]] + ; PushDown(r);
if (k == sz)
return r;
if (k < sz)
return kth(ch[r][], k);
else
return kth(ch[r][], k-sz);
} int getNext(int rt) {
PushDown(rt);
if (ch[rt][] == ) return -;
rt = ch[rt][];
while (ch[rt][]) {
rt = ch[rt][];
PushDown(rt);
}
return rt;
} void init() {
ch[root][] = ch[root][] = size[root] = rev[root] = ;
newNode(root, n+, );
newNode(ch[root][], n+, root);
Build(grandlson, , n, ch[root][]);
PushUp(ch[root][]);
PushUp(root);
} void inorder(int rt) {
if (rt == ) return ; inorder(ch[rt][]);
printf("rt = %d, lson = %d, rson = %d, fa = %d, sz = %d, rev = %d\n",
rt, ch[rt][], ch[rt][], pre[rt], size[rt], rev[rt]);
inorder(ch[rt][]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int r; while (scanf("%d", &n)!=EOF && n) {
rep(i, , n+) {
scanf("%d", &nd[i].val);
nd[i].id = i;
}
init();
sort(nd+, nd++n);
rep(i, , n+) {
r = nd[i].id;
Splay(r, );
ans[i] = size[ch[root][]];
Splay(kth(root, i), );
Splay(getNext(r), root);
UpdateRev(grandlson);
}
rep(i, , n+) {
printf("%d", ans[i]);
if (i == n)
putchar('\n');
else
putchar(' ');
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】1890 Robotic Sort的更多相关文章

  1. 【BZOJ1552】[Cerc2007]robotic sort Splay

    [BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...

  2. 【bzoj1552】[Cerc2007]robotic sort

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

  3. 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值

    [bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...

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

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

  5. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  6. HDU 1890 Robotic Sort(splay)

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

  7. HDU 1890 Robotic Sort | Splay

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

  8. 【转】Postgres SQL sort 操作性能调优

    这篇文章将以实战的方式结合笔者在项目中真实遇到的情况来讲解.说到SQL,大家可能会遇到一些写法稍微复杂的写法.比如SQL中遇到的有聚合函数sum等,也有遇到使用group by / order by的 ...

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

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

随机推荐

  1. ajax 特殊参数值无法传到后台问题

    用原生的ajax请求后台的登录功能,使用特殊字符作为密码的时候发现无法把参数传到后台;发现前端就报错了.可能是因为特殊符号吧. 用 encodeURIComponent() 这个方法进行编码之后就可以 ...

  2. Android中log4j的运用

    网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...

  3. 将Excel,ppt和word转化为html

    有些时候可能需要将Excel,ppt和word转化为html在页面上显示.我从网上查到一些代码,记录在这里以供需要的朋友参考 1.将word转化为html显示 //================== ...

  4. Java_LIST使用方法和四种遍历arrayList方法

    1.List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象.   List接口的常用实现类有ArrayList和Linked ...

  5. SQL 查找存储过程及视图与自带函数

    查找所有所有存储过程的名称及信息select * from sysobjectswhere type='P' 查看存储过程定义语句sp_helptext [存储过程名] 查看所有视图及信息select ...

  6. Oracle官网下载地址大全(包括11g、10g和9i)

    Oracle11g下载: Microsoft Windows(32 位)的 Oracle Database 11g 第 2 版 (11.2.0.1.0) http://download.oracle. ...

  7. [转]Javascript 严格模式详解

    原文地址:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscript 5添加 ...

  8. java web-----servelet

    1,定义servlet: 2,servlet生命周期: 3,HttpServlet常用方法: 4,servlet常用接口: 一,servlet定义: servlet是一个继承HttpServlet类的 ...

  9. HDU 4707 Pet(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...

  10. stack around the variable “XX” was corrupted

    晚上花了几个小时fix了这个恼人的BUG!“在变量XX周围的堆栈已损坏” 在网上找到的解释是: 把“project->配置属性->c/c++->代码生成->基本运行时检查 设置 ...