牛客网多校第3场 C-shuffle card 【splay伸展树】
题目链接:戳这里
转自:戳这里
关于splay入门:戳这里
题意:给n个数,进行m次操作,每次都从n个数中取出连续的数放在最前面。
解题思路:splay的区间操作。
附代码:
1 #include<iostream>
2 #include<stdio.h>
3 using namespace std;
4 int n,m,sz,rt;
5 int fa[100005],c[100005][2],id[100005];
6 int size[100005];
7 bool rev[100005];
8 void pushup(int k)
9 {
10 int l=c[k][0],r=c[k][1];
11 size[k]=size[l]+size[r]+1;
12 }
13 void pushdown(int k)
14 {
15 int l=c[k][0],r=c[k][1];
16 if(rev[k])
17 {
18 swap(c[k][0],c[k][1]);
19 rev[l]^=1;rev[r]^=1;
20 rev[k]=0;
21 }
22 }
23 void rotate(int x,int &k)
24 {
25 int y=fa[x],z=fa[y],l,r;
26 if(c[y][0]==x)l=0;else l=1;r=l^1;
27 if(y==k)k=x;
28 else {if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;}
29 fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
30 c[y][l]=c[x][r];c[x][r]=y;
31 pushup(y);pushup(x);
32 }
33 void splay(int x,int &k)
34 {
35 while(x!=k)
36 {
37 int y=fa[x],z=fa[y];
38 if(y!=k)
39 {
40 if(c[y][0]==x^c[z][0]==y)rotate(x,k);
41 else rotate(y,k);
42 }
43 rotate(x,k);
44 }
45 }
46 int find(int k,int rank)
47 {
48 pushdown(k);
49 int l=c[k][0],r=c[k][1];
50 if(size[l]+1==rank)return k;
51 else if(size[l]>=rank)return find(l,rank);
52 else return find(r,rank-size[l]-1);
53 }
54 void rever(int l,int r)
55 {
56 int x=find(rt,l),y=find(rt,r+2);
57 splay(x,rt);splay(y,c[x][1]);
58 int z=c[y][0];
59 rev[z]^=1;
60 }
61 void build(int l,int r,int f)
62 {
63 if(l>r)return;
64 int now=id[l],last=id[f];
65 if(l==r)
66 {
67 fa[now]=last;size[now]=1;
68 if(l<f)c[last][0]=now;
69 else c[last][1]=now;
70 return;
71 }
72 int mid=(l+r)>>1;now=id[mid];
73 build(l,mid-1,mid);build(mid+1,r,mid);
74 fa[now]=last;pushup(mid);
75 if(mid<f)c[last][0]=now;
76 else c[last][1]=now;
77 }
78 int main()
79 {
80 scanf("%d%d",&n,&m);
81 for(int i=1;i<=n+2;i++)
82 id[i]=++sz;
83 build(1,n+2,0);rt=(n+3)>>1;
84 for(int i=1;i<=m;i++)
85 {
86 int l,r;
87 scanf("%d%d",&l,&r);
88 rever(1,l+r-1); //区间通过3次翻转,达到洗牌效果
89 rever(1,r);
90 rever(r+1,l+r-1);
91 }
92 for(int i=2;i<=n+1;i++)
93 printf("%d ",find(rt,i)-1);
94 return 0;
95 }
牛客网多校第3场 C-shuffle card 【splay伸展树】的更多相关文章
- 牛客网多校第3场C-shuffle card 平衡树或stl(rope)
链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...
- 牛客网多校第3场Esort string (kmp)
链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...
- 牛客网多校赛第九场A-circulant matrix【数论】
链接:https://www.nowcoder.com/acm/contest/147/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
- 牛客网多校第5场 H subseq 【树状数组+离散化】
题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...
- 牛客网多校第5场 I vcd 【树状数组+离散化处理】【非原创】
题目:戳这里 学习博客:戳这里 作者:阿狸是狐狸啦 n个点,一个点集S是好的,当且仅当对于他的每个子集T,存在一个右边无限延长的矩形,使的这个矩形包含了T,但是和S-T没有交集. 求有多少个这种集合. ...
- 牛客网多校第4场 J Hash Function 【思维+并查集建边】
题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...
- 牛客网多校第4场 A.Ternary String 【欧拉降幂】
题目:戳这里 学习博客:戳这里 欧拉函数的性质: ① N是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身) ② 除了N=2,φ(N)都是偶数. ③ 小于N且与N互质的所有数的和是φ(n)*n/ ...
- 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)
链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
随机推荐
- 安装python性能检测工具line_profiler
line_profiler是一款监测python的CPU密集型性能问题的强大工具,可以对函数进行逐行分析,在linux上安装时一切正常,然而今天在win10 64位系统安装失败了 pip3 insta ...
- MyBatis初级实战之六:一对多关联查询
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- canvas星空背景特效+CSS旋转相册学习
今天在看帖子的时候,看到了个有趣的css旋转相册,刚好之前做了一个星空背景dome,这里给大家分享下代码: 旋转相册参考:https://blog.csdn.net/gitchatxiaomi/art ...
- 一键测试VPS到国内速度脚本 SuperBench.sh,以及一键验收云主机脚本
我们买国外VPS服务器测试网络通常会用到speedtest,speedtest默认是测试到最近的节点,那么到国内速度如何呢?虽然可以指定服务器编号,但是一个个测试还是比较麻烦的,这里推荐一个脚本整合了 ...
- map 传递给函数的代价
https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/08.1.md map 传递给函数的代价很小:在 32 位机器上占 4 ...
- OpenSSL 常见对称加密算法特性分析
在选择加密算法,面对一大长串的选项时,大家都有这样的疑问,究竟哪种加密方式是最好的呢? 对于加密方式.算法来说,一般安全性与性能呈负相关,越是安全的,对性能要求则更高. 现在主流的加密协议的安全性均能 ...
- [Python]编码声明:是coding:utf-8还是coding=utf-8呢
PEP 263 -- Defining Python Source Code Encodings | Python.org https://www.python.org/dev/peps/pep-02 ...
- SpringBoot Web 学习
SpringBoot Web 开发 静态资源 打开WebMvcAutoConfiguration类里面的静态类WebMvcAutoConfigurationAdapter里面的addResourceH ...
- Eclipse+Maven+Spring
1.首先按照how2j教程搭建Maven项目:http://how2j.cn/k/maven/maven-eclipse-maven-project/1332.html 2. 刚建好时没有资源文件夹的 ...
- 十:SpringBoot-配置AOP切面编程,解决日志记录业务
SpringBoot-配置AOP切面编程,解决日志记录业务 1.AOP切面编程 1.1 AOP编程特点 1.2 AOP中术语和图解 2.SpringBoot整合AOP 2.1 核心依赖 2.2 编写日 ...