2018牛客多校第三场 C.Shuffle Cards
题意:
给出一段序列,每次将从第p个数开始的s个数移到最前面。求最终的序列是什么。
题解:
Splay翻转模板题。存下板子。
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e6;
const int N = 1e5+;
int n, m;
int s, t;
int root, sz, tot;
int key[N], f[N], size[N], delta[N], cnt[N], ch[N][];
int a[N], ans[N];
int get(int x) {
return ch[f[x]][] == x;
}
void update(int x) {
if(x) {
size[x] = cnt[x];
if(ch[x][]) size[x] += size[ch[x][]];
if(ch[x][]) size[x] += size[ch[x][]];
}
}
int build(int l, int r, int fa) {
if (l > r) return ;
int mid = l+r>>;
int now = ++sz;
key[now] = a[mid]; f[now] = fa; cnt[now] = ;
int lch = build(l, mid-, now);
int rch = build(mid+, r, now);
ch[now][] = lch, ch[now][] = rch;
update(now);
return now;
}
void pushdown(int x) {
if(x && delta[x]) {
swap(ch[x][], ch[x][]);
delta[ch[x][]] ^= ; delta[ch[x][]] ^= ;
delta[x] = ;
}
}
void rotate(int x) {
pushdown(f[x]);
pushdown(x);
int old = f[x], oldf = f[old], which = get(x);
ch[old][which] = ch[x][which^]; f[ch[old][which]] = old;
f[old] = x; ch[x][which^] = old;
f[x] = oldf;
if(oldf) ch[oldf][ch[oldf][]==old] = x;
update(old); update(x);
}
void splay(int x, int tar) {
for(int fa; (fa = f[x])!=tar; rotate(x))
if(f[fa] != tar)
rotate((get(x)==get(fa))?fa:x);
if(!tar) root = x;
}
int find(int x) {
int now = root, fa = ;
while() {
pushdown(now);
if(x <= size[ch[now][]])
now = ch[now][];
else {
x -= size[ch[now][]]+;
if(x == ) return now;
now = ch[now][];
}
}
}
void print(int now) {
pushdown(now);
if(ch[now][]) print(ch[now][]);
if(key[now]!=-INF && key[now]!=INF) ans[++tot] = key[now];
if(ch[now][]) print(ch[now][]);
}
int main() {
scanf("%d%d", &n, &m);
a[] = -INF; a[n+] = INF;
for(int i = ; i <= n+; i++) a[i] = i-;
root = build(, n+, );
while(m--) {
scanf("%d%d", &s, &t);
if(s == ) continue;
int f1 = find();
int f2 = find(s+t+);
splay(f1, );
splay(f2, f1);
delta[ch[ch[root][]][]] ^= ;
f2 = find(t+);
splay(f2, f1);
delta[ch[ch[root][]][]] ^= ;
f1 = find(t+);
f2 = find(s+t+);
splay(f1, );
splay(f2, f1);
delta[ch[ch[root][]][]]^=;
}
print(root);
for(int i = ; i < n; i++) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}
2018牛客多校第三场 C.Shuffle Cards的更多相关文章
- 牛客多校第三场 F Planting Trees
牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- 牛客多校第三场 A—pacm team (4维背包加路径压缩)
链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...
- 牛客多校第三场-A-PACM Team-多维背包的01变种
题目我就不贴了...说不定被查到要GG... 题意就是我们需要在P,A,C,M四个属性的限制下,找到符合条件的最优解... 这样我们就需要按照0/1背包的思路,建立一个五维度数组dp[i][j][k] ...
- 牛客多校第三场 A- PACM Team 背包/记忆路径
https://www.nowcoder.com/acm/contest/141#question 一眼背包,用四维dp记录在A,B,C,D条件限制下可以获得的最大知识点,但是题目要求输出路径,在输入 ...
- 2018牛客多校第六场 G.Pikachu
题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...
- 2018牛客多校第六场 I.Team Rocket
题意: 给出n个区间和m个点(点按顺序给出且强制在线).每个区间只会被第一个他包含的点摧毁.问每个点能摧毁多少个区间以及每个区间是被哪个点摧毁的. 题解: 将n个区间按照左端点排序,然后用vector ...
- 2018牛客多校第五场 H.subseq
题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...
- 2018牛客多校第五场 E.room
题意: 一共有n个宿舍,每个宿舍有4个人.给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动. 题解: 对于第一年的每个宿舍,向今年的每种组合连边.流量为1,费用为(4 - 组合中已在该宿 ...
随机推荐
- Solr简单总结
Solr 运行Solr服务 方式一:Jetty服务器启动Solr 进入solr-4.10.2/example目录 打开命令行,执行java –jar start.jar命令,即可启动Solr服务 打开 ...
- PHP(YII2实现) 微信网页授权
参考地址 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 实现步骤分析: 获取code->access ...
- Idea 2017 激活方法
http://www.cnblogs.com/suiyueqiannian/p/6754091.html
- web学习第一天
学习web心得 表格 table,表单 form,跑马灯效果 marquee,导入背景图片<img src="图片路径"> 第一天学的不是很难,作业也相对来说比较简单, ...
- Leecode刷题之旅-C语言/python-26.移除元素
/* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-elem ...
- Electronic Devices【电子设备】
Electronic Devices We may think we're a culture that gets rid of our worn technology at the first si ...
- rhel6.4 根目录扩容
状况:根目录容量不足 解决:扩容根目录 ====================================================== 解决步骤: 1. 将新的磁盘加入服务器 2. 使用 ...
- Python tips(
(此文是在实际工程中遇到的一些小问题,给予解决和整理.解决方法大多来自网上零散的文章.有一个系统化的Python问题解决方案,来自<Python 3 学习笔记>雨痕著,其中对Python的 ...
- Kubernetes-Envoy(一种全新的Ingress实现方式)
Ingress 在讲Envoy之前,先介绍一下Kubernetes中Service的表现形式为IP:Port,及工作在Ingress:TCP/IP层.而对于基于HTTP的服务来说,不同的URL地址经常 ...
- RHCE7认证学习笔记17——KickStart安装系统
一.自动化安装系统工具 1.Cobbler 另一个自动化安装工具: 2.Kickstart 二.使用kickstart自动化安装系统 服务器安装的软件: 1.dhcp服务 [root@lin ...