#Splay#U137476 序列
题目
给定长度为\(n\)的序列\(Ai\) ,我们将按照如下操作给\(Ai\) 排序,
先找到编号最小的所在位置\(x1\) ,将\([1,x1]\) 翻转,
再找到编号第二小的所在位置\(x2\) ,将\([1,x2]\) 翻转,
如果有相同的\(Ai\) ,则按照输入顺序操作。输出所有的\(xi\) 。
Sample Input:
6
3 4 5 1 6 2
Sample Output:
4 6 4 5 6 6
分析
Splay裸题,但赛时不会Splay,wtcl
这道题的关键就是记录每个数的在Splay中的编号,
要注意相同的数按照输入顺序操作,所以不能够直接建树,要一个个插入
那么将操作的编号按照数的大小排序,相同编号小则优先,那么得到编号就很容易寻找排名了
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
const int inf=0x7fffffff,N=100011; int b[N],ans[N],n;
inline signed iut(){
rr int ans=0,f=1; rr char c=getchar();
while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans*f;
}
inline void print(int ans){
if (ans<0) putchar('-'),ans=-ans;
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
struct Splay{
int siz[N],lazy[N],cnt[N],son[N][2],fat[N],w[N],root,tot;
inline void pup(int x){siz[x]=siz[son[x][0]]+siz[son[x][1]]+cnt[x];}
inline bool Is_R(int x){return son[fat[x]][1]==x;}
inline void pdown(int x){
if (x&&lazy[x]){
lazy[son[x][0]]^=1,lazy[son[x][1]]^=1;
swap(son[x][0],son[x][1]),lazy[x]=0;
}
}
inline void rotate(int x){
rr int Fa=fat[x],FFa=fat[Fa],wh=Is_R(x);
son[FFa][Is_R(Fa)]=x,fat[x]=FFa,son[Fa][wh]=son[x][wh^1];
fat[son[x][wh^1]]=Fa,son[x][wh^1]=Fa,fat[Fa]=x,pup(Fa),pup(x);
}
inline void update(int x,int tar){
if (x==tar) return;
update(fat[x],tar),pdown(x);
}
inline void splay(int x,int tar){
update(x,tar);
for (;fat[x]!=tar;rotate(x)){
rr int Fa=fat[x],FFa=fat[Fa];
if (FFa!=tar) rotate((Is_R(x)^Is_R(Fa))?x:Fa);
}
if (!tar) root=x;
}
inline signed rank(int x){
splay(x,0);
return siz[son[root][0]];
}
inline signed kth_site(int rk){
rr int now=root;
if (siz[now]<rk) return -1;
while (1){
pdown(now);
rr int lson=son[now][0];
if (siz[lson]+cnt[now]<rk)
rk-=siz[lson]+cnt[now],now=son[now][1];
else if (rk<=siz[lson]) now=son[now][0];
else break;
}
splay(now,0);
return now;
}
inline void Invert(int L,int R){
rr int l=kth_site(L-1),r=kth_site(R+1);
splay(l,0),splay(r,l);
lazy[son[son[root][1]][0]]^=1;
}
}Tre;
bool cmp(int x,int y){return (Tre.w[x]^Tre.w[y])?Tre.w[x]<Tre.w[y]:x<y;}
signed main(){
n=iut(),Tre.w[1]=-inf,Tre.w[n+2]=inf;
for (rr int i=2;i<n+2;++i) Tre.w[i]=iut();
for (rr int i=1;i<=n+2;++i)
Tre.son[i-1][1]=i,Tre.fat[i]=i-1,Tre.lazy[i]=0,
b[i]=i,Tre.cnt[i]=1,Tre.pup(i);
Tre.root=n+2,sort(b+1,b+2+n,cmp);
for (rr int i=2;i<n+2;++i){
rr int l=i,r=Tre.rank(b[i])+1;
Tre.Invert(l,r),ans[i-1]=r-1;
}
for (rr int i=1;i<=n;++i) print(ans[i]),putchar(i==n?10:32);
return 0;
}
#Splay#U137476 序列的更多相关文章
- BZOJ 1251 Splay维护序列
思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...
- 【模板】splay维护序列
题目大意:维护一个长度为 N 的序列,支持单点插入,单点询问. 注意事项如下: build 函数中要记得初始化 fa. 插入两个端点值. 代码如下 #include <bits/stdc++.h ...
- splay树 序列终结者
/* 4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 网上有许多题,就是给定一个序 ...
- BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系
题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
- Splay模板(序列终结者)
我只是一个存模板的,详细的请看这里http://blog.csdn.net/whai362/article/details/47298133 题目链接:http://www.codevs.cn/pro ...
- BZOJ 3323 splay维护序列
就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ...
- UVA 11996 Jewel Magic —— splay、序列的分裂与合并、LCP的哈希算法
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- 伸展树Splay
新学的,其实吧,就那么回事.... 看了几天,splay处理序列问题,真的非常厉害,翻转,插入,删除,线段树实现不了的功能,splay用起来很方便. POJ 3580 SuperMemo 这题基本就是 ...
- 【BZOJ2809】【splay启发式合并】dispatching
Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...
随机推荐
- 统信UOS系统开发笔记(二):国产统信UOS系统搭建Qt开发环境安装Qt5.12
前言 开发国产应用,使用到统信UOS系统,安装Qt5.12.8的Qt开发安装包直接安装(这是本篇使用的方式,另外一种源码编译安装将在下一篇讲解) 统信UOS系统版本 系统版本: Q ...
- Java 方法的重载(overload)
1 /* 2 * 3 * 方法的重载(overload) 4 * 1.定义:在同一个类中,允许存在一个以上的同名方法,只要他们的参数个数或者参数类型不同 5 * 6 * "两同一不同&quo ...
- Codeforces Round 926 (Div. 2)(A~D)
目录 A B C D A 输出最大值减最小值,或者排序算一下答案 #include <bits/stdc++.h> #define int long long #define rep(i, ...
- python AI应用开发编程实战 大模型实战基础(数据存储类型列表与字典)(二)
大模型开发中,需要和自己的业务融合,我们要对自己的数据处理,熟悉外理excle word pdf 数据然后处理后可以放到向量数据库,或者直接Assistants API传到大模型引用,不管怎么样数 ...
- vue table 里面 slot 的模板复用 slot-scope template v-for
vue table 里面 slot 的模板复用 slot-scope template v-for 需求 经常在table里面要有自定义列,但是会有相同的自定义列,这个时候又不想写很多一样的templ ...
- dbvisualizer之编辑区中文乱码问题
!在SQL Commander中,sql语句中如果有中文,显示是'口口口'. 解决办法如下: 在Tools->tool Properties->General->Appearance ...
- C++多态底层原理:虚函数表
虚函数表 C++ 对象模型 在有虚函数的情况下,C++对象的模型可以概括为:虚函数表指针+数据struct.在对象所在的内存里:前8个字节(64位系统)是虚函数表所在地址,后边是对象中的member ...
- Spring Boot自动运行之 CommandLineRunner、ApplicationRunner和@PostConstruct
在使用Spring Boot开发的工作中,我们经常会需要遇到一种功能需求,比如在服务启动时候,去加载一些配置,去请求一下其他服务的接口.Spring Boot给我们提供了三种常用的实现方法: 第一种是 ...
- 【C语言复习笔记】一些要点
[C语言复习笔记]一些要点 按学校教材复习的,整理的是我不熟悉的地方 最近C用的好少,快忘完了就赶紧整理一下(Python真好玩) 第一章 初识C语言 存储器 内存容量的大小,取决于地址总线的数量 \ ...
- 为什么ASP.NET Core的路由处理器可以使用一个任意类型的Delegate
毫不夸张地说,路由是ASP.NET Core最为核心的部分.路由的本质就是注册一系列终结点(Endpoint),每个终结点可以视为"路由模式"和"请求处理器"的 ...