Friends and Subsequences

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121333#problem/H

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 max(alar)==min(blbr) is satisfied.

Sample Input

Input

6

1 2 3 2 1 4

6 7 1 2 3 2

Output

2

Input

3

3 3 3

1 1 1

Output

0

Hint

The occasions in the first sample case are:

1.l = 4,r = 4 since max{2} = min{2}.

2.l = 4,r = 5 since max{2, 1} = min{2, 3}.

There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.

题意:

分别已知a b数组任意区间的最大值、最小值;

求有多少区间[l,r]满足max(alar)==min(blbr);

题解:

RMQ:O(nlgn)预处理 O(1)求出任意区间的min/max;

在固定区间左端点情况下:

由于最大值最小值均具有单调性;

用两次二分操作分别求出第一次和最后一次满足min==max的右端点,作差累加即可;

注意:两次二分操作的差别和意义.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n, A[maxn], B[maxn];
int d_min[maxn][30];
int d_max[maxn][30]; void RMQ_init() {
for(int i=0; i<n; i++) d_max[i][0] = A[i], d_min[i][0] = B[i];
for(int j=1; (1<<j)<=n; j++)
for(int i=0; i+(1<<j)-1<n; i++) {
d_min[i][j] = min(d_min[i][j-1], d_min[i+(1<<(j-1))][j-1]);
d_max[i][j] = max(d_max[i][j-1], d_max[i+(1<<(j-1))][j-1]);
}
}
int RMQ_min(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
return min(d_min[L][k], d_min[R-(1<<k)+1][k]);
}
int RMQ_max(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
return max(d_max[L][k], d_max[R-(1<<k)+1][k]);
} int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n) != EOF)
{
for(int i=0; i<n; i++) scanf("%d",&A[i]);
for(int i=0; i<n; i++) scanf("%d",&B[i]);
RMQ_init(); LL ans = 0;
for(int i=0; i<n; i++) {
if(A[i] > B[i]) continue;
int first_r=-1, last_r=-1;
int l=i,r=n-1,mid; while(l <= r) {
mid = (l+r) / 2;
if(RMQ_max(i,mid) == RMQ_min(i,mid)) first_r = mid;
if(RMQ_max(i,mid) >= RMQ_min(i,mid)) r = mid-1;
else l = mid+1;
}
if(first_r == -1) continue; l=i; r=n-1;
while(l <= r) {
mid = (l+r) / 2;
if(RMQ_max(i,mid) > RMQ_min(i,mid))
r = mid-1;
else l = mid+1, last_r = mid;
} ans += last_r - first_r + 1;
} printf("%I64d\n", ans);
} return 0;
}

CodeForces 689D Friends and Subsequences (RMQ+二分)的更多相关文章

  1. 689D Friends and Subsequences RMQ+二分

    题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...

  2. codeforces 689D D. Friends and Subsequences(RMQ+二分)

    题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...

  3. CodeForces 689D Friends and Subsequences

    枚举,二分,$RMQ$. 对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的. 因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置$ ...

  4. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  5. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  7. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  9. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

随机推荐

  1. C++中变量自动初始化的问题

    C++中有一些变量在如果没有赋初值会被编译器自动赋值为0,但有的变量又不会这样,而得到一个随机数,下面具体讨论一下: 首先看一下C++中的几个存储区:1.栈区:由编译器自动分配释放 ,存放函数的参数值 ...

  2. csv 文件介绍

    CSV即Comma Separate Values,这种文件格式经常用来作为不同程序之间的数据交互的格式. 具体文件格式 每条记录占一行 以逗号为分隔符 逗号前后的空格会被忽略 字段中包含有逗号,该字 ...

  3. poj1088

    这题是dp还是dfs+记忆化?(其实好像没什么区别?) 用f[i,j]表示滑到(i,j)时之后最多能滑多远,依次穷举每一个起点(i,j)则 f[i,j]=max{f[i,j-1],f[i-1,j],f ...

  4. linux的HugePage与oracle amm关系

     如果Oracle 是11g以后的版本,那么默认创建的实例会使用Automatic Memory Management (AMM)的特性,该特性与HugePage不兼容. 在设置HugePage之前需 ...

  5. Hadoop configration类分析

    configration这个类是分析hadoop源代码一个很好地入口. 先从需求说起.对于一个大型的文件系统,基于配置文件可以增强灵活性.congfigration类就是为了管理配置文件的. 配置文件 ...

  6. iphone 如何清空UIWebView的缓存

      iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...

  7. (转)在mac上配置cocos2d-x开发环境

    转自:http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一.下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录 ...

  8. LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)

    题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...

  9. 【 D3.js 高级系列 — 10.0 】 思维导图

    思维导图的节点具有层级关系和隶属关系,很像枝叶从树干伸展开来的形状.在前面讲解布局的时候,提到有五个布局是由层级布局扩展来的,其中的树状图(tree layout)和集群图(cluster layou ...

  10. 省常中模拟 Test2 Day2

    two 模拟 大意:给你一个 N 位二进制数,有四种操作:加1.减1.乘2.整除2.给定一个操作序列,求最终结果.N <= 5*10^6.数据保证不会在最高位上进行进位或退位操作. 初步解法:由 ...