splay区间反转练手题

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=;
int n,fa[N],ch[N][],val[N],siz[N],tot,pos[N],rt;
struct Node{
int id,v;
}a[N];
bool cmp1(Node x,Node y) {return x.v==y.v?x.id<y.id:x.v<y.v;}
bool cmp2(Node x,Node y) {return x.id<y.id;}
bool rev[N];
inline void pushup(int x) {siz[x]=siz[ch[x][]]+siz[ch[x][]]+;}
void pushdown(int x) {
if(rev[x]) {
swap(ch[x][],ch[x][]);
rev[ch[x][]]^=,rev[ch[x][]]^=;
rev[x]=;
}
}
int build(int f,int l,int r) {
if(l>r) return ;
int now=++tot,mid=l+r>>;
val[now]=a[mid].v;fa[now]=f;
pos[a[mid].v]=now;
ch[now][]=build(now,l,mid-);
ch[now][]=build(now,mid+,r);
pushup(now);
return now;
}
inline void rotate(int x) {
int f=fa[x],ff=fa[f];
pushdown(f);pushdown(x);
bool tag=ch[fa[x]][]==x;
ch[f][tag]=ch[x][tag^];
fa[ch[f][tag]]=f;
fa[f]=x;
ch[x][tag^]=f;
fa[x]=ff;
if(ff) ch[ff][f==ch[ff][]]=x;
pushup(f);pushup(x);
}
inline void splay(int x,int tar){
for(int f;(f=fa[x])!=tar;rotate(x)) if(fa[f]!=tar)rotate((x==ch[fa[x]][])==(f==ch[fa[f]][])?f:x);
if(!tar) rt=x;
}
int nxt(){
pushdown(rt);
int x=ch[rt][];
while(pushdown(x),ch[x][]) x=ch[x][];
return x;
}
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i+].v),a[i+].id=i+;
a[].v=;a[n+].v=n+;//a[1].id=1,a[n+2].id=n+2;
sort(a+,a++n,cmp1);
for(int i=;i<=n+;i++) a[i].v=i-;
sort(a+,a++n,cmp2);
rt=build(,,n+);
for(int i=;i<=n;i++) {
int x=pos[i];splay(x,);
printf("%d ",siz[ch[x][]]);
x=nxt();
int y=pos[i-];
splay(y,);
splay(x,y);
rev[ch[x][]]^=;
}
}

robotic sort

[Cerc2007]robotic sort的更多相关文章

  1. [BZOJ1552][Cerc2007]robotic sort

    [BZOJ1552][Cerc2007]robotic sort 试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数 ...

  2. BZOJ 1552: [Cerc2007]robotic sort( splay )

    kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...

  3. 【BZOJ1552】[Cerc2007]robotic sort Splay

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

  4. bzoj 1552: [Cerc2007]robotic sort

    1552: [Cerc2007]robotic sort Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1198  Solved: 457[Submit] ...

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

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

  6. 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序

    FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...

  7. 【bzoj1552】[Cerc2007]robotic sort

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

  8. [BZOJ1552] [Cerc2007] robotic sort (splay)

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

  9. BZOJ1552[Cerc2007]robotic sort&BZOJ3506[Cqoi2014]排序机械臂——非旋转treap

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

随机推荐

  1. 高仿QQ6.0之側滑删除

    前两天已经完毕了高仿QQ6.0側滑和优化,今天来看下側滑删除的实现吧,假设有兴趣,能够去看下之前的两篇,仿QQ6.0側滑之ViewDragHelper的使用(一)和高仿QQ6.0側滑菜单之滑动优化(二 ...

  2. BNU 13259.Story of Tomisu Ghost 分解质因子

    Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost ...

  3. oc39-- 类的内存存储

    虚线是isa的指向,实线是继承关系. // // main.m // 类的本质 #import <Foundation/Foundation.h> #import "Person ...

  4. B1231 [Usaco2008 Nov]mixup2 混乱的奶牛 状压dp

    发现是状压dp,但是还是不会...之前都白学了,本蒟蒻怎么这么菜,怎么都学不会啊... 其实我位运算基础太差了,所以状压学的不好. 题干: Description 混乱的奶牛 [Don Piele, ...

  5. 云-阿里云-OSS:对象存储 OSS

    ylbtech-云-阿里云-OSS:对象存储 OSS 对象存储服务(Object Storage Service,OSS)是一种海量.安全.低成本.高可靠的云存储服务,适合存放任意类型的文件.容量和处 ...

  6. day-05 python函数

    # #-*- coding:utf-8 -*-# 1:编写一个名为 make_shirt()的函数,它接受一个尺码以及要印到 T 恤上的字样.这个函数应打印一个句子,概要地说明 T 恤的尺码和字样.d ...

  7. java selenium启动火狐浏览器报错:Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z

    Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build in ...

  8. Vue 区别

    computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.

  9. Ajax 传递json字符串到客户端时报 Internal server error

    架构:struts2+JQuery 需求:就是前台请求后台,后台查询数据库,将数据转换成json格式,使用struts2框架赋值给action内的变量jsonStr,前台通过 response.jso ...

  10. JS排序之快速排序

    JS排序之快速排序 一个数组中的数据,选择索引为(2/数组长度)的那个数据作为基数,数组中的其他数据与它对比,比它数值小的放在做数组,比它数值大的放在右数组,最后组合 左数组+基数+右数组,其中,左数 ...