【BZOJ3506】排序机械臂(Splay)
【BZOJ3506】排序机械臂(Splay)
题面
题解
对于每次旋转的物体
显然可以预处理出来
现在只要模拟旋转操作就行了
至于在哪里放标记的问题
我只在第K大放会鬼。。
所以在Splay里面也放了一次(和LCT一样的)
然而我每次都把排到了正确位置的元素直接给删掉了。。。
所以跑的很慢很慢。。。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 120000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int ch[2],ff;
int v,size;
int rev;
}t[MAX];
int root,n;
int S[MAX],top;
void pushup(int x)
{
if(!x)return;
t[x].size=t[lson].size+t[rson].size+1;
}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff;
int k=t[y].ch[1]==x;
if(z)t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
void putrev(int x)
{
swap(lson,rson);
t[x].rev^=1;
}
void pushdown(int x)
{
if(!t[x].rev)return;
t[x].rev^=1;
if(lson)putrev(lson);
if(rson)putrev(rson);
}
void Splay(int x,int goal)
{
S[top=1]=x;
for(int i=x;i!=root;i=t[i].ff)S[++top]=t[i].ff;
while(top)pushdown(S[top--]);
while(t[x].ff!=goal)
{
int y=t[x].ff,z=t[y].ff;
if(z!=goal)
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
if(!goal)root=x;
}
int Build(int l,int r)
{
if(l>r)return 0;
int mid=(l+r)>>1;
t[mid].ch[0]=Build(l,mid-1);
t[mid].ch[1]=Build(mid+1,r);
t[t[mid].ch[0]].ff=t[t[mid].ch[1]].ff=mid;
pushup(mid);
return mid;
}
int Kth(int k)
{
int x=root;
while(233)
{
if(t[x].rev)pushdown(x);
if(t[lson].size+1>=k)
{
if(t[lson].size+1==k)return x;
else x=lson;
}
else k-=t[lson].size+1,x=rson;
}
return 0;
}
int Rank(int x)//查找x的排名
{
Splay(x,0);
return t[lson].size+1;
}
void Delete(int x)//删除节点x
{
int tt=Rank(x);
int L=Kth(tt-1);
int R=Kth(tt+1);
Splay(L,0);Splay(R,L);
t[R].ch[0]=0;
pushup(R);pushup(L);
}
void Outp(int x)
{
if(lson)Outp(lson);
printf("%d ",t[x].v);
if(rson)Outp(rson);
}
pair<int,int> w[MAX];
int main()
{
t[0].v=1e9;
n=read();
for(int i=2;i<=n+1;++i)t[i].v=read(),w[i-1]=make_pair(t[i].v,i);
sort(&w[1],&w[n+1]);
t[1].v=t[n+2].v=1e9;
root=Build(1,n+2);
for(int i=1;i<=n;++i)
{
int pp=w[i].second;
int gg=Rank(pp);
printf("%d ",gg-2+i);
Splay(1,0);
Splay(pp,1);
if(t[pp].ch[0])putrev(t[pp].ch[0]);
Delete(pp);
}
return 0;
}
/*
9
1 3 2 3 3 2 1 2 1
*/
【BZOJ3506】排序机械臂(Splay)的更多相关文章
- [BZOJ3506] [Cqoi2014] 排序机械臂 (splay)
Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...
- 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 806 Solved: 329[Submit][ ...
- 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值
可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...
- 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)
点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...
- 刷题总结:排序机械臂(石室中学oj)(splay)
题目: 题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到最低的物品位置 P1,并把从左起第 1 个至第 P1 个之间的物品反序 ...
- P3165 [CQOI2014]排序机械臂
题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1 ,并把左起第一个物品至 P1P_1P1 ...
- LibreOJ2241 - 「CQOI2014」排序机械臂
Portal Description 给出一个\(n(n\leq10^5)\)个数的序列\(\{a_n\}\),对该序列进行\(n\)次操作.若在第\(i\)次操作前第\(i\)小的数在\(p_i\) ...
- [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap
robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...
- 洛谷P3165 [CQOI2014]排序机械臂
题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...
随机推荐
- 一个.net专业户转Spring Boot V2.0开发的体会
java web的idea开发工具总体用起来还是比vs差很多,但是在使用Hibernate跟MyBatis的感触,Hibernate有着.net core ef没有的细腻,Hibernate在细节上完 ...
- Cookie禁用 获取session
转自:http://blog.csdn.net/u010433704/article/details/40950599 Cookie与 Session,一般认为是两个独立的东西,Session采用的是 ...
- fifteen-puzzle
http://www.math.ubc.ca/~cass/courses/m308-02b/projects/grant/fifteen.html http://jamie-wong.com/2011 ...
- MySQL5.7 group by新特性,报错1055
项目中本来使用的是mysql5.6进行开发,切换到5.7之后,突然发现原来的一些sql运行都报错,错误编码1055,错误信息和sql_mode中的"only_full_group_by&qu ...
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication【最小割】分析+题解代码
洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...
- Python自动化--语言基础3--字典、函数、全局/局部变量
字典 dict1 = {'name':'han','age':18,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values ...
- JS原生Ajax&Jquery的Ajax技术&Json
1.介绍Ajax Ajax = 异步 JavaScript 和 XML Ajax是一种创建快速动态网页的技术 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新.这意味着可以不用整个 ...
- 使用Ajax发送http请求(get&post请求)
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 同步和异步 同步和异步的概念 同步:必须等待前面的任务完成,才能继续后面 ...
- 一种解决eclipse中安装maven出错的方法
1.安装步骤:https://jingyan.baidu.com/article/a17d5285feb4dd8099c8f26e.html 2.安装第三步的解决办法:m2e 路径换成 http ...
- centos/linux下的安装mysql
1.从yum 下面下载mysql数据库 yum -y install mysql-server 2.查询该mysql是否安装完成 rpm -qa|grep mysql-server 出现如下图所示标明 ...