题目: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. python 反转列表的3种方式

    转载自:https://blog.csdn.net/bookaswine/article/details/42468735 方式一:使用reversed()函数 a=[1,2,3,4,5,6,7,8, ...

  2. 原生js实现拖拽效果

    面向对象 + 原生js拖拽 拖拽div等盒子模型,都是日常操作没有什么问题,如果是拖拽图片的话,会有一点小坑要踩...... 那么我们看代码: var Move_fn = {}; (function( ...

  3. Backdoor CTF 2013 :电子取证250

    0x00题目 h4x0r厌烦了你对他的城堡的所有攻击,所以他决定报复攻击你,他给你发来一封带有图片的邮件作为警告,希望你能找出他的警告消息:-) 消息的MD5值就是flag. 图片如下: 0x01解题 ...

  4. linux SMbus错误

    针对piix4_smbus ****host smbus controller not enabled的解决方法 查看文件并用超级权限修改内容 在末尾加入blacklist i2c——piix4 重启 ...

  5. 3.Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)

    上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行成功了. 那是因为我们只是发起了测试,但并没有对每次请求测试的返回作校验. 所以 ...

  6. 转 jmeter 等待时间 pacing think time

    第一部分:Request之间的等待时间的设置 先明确一些概念:1)定时器是在每个sampler(采样器)之前执行的,而不是之后:是的,你没有看错,不管这个定时器的位置放在sampler之后,还是之下, ...

  7. position: relative 和 position: absoution 的详解

    position属性指定一个元素(静态的,相对的,绝对或固定)的定位方法的类型 relative:生成相对定位的元素,相对于其正常位置进行定位. 对应下图的偏移 absolute: 生成绝对定位的元素 ...

  8. Debug模式的三种配置方法

    使用`app.config.from_object`的方式加载配置文件: 1. 导入`import config`.2. 使用`app.config.from_object(config)`. ### ...

  9. linux每日命令(4):解压命令

    1) Ubuntu 16.04 已经自动安装了unzip 软件,解压命令: unzip FileName.zip 2) 如果没有安装unzip,可以使用下面的命令安装: sudo apt instal ...

  10. HTTP、HTTP1.0、HTTP1.1、HTTP2.0、HTTPS

      一.HTTP HTTP(超文本传输协议,HyperText Transfer Protocol)是应用层的协议,目前在互联网中应用广泛. 它被设计用于Web浏览器和Web服务器之间的通信,但它也可 ...