BZOJ_1552_[Cerc2007]robotic sort_splay
BZOJ_1552_[Cerc2007]robotic sort_splay
题意:

分析:
splay维护区间操作
可以先把编号排序,给每个编号分配一个固定的点,映射过去
查找编号的排名时先找到这个点,找出到根的路径
从上至下pushdown标记,顺便求出这个点的排名
然后翻转啥的就好做了
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int a[N],ch[N][2],f[N],siz[N],val[N],turn[N],rt;
int n,w[N];
struct A{
int num,id;
}b[N];
bool cmp(const A &x,const A &y)
{
if(x.num==y.num)return x.id<y.id;
return x.num<y.num;
}
void print(int p)
{
if(!p)return ;
if(ls)print(ls);
printf("p=%d ,val[p]=%d ",p,val[p]);
if(rs)print(rs);
}
void pushup(int p){ if(p) siz[p]=siz[ls]+siz[rs]+1; }
void pushdown(int p)
{
if(turn[p])
{
turn[p]=0;
turn[ls]^=1;turn[rs]^=1;
swap(ch[ls][0],ch[ls][1]);
swap(ch[rs][0],ch[rs][1]);
}
}
void rotate(int x)
{
int y=f[x],z=f[y],k=get(x);
ch[y][k]=ch[x][k^1];f[ch[y][k]]=y;ch[x][k^1]=y;
f[y]=x;f[x]=z;if(z)ch[z][ch[z][1]==y]=x;
pushup(y),pushup(x);if(rt==y)rt=x;
}
void splay(int x,int y)
{
for(int fa;(fa=f[x])!=y;rotate(x))
if(f[fa]!=y)
rotate((get(x)==get(fa)) ? fa : x);
}
int find(int x)
{
int p=rt;
while(1){
pushdown(p);
if(x<=siz[ls])p=ls;
else{
x-=siz[ls]+1;
if(!x)return p;
p=rs;
}
}
}
void build(int fa,int l,int r,int flg)
{
if(l>r)return ;
int mid=l+r>>1;
ch[fa][flg]=mid;
f[mid]=fa;
siz[mid]=1;
val[mid]=a[mid-1];
w[a[mid-1]]=mid;
build(mid,l,mid-1,0);
build(mid,mid+1,r,1);
pushup(mid);
}
int getrank(int p)
{
int cnt=0;
while(p)
{
a[++cnt]=p;
p=f[p];
}
int i;
for(i=cnt;i;i--) pushdown(a[i]);
splay(a[1],0);
return siz[ch[a[1]][0]];
}
void reverse(int x,int p)
{
x=find(x);
p=find(p);
splay(x,0);
splay(p,rt);
turn[ls]=1;
swap(ch[ls][0],ch[ls][1]);
pushup(p);pushup(x);
}
int main()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&b[i].num);
b[i].id=i;
}
sort(b+1,b+n+1,cmp);
for(i=1;i<=n;i++)
{
a[b[i].id]=i;
}
build(0,1,n+2,1);
rt=n+3>>1;
for(i=1;i<=n;i++)
{
int tmp=getrank(w[i]);
if(i-1)printf(" %d",tmp);
else printf("%d",tmp);
reverse(i,tmp+2);
}
}
BZOJ_1552_[Cerc2007]robotic sort_splay的更多相关文章
- [BZOJ1552][Cerc2007]robotic sort
[BZOJ1552][Cerc2007]robotic sort 试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数 ...
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
- 【BZOJ1552】[Cerc2007]robotic sort Splay
[BZOJ1552][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N ...
- bzoj 1552: [Cerc2007]robotic sort
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1198 Solved: 457[Submit] ...
- 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值
[bzoj1552/3506][Cerc2007]robotic sort Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. ...
- [Cerc2007]robotic sort
splay区间反转练手题 #include <iostream> #include <cstdio> #include <algorithm> using name ...
- 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序
FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...
- BZOJ 1552/1506 [Cerc2007]robotic sort
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1552 [分析] 这题哇!又有翻转操作...每次要输出第几个?是吧... 所以又要用Spla ...
- 【bzoj1552】[Cerc2007]robotic sort
题目描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开的 ...
随机推荐
- MapReduce编程模型详解(基于Windows平台Eclipse)
本文基于Windows平台Eclipse,以使用MapReduce编程模型统计文本文件中相同单词的个数来详述了整个编程流程及需要注意的地方.不当之处还请留言指出. 前期准备 hadoop集群的搭建 编 ...
- 【redis】Java连接云服务器redis之JedisConnectionException的异常问题
代码很简单: public static void main(String[] args) { Jedis jedis = new Jedis("116.85.10.216",63 ...
- 使用IntelliJ IDEA的小技巧快乐编程(2)
前言 本篇介绍的技巧为IntelliJ IDEA中自动代码生成相关的技巧,合理的使用这些技巧将大大提高的你的编码效率 :) Trick 6. 使用模板代码 idea默认的提供了许多模板代码,你可以使用 ...
- Java 深度克隆 clone()方法重写 equals()方法的重写
1.为什么要重写clone()方法? 答案:Java中的浅度复制是不会把要复制的那个对象的引用对象重新开辟一个新的引用空间,当我们需要深度复制的时候,这个时候我们就要重写clone()方法. 2.为什 ...
- 如何写jquery插件
首页 新文章 联系 管理 订阅 自己写一个 jQuery 插件 我知道这一天终将会到来,现在,它来了. 需求 开发 SharePoint 的 CSOM 应用时,经常需要在网页上输出一 ...
- Struts,Spring,Hibernate三大框架的
1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transation 5.持 ...
- 2018年,传言QQ首次被神秘黑客DDOS攻击,Python可以实现?
于2018-5-10日晚 网络流传黑客DDOS攻击了QQ服务器,导致大家聊天发送内容时出现感叹号.我们都知道一般情况下出现感叹号都是你的网络不稳定,或者...别人已经删除你了.然而昨晚很奇怪,发出的内 ...
- Linnux入门之简介
一.Linux简介 Minix(教授实验) -> Linux(大三学生Linus)企鹅作为吉祥物 linux主要分为内核版本和发行版本 linux 内核版本 :官网下载:https://www. ...
- 连接Access数据遇到的问题总览!
由于要访问一个厂商的access数据,所以要写一个对于access的demo,相对于mysql.sqlserver来说,连接access花费了不少精力,现在将遇到的问题分享出来,以后大家遇到类似问题时 ...
- CSS学习笔记五:display,position区别
最近常用css,经常在位置方面使用导display与position这两个属性,所以想要弄清楚它们之间的意思. 一.display 作用是规定元素应该生成的框的类型.意思是定义建立布局时元素生成的显示 ...