题意:

给一个n的排列,求满足a[l]+a[r]=max(l,r)的(l,r)对数,max(l,r)指的是l到r之间的最大a[p]

n<=2e5

思路:

先用单调栈处理出每个点能扩展的l[i],r[i]

搜索以每个点为最大值时的贡献,对每个点只搜索它左边部分或右边部分最小的那个

可以证明,每个点最多被搜到logn次,类似于启发式合并的思想,

复杂度为nlogn

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#define LLONG_MAX 9223372036854775807 using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 1e5+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int a[maxn];
int l[maxn],r[maxn];
int idx[maxn];
ll ans = ;
void gao(int x){
int L,R;
int pL,pR;
if(x-l[x]<r[x]-x){
L=l[x];
R=x-;
pL=x+;
pR=r[x];
}
else{
pL=l[x];
pR=x-;
L=x+;
R=r[x];
}
for(int i = L; i <= R; i++){
int y = a[x]-a[i];
if(idx[y]>=pL&&idx[y]<=pR)ans++;
}
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
idx[a[i]]=i;
l[i] = r[i] = i;
}
a[]=a[n+]=n+;
for(int i = ; i <= n; i++){
while(a[l[i]-] <= a[i]){
l[i] = l[l[i]-];
}
}
for(int i = n; i >= ; i--){
while(a[r[i]+] <= a[i]){
r[i] = r[r[i]+];
}
}
ans = ;
for(int i = ; i <= n; i++){
gao(i);
}
printf("%lld",ans);
return ;
}

Codeforces 1156E Special Segments of Permutation(启发式合并)的更多相关文章

  1. codeforces 1156E Special Segments of Permutation

    题目链接:https://codeforc.es/contest/1156/problem/E 题目大意: 在数组p中可以找到多少个不同的l,r满足. 思路: ST表+并查集. ST表还是需要的,因为 ...

  2. Codeforces 1156E Special Segments of Permutation(单调栈)

    可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...

  3. Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)

    题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...

  4. Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map

    E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...

  5. Codeforces 1455G - Forbidden Value(map 启发式合并+DP)

    Codeforces 题面传送门 & 洛谷题面传送门 首先这个 if 与 end 配对的结构显然形成一个树形结构,考虑把这棵树建出来,于是这个程序的结构就变为,对树进行一遍 DFS,到达某个节 ...

  6. Codeforces 208E - Blood Cousins(树上启发式合并)

    208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...

  7. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  8. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  9. CodeForces - 778C: Peterson Polyglot (启发式合并trie树)

    Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...

随机推荐

  1. 深度学习论文翻译解析(六):MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications

    论文标题:MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications 论文作者:Andrew ...

  2. 21.pyinstaller相关参数

    pyinstaller相关参数                  命令                                                          描述  -F, ...

  3. 我该如何学习spring源码以及解析bean定义的注册

    如何学习spring源码 前言 本文属于spring源码解析的系列文章之一,文章主要是介绍如何学习spring的源码,希望能够最大限度的帮助到有需要的人.文章总体难度不大,但比较繁重,学习时一定要耐住 ...

  4. Linux学习_菜鸟教程_4

    Linux远程登录 已经了解了登录流程,学会了用SecureCRT进行操作 Linux文件基本属性 Linux为保护系统安全,对不同的用户,开放不同的文件访问权限. 在linux中,我们可以使用ll或 ...

  5. Fabric1.4:链码管理与测试

    1 链码介绍 智能合约在 Hyperledger Fabric 中称为链码(chaincode),是提供分布式账本的状态处理逻辑.链码被部署在fabric 的网络节点中,能够独立运行在具有安全特性的受 ...

  6. 【转】在Ubuntu下建立Eclipse的Android开发环境

    本文将介绍如何建立Ubuntu下基于Eclipse的Android开发环境的方法. 大部分的Android开发者都是使用Eclipse来开发Android,本文将向各位介绍一下建立Ubuntu下基于E ...

  7. LoopBox 用于包装循环的盒子

    /******************************************************* * * 作者:朱皖苏 * 创建日期:20180608 * 说明:此文件只包含一个类,具 ...

  8. git 工作中实用合并分支

    合并分支 .克隆远程分支 git clone -b dev1. url .创建本地分支,并关联远程分支 git checkout -b dev_wt orgin/dev_wt .合并某分支到当前分支 ...

  9. gitbub 基本使用

    一.环境 git:https://git-scm.com/ 申请github账号:https://github.com/ 二.安装git 一直next即可 三.创储存建库 1.选择New reposi ...

  10. limit_choices_to

    # 多对多 老师 teachers = models.ManyToManyField(verbose_name='任课老师', to='UserInfo', related_name="ab ...