题目: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. 开放融合 | “引擎级”深度对接!POLARDB与SuperMap联合构建首个云原生时空平台

    阿里巴巴新一代自研云数据库POLARDB与超图软件SuperMap GIS实现 “引擎级”深度对接,构建了自治.弹性.高可用的云原生时空数据管理平台联合解决方案,推出了业界首个“云原生数据库+云原生G ...

  2. 阿里云异构计算团队亮相英伟达2018 GTC大会

    摘要: 首届云原生计算国际会议(KubeCon + CloudNativeCon,China,2018)在上海举办,弹性计算研究员伯瑜介绍了基于虚拟化.容器化编排技术的云计算操作系统PouchCont ...

  3. POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  4. .net文件下载的四种方法

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  5. BZOJ 3653: 谈笑风生(主席树)

    传送门 解题思路 首先对于一个\(a\)来说,要求\(b\)和\(c\),那么\(a,b,c\)一定在一条链上.把\(b\)分类讨论,如果\(b\)是\(a\)的祖宗,这个方案数就很好统计了,就是\( ...

  6. 20175213 2018-2019-2 《Java程序设计》第10周学习总结

    Java内存模型 主内存与工作内存 Java内存模型主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节.此处的变量(Variable)与Java编程中 ...

  7. ROS录制主题和放

    1.按照指定名称录制指定主题, 如录制主题为line_markers,名称为line_extraction的bag包. rosbag record -O line_extraction.bag /li ...

  8. Openstack组件部署 — Keystone Install & Create service entity and API endpoints

    目录 目录 前文列表 Install and configure Prerequisites 先决条件 Create the database for identity service 生成一个随机数 ...

  9. C++——右值引用

    1.绑定到一个将要销毁的右值的引用——移动 2.左值表达式——对象的身份:右值表达式——对象的值 ; int& r=i; int&& rr=i;//×,左值不能绑定到右值引用 ...

  10. Codeforces 388C Fox and Card Game (贪心博弈)

    Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...