题意:

  给出一段序列,每次将从第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. Python练习笔记(2)

    文件读写,多线程.多进程 import time,os,threading,random def file_read(path): try: with open(path, 'r') as f: # ...

  2. python3 练习题100例 (十一)

    题目十一:举例证明角谷猜想:以一个正整数N为例,如果N为偶数,就将它变为N/2,如果除后变为奇数,则将它乘3加1(即3N+1).不断重复这样的运算,经过有限步后,一定可以得到1. #!/usr/bin ...

  3. queue消息队列

    class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out  class queue. ...

  4. 毕业2年 Summary

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/135 看了下去年写毕业一周年总结的时间:2017-6-16,今天 ...

  5. Python3爬虫(五)解析库的使用之XPath

    Infi-chu: http://www.cnblogs.com/Infi-chu/ XPath: 全称是 XML Path Language,XML路径语言,它是一门在XML文档中和HTML文档中查 ...

  6. [BZOJ2809][Apio2012]dispatching(左偏树)

    首先对于一个节点以及它的子树,它的最优方案显然是子树下选最小的几个 用左偏树维护出每棵子树最优方案的节点,记录答案 然后它的这棵树可以向上转移给父节点,将所有子节点的左偏树合并再维护就是父节点的最优方 ...

  7. ajax同步和异步的切换

    ajax为网页提供了非常不错的异步机制,但是有时候两个ajax放在一起,希望第一个完成后再继续第二个ajax的执行.这时候可以将第一个ajax代码带上同步参数即可,如下: $.ajax({ async ...

  8. 一步一步学Linq to sql(三):增删改

    示例数据库 字段名 字段类型 允许空 字段说明 ID uniqueidentifier 表主键字段 UserName varchar(50) 留言用户名 PostTime datetime 留言时间 ...

  9. 1977: [BeiJing2010组队]次小生成树 Tree

    1977: [BeiJing2010组队]次小生成树 Tree https://lydsy.com/JudgeOnline/problem.php?id=1977 题意: 求严格次小生成树,即边权和不 ...

  10. bzoj 一些题目汇总

    2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...