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 二分的更多相关文章

  1. Codeforces Round #361 (Div. 2) D - Friends and Subsequences

    题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足 a中最大值等于b中最小值. 思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写 ...

  2. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  3. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  8. 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 ...

  9. Codeforces Round #361 (Div. 2) D

    D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...

随机推荐

  1. 【API】Mysql UDF BackDoor

    1.MySQL UDF是什么 UDF是Mysql提供给用户实现自己功能的一个接口,为了使UDF机制起作用,函数必须用C或C ++编写,并且操作系统必须支持动态加载.这篇文章主要介绍UDF开发和利用的方 ...

  2. 解决 Windows 环境 Git Bash 无法识别 Composer 命令的问题

    思路 模拟 Linux,复制一个 composer 文件到 Git Bash 的 /usr 的子目录,并赋予执行权限. 解决 首先,请确定你的 composer.phar 文件路径.我的是: /d/w ...

  3. Linux下简单粗暴使用rsync实现文件同步备份【转】

    这篇来说说如何安全的备份,还有一点不同的是上一篇是备份服务器拉取数据,这里要讲的是主服务器如何推送数据实现备份. 一.备份服务器配置rsync文件 vim /etc/rsyncd.conf #工作中指 ...

  4. 19 Error handling and Go go语言错误处理

    Error handling and Go go语言错误处理 12 July 2011 Introduction If you have written any Go code you have pr ...

  5. 基于vue配置axios

    转载地址:https://juejin.im/post/5a02a898f265da43052e0c85 1.背景 在项目开发中ajax请求是必不可缺少 一部分ajax请求不需要loading或则请求 ...

  6. Jquery获取radio单选按钮的value与后面的文字

    一组单选按钮如图: <input name="classId" value="8afa94f45ba3e2c1015ba3fac6c00000" type ...

  7. mongod 一些命令汇总

    1. 导出数据库: mongoexport -d master -c reports -o no.json --type json -f "title,name" -q '{&qu ...

  8. 关于主键的设计、primary key

    主键:用于唯一标识一个表中一行数据. 外键:用于建立两个表之间的关系,A表中有一列是B表中的主键,那么A表中这列的数据就受到B表主键的约束. 那么关于主键应该如何设计呢,这里我说下优缺点: 1.用自动 ...

  9. NYOJ 石子合并(一)(区间DP)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737 题目大意: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆 ...

  10. SQL SERVER 断开所有连接(转)

    通过sql server management studio对数据进行管理,比如数据库改名等,经常遇到有正在运行的连接,以致无法操作,这时候断掉所有的连接很有必要.代码如下:(会断掉某个库的所有连接, ...