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 ...
随机推荐
- ruby puts语法
str = "Welcom to china" str1 = str puts str + " 1" puts str1 + " 1" de ...
- hibernate 双向1对多
1: 还是用客户Customer和订单Order来解释: “一对多”的物理意义:一个客户可以有多个订单,某个订单只能归宿于一个客户. “双向”的物理意义:客户知道自己有哪些订单,订单也知道自己归宿于哪 ...
- 大碗宽面Alpha冲刺阶段博客目录
大碗宽面Alpha冲刺阶段博客目录 一.Scrum Meeting 1. [第六周会议记录]第六周链接 2. [第七周会议记录]第七周链接 二.测试报告 [alpha阶段测试报告](博客链接) ## ...
- <自动化测试>之<selenium API 查找元素操作底层方法>
搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏driver = webdriver.Chrome(); 打开测试页面 driver.get( "http://b ...
- if语句里面continue和break的区别
break:结束整个循环体 continue:结束本次循环 代码说明: public static void main(String[] args) { int x=0; while(x++ < ...
- numpy 中文手册
https://yiyibooks.cn/xx/NumPy_v111/user/index.html
- 用 Flask 来写个轻博客 (15) — M(V)C_实现博文页面评论表单
目录 目录 前文列表 实现 post 视图函数 在 posthtml 中添加表单 效果 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask 来写个轻博客 (2) - Hell ...
- linux shell设置颜色
使用echo或者printf时,可以添加输出文本的颜色设置 echo -e "Maximum \e[1;31m" $max_threads "\e[0mthreads a ...
- 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...
- 数据库(二)—— MySQL索引优化
目录 MySQL的索引优化 一.MySQL 5.7的初始化配置 二.MySQL配置文件 1.配置 2.配置文件作用 三.多实例 1.创建相关的目录 2.创建实例的配置文件 3.初始化 4.授权 5.启 ...