CF1156E Special Segments of Permutation
思路:笛卡尔树?(好像并不一定要建出来,但是可以更好理解)
提交:2次
错因:没有判左右儿子是否为空来回溯导致它T了
题解:
建出笛卡尔树,考虑如何计算答案:
先预处理每一个值出现的位置 \(pos[]\);
对于每一个有左右儿子的点,设他在原序列中的值为 \(mx\),根据笛卡尔树的性质,他比自己的子树中的任何一个元素都大 。这样, 我们遍历他的轻儿子中的元素 \(vl\) ,查询 \(pos[mx-vl]\) 是否在重子树中。
其实可以不建树,直接求出每个点作为最大值能够向左右扩展的区间,枚举小的区间就够了。
复杂度 \(O(nlogn)\) ,原因是类似树剖,每个点最多只会向上跳 \(logn\) 条轻边;而一个点被计算,只有在枚举轻子树的时候;其实类似dsu on tree。
当然,不建树的做法的复杂度虽然解释不同,但本质都是一样的、
代码:
#include<bits/stdc++.h>
#define R register int
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*f;
} const int N=250010;
int n,rt,a[N],pos[N],ans;
struct node {int ls,rs,sz,l,r;} t[N];
#define ls(tr) t[tr].ls
#define rs(tr) t[tr].rs
#define sz(tr) t[tr].sz
#define l(tr) t[tr].l
#define r(tr) t[tr].r
int stk[N],top;
inline void calc(int tr,int rn,int mx) {
for(R i=l(tr);i<=r(tr);++i)
ans+=(pos[mx-a[i]]>=l(rn)&&pos[mx-a[i]]<=r(rn));
}
inline void dfs(int tr) {
sz(tr)=1,l(tr)=r(tr)=tr;
if(ls(tr)) dfs(ls(tr)),l(tr)=l(ls(tr));
if(rs(tr)) dfs(rs(tr)),r(tr)=r(rs(tr));
if(!ls(tr)||!rs(tr)) return ;
sz(tr)=sz(ls(tr))+sz(rs(tr));
if(sz(ls(tr))<sz(rs(tr))) calc(ls(tr),rs(tr),a[tr]);
else calc(rs(tr),ls(tr),a[tr]);
}
inline void main() {
n=g(); for(R i=1;i<=n;++i) a[i]=g(),pos[a[i]]=i;
stk[++top]=0,a[0]=1e9;
for(R i=1;i<=n;++i) { R lst=0;
while(a[stk[top]]<a[i]) lst=stk[top],--top;
ls(i)=lst,rs(stk[top])=i; stk[++top]=i;
} rt=stk[2];
dfs(rt); printf("%d\n",ans);
}
} signed main() {Luitaryi::main(); return 0;}
2019.09.15
61
CF1156E Special Segments of Permutation的更多相关文章
- Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)
题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...
- codeforces 1156E Special Segments of Permutation
题目链接:https://codeforc.es/contest/1156/problem/E 题目大意: 在数组p中可以找到多少个不同的l,r满足. 思路: ST表+并查集. ST表还是需要的,因为 ...
- Codeforces 1156E Special Segments of Permutation(单调栈)
可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...
- Codeforces 1156E Special Segments of Permutation(启发式合并)
题意: 给一个n的排列,求满足a[l]+a[r]=max(l,r)的(l,r)对数,max(l,r)指的是l到r之间的最大a[p] n<=2e5 思路: 先用单调栈处理出每个点能扩展的l[i], ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 64 (Rated for Div. 2) A,B,C,D,E,F
比赛链接: https://codeforces.com/contest/1156 A. Inscribed Figures 题意: 给出$n(2\leq n\leq 100)$个数,只含有1,2,3 ...
- Educational Codeforces Round 64 选做
感觉这场比赛题目质量挺高(A 全场最佳),难度也不小.虽然 unr 后就懒得打了. A. Inscribed Figures 题意 给你若干个图形,每个图形为三角形.圆形或正方形,第 \(i\) 个图 ...
- Assembly Experiment5
Answer to the experiment(1),(2),(3),(4) Experiment(5): Screenshots&Results: from the command u w ...
随机推荐
- SAS学习笔记32 select语句
- 2019杭电多校一 A. Blank (dp)
大意: 长为$n$的数组, 每个位置范围$[0,3]$, $m$个限制$(l,r,x)$表示$[l,r]$内有$x$种数, 求方案数. 维护每个数字最后一次出现位置, 暴力$DP$ 实现时有个技巧是把 ...
- ASM实例远程连接
存在一个软件,远程连接ASM实例 tj2:/picclife/app/grid$ lsnrctl status Listening Endpoints Summary... (DESCRIPTION= ...
- 箭头函数的arguments不可用
ES5中的arguments function func(a,b,c){ console.log(arguments[0],arguments[1],arguments[2]) } func(1,2, ...
- (七)Action之ActionContext(OGNL表达式的使用)
一.ActionContext的重要性 struts中的数据都存放在ActionContext里,所以这部分是Action中的核心. ActionContext又称广义值栈,既然有广义值栈就有侠义值栈 ...
- JS OOP -03 JS类的实现
JS类的实现: a.理解类的实现机制 b.使用prototype对象定义类成员 c.一种JS类的设计模式 a.理解类的实现机制 在JS中可以使用function关键字来定义一个类. 添加类的成员,在函 ...
- requests Use body.encode('utf-8') if you want to send it encoded in UTF-8
基本环境 使用 requests 模块发送 post 请求,请求体包含中文报错 系统环境:centos7.3 python版本:python3.6.8 请求代码: // 得到中文 param_json ...
- vue 钩子函数的初接触
vue-router的路由钩子函数: 第一种:全局钩子函数. router.beforeEach((to, from, next) => { console.log('beforeEach') ...
- stm32 printf重定向
printf函数调用fputc int fputc(int ch, FILE *p) { USART_SendData(USART1, ch); //重定向到串口 while(USART_GetFla ...
- Android Studio 证书问题
彻底解决unable to find valid certification path to requested target 转载: https://www.cnblogs.com/Anderson ...