Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目: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) (线段树二分)的更多相关文章
- 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 (Rated for Div. 2)D(并查集,图)
#include<bits/stdc++.h>using namespace std;int f[2][200007],s[2][200007];//并查集,相邻点int find_(in ...
- 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] ...
- Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)
这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN #define ...
- Educational Codeforces Round 77 (Rated for Div. 2)D(二分+贪心)
这题二分下界是0,所以二分写法和以往略有不同,注意考虑所有区间,并且不要死循环... #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...
- 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 ...
- 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]]+ ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
随机推荐
- c#发送邮件功能
protected void Page_Load(object sender, EventArgs e) { //先到qq邮箱设置中启用smtp服务 Random r ...
- vue常见面试题
什么是 mvvm? MVVM 是 Model-View-ViewModel 的缩写.mvvm 是一种设计思想.Model 层代表数据模型,也可以在 Model 中定义数据修改和操作的业务逻辑:View ...
- js打印窗口内容并当窗口内容较长时自动分页
项目环境Angular: 方法1.window.print() HTML页面上的代码: <div id="tenementBillTable" class="dia ...
- selenium IDE 安装环境配置
- scrapy原理
scarpy据说是目前最强大的爬虫框架,没有之一.就是这么自信. 官网都是这么说的. An open source and collaborative framework for extracting ...
- java集合框架面试要点整理
- CenterOS 设置静态IP
本文主要介绍这样再CenterOS 中设定静态IP. 工具 centerOS 6.9 步骤 执行命令:vi /etc/sysconfig/network-scripts/ifcfg-eth0 编辑,填 ...
- python正常时间和unix时间戳相互转换的方法
python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...
- [c#源码分享]TCP通信中的大文件传送
NetworkComms网络通信框架序言 源码 (为节省空间,不包含通信框架源码,通信框架源码请另行下载) 文件传送在TCP通信中是经常用到的,本文针对文件传送进行探讨 经过测试,可以发送比较大的 ...
- UVA
A network is composed of N computers connected by N - 1 communication links such that any two comput ...