【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. linux内核链表的使用

    linux内核链表:链表通常包括两个域:数据域和指针域.struct list_head{struct list_head *next,*prev;};include/linux/list.h中实现了 ...

  2. java线程池技术(一):ThreadFactory与BlockingQueue

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.ThreadFactory概述以及源码分析 ThreadFactory很简单,就是一个线程工厂也就是负责生产线程的,我们看下ThreadFact ...

  3. 一步一步配置ABP Core Template with Angular

    1.首先去https://aspnetboilerplate.com/Templates下载模板工程,按如下勾选 2.下载后打开工程如下图,并设置Web.host 作为启动项目,rebuild 还原n ...

  4. Phone文件备份

    一 把照片导入到本地 连接手机和PC,插上数据线后PC上会自动检测并弹出对话框,提示导入 可以指定导入的目录.确定之后点击导入即可开始执行导入操作. 二 把语音备忘录导入到本地 需要借助iTunes联 ...

  5. 记录一次CentOS环境升级Python2.6到Python2.7并安装最新版pip

    背景介绍 一次实验中需要安装python-etcd包.安装这个包时要求的python和pip版本比目前系统的版本高. 系统是centos6.6    64位 1 2 3 4 5 6 7 [root@m ...

  6. virsh 常用操作

    virsh list 显示在运行的 虚拟机    virsh list --all 显示在运行和停止的虚拟机    ssh 192.168.0.115 通过网络连接子机   如果没有网络 可以通过 v ...

  7. backgroud背景色样式兼容ie8 rgba()用法

    今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,.1);但是要兼容到i ...

  8. php常用面试知识点

    1.php基础 2.mysql基础 3.js基础 4.jq 5.正则 6.面向对象 7.分页类,购物车类,数据库类,上传类,图片处理类 8.smarty模板技术(以及自己写模板引擎) 9.ajax 1 ...

  9. java中try catch块的使用

    对于关流操作的时候,最好采用如下语句块: InputStream in=......; try{ try{ //some statemenet }finally{ //close stream in. ...

  10. core java

    ConsoleTest,这个程序如果在IDE里运行就会因无法获得控制台而报错 import java.io.Console; public class ConsoleTest { public sta ...