题目:http://codeforces.com/contest/1156/problem/E

题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l,r])

思路:首先我们去枚举区间肯定不现实,我们只能取把能用的区间去用,我们可以想下每个数当最大值的时候所做的贡献

我们既然要保证这个数为区间里的最大值,我们就要从两边扩展,找到左右边界能扩展在哪里,这里你直接去枚举肯定不行

这里我们使用了线段树二分去枚举左右区间最远能走到哪里,然后很暴力的去枚举短的那一边找出右边是否有满足条件的边界

#include<bits/stdc++.h>
#define maxn 200005
#define mod 1000000007
using namespace std;
typedef long long ll;
struct sss
{
int l,r;
int val;
}tree[maxn*];
int n;
int a[maxn];
int pos[maxn];
void build(int cnt,int l,int r)
{
if(l==r){
tree[cnt].l=l;
tree[cnt].r=r;
tree[cnt].val=a[l];
return;
}
tree[cnt].l=l;
tree[cnt].r=r;
int mid=(l+r)/;
build(cnt*,l,mid);
build(cnt*+,mid+,r);
tree[cnt].val=max(tree[cnt*].val,tree[cnt*+].val);
}
int query(int cnt,int l,int r)
{
if(l<=tree[cnt].l&&r>=tree[cnt].r) return tree[cnt].val;
int mid=(tree[cnt].l+tree[cnt].r)/;
if(r<=mid) return query(cnt*,l,r);
else if(l>mid) return query(cnt*+,l,r);
else{
return max(query(cnt*,l,mid),query(cnt*+,mid+,r));
} }
int dl(int x)
{
int val=a[x];
int l=;
int r=x;
while(r-l>){
int mid=(l+r)/;
int max_val=query(,mid,x); //如果满足这个区间的最大值是这个数就继续扩张
if(max_val==val)
{
r=mid;
}
else{
l=mid;
}
}
int max_val=query(,l,x);
if(max_val==val) return l;
else return r;
}
int dr(int x)
{
int val=a[x];
int l=x;
int r=n;
while(r-l>){
int mid=(l+r)/;
int max_val=query(,x,mid);
if(max_val==val)
{
l=mid;
}
else{
r=mid;
}
}
int max_val=query(,x,r);
if(max_val==val) return r;
else return l;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
build(,,n);
int ans=;
for(int i=;i<=n;i++){
int l=dl(i);//找出左边界
int r=dr(i);//找出右边界
if(i-l<r-i){//枚举短的那一边
for(int j=l;j<i;j++){
int other=a[i]-a[j];
if (other<=||other>n)continue;
ans+=pos[other]>i&&pos[other]<=r;//确定令一边是否在这个区间里面
}
}
else{
for(int j=i+;j<=r;j++){
int other=a[i]-a[j];
if (other<=||other>n)continue;
ans+=pos[other]<i&&pos[other]>=l;
}
}
}
printf("%d",ans);
}

Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)的更多相关文章

  1. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  2. 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 ...

  3. Educational Codeforces Round 64 (Rated for Div. 2)D(并查集,图)

    #include<bits/stdc++.h>using namespace std;int f[2][200007],s[2][200007];//并查集,相邻点int find_(in ...

  4. Educational Codeforces Round 61 (Rated for Div. 2)D(二分,模拟,思维)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;int n,k;ll a[200007],b[200007] ...

  5. Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)

    这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN #define ...

  6. Educational Codeforces Round 77 (Rated for Div. 2)D(二分+贪心)

    这题二分下界是0,所以二分写法和以往略有不同,注意考虑所有区间,并且不要死循环... #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...

  7. Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[20 ...

  8. Educational Codeforces Round 87 (Rated for Div. 2) D树状数组加二分删除的值

    Sample Input 5 4 1 2 3 4 5 -5 -1 -3 -1 Sample Output 3 思路,首先发现a[i]的值的范围是在1~n之间,每次插入我们可以直接把cnt[a[i]]+ ...

  9. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

随机推荐

  1. Linux下github的使用

    在linux下搭建git环境 1.创建Github账号,https://github.com 2.Linux创建SSH密钥: ssh-keygen ##一直默认就可以了 3.将公钥加入到Github账 ...

  2. [CSP-S模拟测试67]题解

    时隔多年,终于又有了一套我能改完的题…… A.神炎皇 遇到这种要求整除的题显然拆出gcd 设$d=gcd(a,b)\ \ \ a'=\frac{a}{d} \ \ \ b'=\frac{b}{d}$ ...

  3. Vue使用lib-flexible,将px转化为rem

    1.下载lib-flexible 我使用的是vue-cli+webpack,所以是通过npm来安装的 npm i lib-flexible --save 2.引入lib-flexible 在main. ...

  4. IE8 indexOf

    因为ie8中的js数组没有indexOf方法,所以使用之前要先加入这段js代码 if (!Array.prototype.indexOf) { Array.prototype.indexOf = fu ...

  5. spring restTemplate使用方法

    https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...

  6. Redis 5.0.7 讲解,单机、集群模式搭建

    Redis 5.0.7 讲解,单机.集群模式搭建 一.Redis 介绍 不管你是从事 Python.Java.Go.PHP.Ruby等等... Redis都应该是一个比较熟悉的中间件.而大部分经常写业 ...

  7. 用 Flask 来写个轻博客 (14) — M(V)C_实现项目首页的模板

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 实现所需要的视图函数 实现 home.html 模板 代码分析 实现效 ...

  8. excel数据生成sql insert语句

    excel数据生成sql insert语句 excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age . 在你的excel表格中增加一列,利用ex ...

  9. 初步认识AutoMapper转载 https://www.cnblogs.com/fred-bao/p/5700776.html

    初步认识AutoMapper AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象   前言 通常 ...

  10. 导数与偏导数 Derivative and Partial Derivative

    之前做了很长时间“罗辑思维”的听众,罗胖子曾经讲起过,我们这一代人该如何学习.其中,就讲到我们这个岁数,已经不可能再去从头到尾的学习一门又一门工具课程了,而是在学习某一领域时,有目的的去翻阅工具课程中 ...