【BZOJ3506】排序机械臂(Splay)

题面

神TMBZOJ没有题面,感谢SYC的题面

洛谷的题面也不错

题解

对于每次旋转的物体

显然可以预处理出来

现在只要模拟旋转操作就行了

至于在哪里放标记的问题

我只在第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)的更多相关文章

  1. [BZOJ3506] [Cqoi2014] 排序机械臂 (splay)

    Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...

  2. 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay

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

  3. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  4. 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)

    点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...

  5. 刷题总结:排序机械臂(石室中学oj)(splay)

    题目: 题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到最低的物品位置 P1,并把从左起第 1 个至第 P1 个之间的物品反序 ...

  6. P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1​ ,并把左起第一个物品至 P1P_1P1 ...

  7. LibreOJ2241 - 「CQOI2014」排序机械臂

    Portal Description 给出一个\(n(n\leq10^5)\)个数的序列\(\{a_n\}\),对该序列进行\(n\)次操作.若在第\(i\)次操作前第\(i\)小的数在\(p_i\) ...

  8. [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap

    robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...

  9. 洛谷P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...

随机推荐

  1. PHP中的错误处理机制

    常见的三种错误: 1.Notice :通知性错误,最小的错误,当发生通知性错误时,会弹出一个提示信息.不会中断代码的执行. 错误代码: #例如Notice: 2.Warning:警告性错误,当发生警告 ...

  2. 【Tools】linux更改分辨率,解决虚拟机安装后太小的问题

    Linux更改屏幕分辨率 1,分辨率模式已存在 1)如何查询是否存在: 终端输入命令:xrandr,即会输出当前已存在的分辨率模式. 2)如何配置: 使用命令xrandr --output 显示器名称 ...

  3. PLEC-交流电机系统+笔记

    1.固有机械特性近似图 2.三相交流电机的控制系统 1)理论推导 第一次制动选择能耗制动,第二次制动选择倒拉制动. 2)模型搭建 3)模拟仿真 3.心得体会和笔记总结 制动方式的选择主要是根据各个制动 ...

  4. linux使用tcpdump抓包工具抓取网络数据包,多示例演示

    tcpdump是linux命令行下常用的的一个抓包工具,记录一下平时常用的方式,测试机器系统是ubuntu 12.04. tcpdump的命令格式 tcpdump的参数众多,通过man tcpdump ...

  5. 微信小程序Md5加密(utf-8汉字无影响)

    微信小程序不让使用第三方jqMD5 只好改原生js咯 废话不多说直接贴代码 其实就是将原生function调用改为 module.exports = md5; 文中 红色标注 使用方法 将md5.js ...

  6. Hibernate 一对一中的一些问题

    1.对于想查询一对一种一方为空的时候使用 例如一个用户对应一个人,则要从人查找没有用户的人员的话, 使用hql语句是查询不到的 我今天也碰到了这个问题,研究了下,可以用以下语句查出来:from Per ...

  7. Maven json包找不到解决办法

    在Maven中央仓库找到Maven的jar <dependency> <groupId>net.sf.json-lib</groupId> <artifact ...

  8. CodeForces - 796A Buying A House

    思路:从m直接向两边枚举,如果当前点需要的费用小于等于k,说明一定是最近距离. AC代码 #include <cstdio> #include <cmath> #include ...

  9. APP性能测试(启动时间)

    #encoding:utf-8 import csv import os import time class App(object): def __init__(self): self.content ...

  10. chrome浏览器Timing内各字段解析

    Queueing请求文件顺序的的排序   Stalled是浏览器得到要发出这个请求的指令到请求可以发出的等待时间,一般是代理协商.以及等待可复用的TCP连接释放的时间,不包括DNS查询.建立TCP连接 ...