题意:

  给出一段序列,每次将从第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的更多相关文章

  1. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

  2. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  3. 牛客多校第三场 A—pacm team (4维背包加路径压缩)

    链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...

  4. 牛客多校第三场-A-PACM Team-多维背包的01变种

    题目我就不贴了...说不定被查到要GG... 题意就是我们需要在P,A,C,M四个属性的限制下,找到符合条件的最优解... 这样我们就需要按照0/1背包的思路,建立一个五维度数组dp[i][j][k] ...

  5. 牛客多校第三场 A- PACM Team 背包/记忆路径

    https://www.nowcoder.com/acm/contest/141#question 一眼背包,用四维dp记录在A,B,C,D条件限制下可以获得的最大知识点,但是题目要求输出路径,在输入 ...

  6. 2018牛客多校第六场 G.Pikachu

    题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...

  7. 2018牛客多校第六场 I.Team Rocket

    题意: 给出n个区间和m个点(点按顺序给出且强制在线).每个区间只会被第一个他包含的点摧毁.问每个点能摧毁多少个区间以及每个区间是被哪个点摧毁的. 题解: 将n个区间按照左端点排序,然后用vector ...

  8. 2018牛客多校第五场 H.subseq

    题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...

  9. 2018牛客多校第五场 E.room

    题意: 一共有n个宿舍,每个宿舍有4个人.给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动. 题解: 对于第一年的每个宿舍,向今年的每种组合连边.流量为1,费用为(4 - 组合中已在该宿 ...

随机推荐

  1. HTML基础全荟

    第一讲 html概述 1.认识HTML <! DOCTYPE html> <html> <style></style> <head>< ...

  2. MySQL实现排名并查询指定用户排名功能,并列排名功能

    MySQL实现排名并查询指定用户排名功能,并列排名功能 表结构: CREATE TABLE test.testsort ( id int(11) NOT NULL AUTO_INCREMENT, ui ...

  3. QQ群认证 人数再度扩容 权限随之升级

    群排名,得资源得天;之于排名,群容量有杠杆意义. 而今,流量分散,打法完全无法集中,全平台战略,越发凸显.QQ群,已是必争之地. 去年,Q群霸屏春天,一切那么顺其自然;而今,除了拼资源,还是拼资源.除 ...

  4. 微信小程序横向滚动

    <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" > ...

  5. mysql在cmd里中文乱码解决办法

    右边画红线部分中文已经乱码,左边红线里中文则完美显示出来了. 解决办法   用set  names   utf-8: 效果如图

  6. 第一个python代码

    # -*- coding:utf-8 -*- user = raw_input("请输入用户名") passwd = raw_input("请输入密码") if ...

  7. 分享一个工作中遇得到的sql(按每天每人统计拖车次数与小修次数)

    查询每人每天的数据 首先先建表 CREATE TABLE `user` ( `name` ) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CR ...

  8. 记一次MD5妙用

    记一次MD5妙用 最近项目组中在做历史记录的改造工作,主持讨论了多次,但每次讨论完都觉的很完美了,但实际在写这部分逻辑的时候还是会发现一些问题出来,很难受,反反复复的暴露智商是硬伤,人艰不拆,暂先不扯 ...

  9. Codeforces 845 C Two TVs

    参考:https://blog.csdn.net/xjh_shin/article/details/77491693 #include <iostream> #include <cs ...

  10. SIMD数据并行(四)——三种结构的比较

    在计算机体系中,数据并行有两种实现路径:MIMD(Multiple Instruction Multiple Data,多指令流多数据流)和SIMD(Single Instruction Multip ...