【洛谷 P3165】 [CQOI2014]排序机械臂 (Splay)
题目链接
debug了\(N\)天没debug出来,原来是找后继的时候没有pushdown。。。
众所周知,,Splay中每个编号对应的节点的值是永远不会变的,因为所有旋转、翻转操作改变的都是父节点和子节点的指针。
于是记录每个数在\(Splay\)中的位置,然后按大小升序排序,每次把第\(i\)个数转到根,然后其左儿子的大小就是本次的答案(为什么不是左儿子大小+1?因为有个哨兵节点啊)。
然后区间翻转就不用说了,基本操作。
#include <cstdio>
#include <algorithm>
using namespace std;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0',ch = getchar();
return s * w;
}
const int MAXN = 100010;
struct Map{
int val, pos, id;
int operator < (const Map A) const{
return val < A.val || (val == A.val && id < A.id);
}
}p[MAXN];
struct SplayTree{
int ch[2], fa, val, lazy, size, pos;
Map Min;
}t[MAXN];
int num, root, n, m;
void pushdown(int u){
if(t[u].lazy){
swap(t[u].ch[0], t[u].ch[1]);
t[t[u].ch[0]].lazy ^= 1;
t[t[u].ch[1]].lazy ^= 1;
t[u].lazy = 0;
}
}
void pushup(int u){
t[u].size = t[t[u].ch[0]].size + t[t[u].ch[1]].size + 1;
}
void rotate(int x){
int y = t[x].fa; int z = t[y].fa; int k = t[y].ch[1] == x;
pushdown(y); pushdown(x);
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y); pushup(x);
}
void Splay(int x, int goal){
while(t[x].fa != goal){
int y = t[x].fa; int z = t[y].fa;
if(z) pushdown(z); pushdown(y); pushdown(x);
if(z != goal)
(t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
rotate(x);
}
if(goal == 0) root = x;
}
int build(int l, int r){
int id = ++num;
int mid = (l + r) >> 1;
t[id].val = p[mid].val; p[mid].pos = id;
if(mid > l){
t[id].ch[0] = build(l, mid - 1);
t[t[id].ch[0]].fa = id;
}
if(mid < r){
t[id].ch[1] = build(mid + 1, r);
t[t[id].ch[1]].fa = id;
}
pushup(id);
return id;
}
inline int findKth(int k){
int u = root;
while(1){
pushdown(u);
if(t[t[u].ch[0]].size >= k) u = t[u].ch[0];
else if(t[t[u].ch[0]].size == k - 1) return u;
else k -= t[t[u].ch[0]].size + 1, u = t[u].ch[1];
}
}
int next(int x){
Splay(x, 0);
pushdown(x);
int u = t[x].ch[1];
pushdown(u);
while(t[u].ch[0]){
u = t[u].ch[0];
pushdown(u);
}
return u;
}
int main(){
n = read();
for(int i = 1; i <= n; ++i)
p[i].val = read(), p[i].id = i;
p[n + 1].val = 2147483647; p[n + 1].id = n + 1; p[0].val = -2147483646;
root = build(0, n + 1);
sort(p + 1, p + n + 1);
for(int i = 1; i <= n; ++i){
int l = findKth(i);
int r = p[i].pos;
Splay(r, 0);
printf("%d ", t[t[root].ch[0]].size);
r = next(r);
Splay(l, 0);
Splay(r, l);
t[t[t[root].ch[1]].ch[0]].lazy ^= 1;
}
return 0;
}
【洛谷 P3165】 [CQOI2014]排序机械臂 (Splay)的更多相关文章
- 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值
可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...
- 洛谷P3165 [CQOI2014]排序机械臂
题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...
- [UVA1402]Robotic Sort;[SP2059]CERC07S - Robotic Sort([洛谷P3165][CQOI2014]排序机械臂;[洛谷P4402][Cerc2007]robotic sort 机械排序)
题目大意:一串数字,使用如下方式排序: 先找到最小的数的位置$P_1$,将区间$[1,P_1]$反转,再找到第二小的数的位置$P_2$,将区间$[2,P_2]$反转,知道排序完成.输出每次操作的$P_ ...
- P3165 [CQOI2014]排序机械臂
题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1 ,并把左起第一个物品至 P1P_1P1 ...
- Luogu P3165 [CQOI2014]排序机械臂
先讲一下和这题一起四倍经验的题: Luogu P4402 [Cerc2007]robotic sort 机械排序 SP2059 CERC07S - Robotic Sort UVA1402 Robot ...
- [BZOJ3506] [Cqoi2014] 排序机械臂 (splay)
Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...
- 【BZOJ3506】[CQOI2014] 排序机械臂(Splay)
点此看题面 大致题意: 给你\(n\)个数.第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推.求 ...
- bzoj3506 [Cqoi2014]排序机械臂
bzoj3506 此题是一道比较简单的spaly题目. 用splay维护序列,将每个点排到对应的位置之后删除,这样比较容易区间翻转. 我的指针写法在洛谷上AC了,但在bzoj上RE. #include ...
- 1552/3506. [CQOI2014]排序机械臂【平衡树-splay】
Description Input 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. 第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. Output ...
随机推荐
- lintcode-151-买卖股票的最佳时机 III
151-买卖股票的最佳时机 III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 注意事项 你不可以同时参与多笔交易(你必须在 ...
- JavaScript:理解事件、事件处理函数、钩子函数、回调函数
详情请点击 http://www.jianshu.com/p/a0c580ed3432
- Perfmon - 脚本自动监控
PerfMon-Windows性能监视器是个好东西,可以辅助我们分析发生问题时间段服务器资源占用情况,但是部署性能计数器确实一个相当麻烦的事情,往往这种枯燥的事别人还做不了,只能由我们这些希望获取到P ...
- centos升级python(从2.6.6升级到2.7.8)
***先安装readline,否则升级后python回退和方向键不能使用 yum install readline-devel.x86_64 1.#wget www.python.org/ftp/ ...
- CS6的安装与破解
大家在Mac下肯定也少不了对图片进行修改,那也就少不了Photoshop这款软件. 今天在这里分享下苹果下的Adobe PhotoshopCS6,这个软件大家应该都很熟悉,主要功能什么我就不多做介绍了 ...
- BZOJ 1197 花仙子的魔法(递推)
数学归纳法. dp[i][j]=dp[i][j-1]+dp[i-1][j-1]. # include <cstdio> # include <cstring> # includ ...
- 【poj2096】Collecting Bugs 期望dp
题目描述 Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other materia ...
- HDU1561:The more, The Better——题解
http://acm.hdu.edu.cn/showproblem.php?pid=1561 ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允 ...
- HDU1007:Quoit Design——题解
http://acm.hdu.edu.cn/showproblem.php?pid=1007 题目大意:给n个点,求点对最短距离/2. —————————————————————— 平面分治裸题. 暂 ...
- AOJ.850 电缆公司的烦恼 (二分+枚举)
AOJ.850 电缆公司的烦恼 (二分+枚举) 题意分析 从[1,average]二分枚举长度即可,由于保留2位小数,可以将数据扩大10^2倍后后枚举,输出时除100即可. 代码总览 #include ...