Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences
题目连接:
http://www.codeforces.com/contest/689/problem/D
Description
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of while !Mike can instantly tell the value of .
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs is satisfied.
How many occasions will the robot count?
Input
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.
Output
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied
Sample Input
6
1 2 3 2 1 4
6 7 1 2 3 2
Sample Output
2
Hint
题意
给你一个a数组和一个b数组
问你有多少对(l,r)满足,a数组中max(L,R)恰好等于b数组中的min(L,R)
题解
暴力枚举L,然后二分相等的那个区间就好了。
因为max肯定是递增的,min是递减的
那个相等的区间可以二分出来。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n;
int a[maxn],b[maxn];
struct RMQ{
const static int RMQ_size = maxn;
int n;
int ArrayMax[RMQ_size][21];
int ArrayMin[RMQ_size][21];
void build_rmq(){
for(int j = 1 ; (1<<j) <= n ; ++ j)
for(int i = 0 ; i + (1<<j) - 1 < n ; ++ i){
ArrayMax[i][j]=max(ArrayMax[i][j-1],ArrayMax[i+(1<<(j-1))][j-1]);
ArrayMin[i][j]=min(ArrayMin[i][j-1],ArrayMin[i+(1<<(j-1))][j-1]);
}
}
int QueryMax(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return max(ArrayMax[L][k],ArrayMax[R-(1<<k)+1][k]);
}
int QueryMin(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return min(ArrayMin[L][k],ArrayMin[R-(1<<k)+1][k]);
}
void init(int * a,int sz){
n = sz ;
for(int i = 0 ; i < n ; ++ i) ArrayMax[i][0] = ArrayMin[i][0] = a[i];
build_rmq();
}
}s1,s2;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
for(int i=0;i<n;i++)scanf("%d",&b[i]);
a[n]=2e9;
b[n]=-2e9;
s1.init(a,n+1);
s2.init(b,n+1);
long long ans = 0;
for(int i=0;i<n;i++){
if(a[i]>b[i])continue;
int l=i,r=n,ansl=i;
while(l<=r){
int mid=(l+r)/2;
if(s1.QueryMax(i,mid)>=s2.QueryMin(i,mid))r=mid-1,ansl=mid;
else l=mid+1;
}
if(s1.QueryMax(i,ansl)>s2.QueryMin(i,ansl))continue;
l=i,r=n;
int ansr=i;
while(l<=r){
int mid=(l+r)/2;
if(s1.QueryMax(i,mid)>s2.QueryMin(i,mid))r=mid-1,ansr=mid;
else l=mid+1;
}
ans+=ansr-ansl;
}
cout<<ans<<endl;
}
Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分的更多相关文章
- Codeforces Round #361 (Div. 2) D - Friends and Subsequences
题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足 a中最大值等于b中最小值. 思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写 ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) D
D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...
随机推荐
- YUV422(UYVY)转RGB565源代码及其讲解.md
目录 前言 源码 代码分析 YUV三个分量的关系 循环遍历 结束语 前言 使用zmm220核心板,IFACE102版本的内核等,4300型号的LCD,XC7011_SC1145摄像头,亲测有效. 本文 ...
- asp.net 伪静态实现(UrlRewritingNet)
UrlRewritingNet.UrlRewriter源码地址 https://github.com/aspnetde/UrlRewritingNet部署步骤: 步骤一: <!--只允许存在一个 ...
- css-概述和选择器
一:CSS 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与 ...
- centos7 安装java和tomcat9
centos7 安装java 下载好java安装包后,首先是解压,然后配置环境变量. 在usr下新建Java文件夹,把java解压到Java文件夹中 新建文件夹 # mkdir /usr/Java 键 ...
- Nginx1.8.1打开gzip压缩
1.进入Nginx配置文件目录,打开nginx配置文件 cd /usr/local/src/nginx-1.8.1 vi nginx.conf 2.找到“http {”在之间加入如下配置 gzip o ...
- irport报表,把数字金额转换成大写人民币金额
1.编写oracle函数 CREATE OR REPLACE Function MoneyToChinese(Money In Number) Return Varchar2 Is strYuan ) ...
- jersey 过滤器名称绑定的问题 NameBinding Provider
查资料也不容易查,这个问题困扰了我两天. 当没有 @Provider 的时候 过滤器不会被执行.
- Linux学习笔记:touch新建文件、修改访问、改动时间
touch用于创建新的空文件或者修改已有文件的时间戳. 语法:touch file.txt 如果file存在,使用touch指令可更改这个文件或目录的日期时间,包括存取时间和更改时间. 如果file不 ...
- 20155225 2016-2017-2《Java程序设计》课程总结
20155225 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:新的开始 预备作业2:C语言学习回顾 预备作业3:Linux基础入门和虚拟机的安装 第一 ...
- 20155309 2016-2017-2《Java程序设计》课程总结
预备作业1http://www.cnblogs.com/nhx19970709/p/6155580.html 第一次写博客,也是第一次用Markdown,具体流程都还不是很熟悉 预备作业2http:/ ...