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. BZOJ 2339 卡农(组合数学)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2339 题意: 思路: i64 Pow(i64 a,i64 b,i64 mod){    ...

  2. Android开发之TextView实现跑马灯效果

    TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符:省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中). 其实现只需在x ...

  3. [IE编程] 多页面基于IE内核浏览器的代码示例

    有不少人发信问这个问题,我把答案贴在这里: 建议参考 WTL (Windows Template Library) 的代码示例工程TabBrowser  (在WTL目录/Samples/TabBrow ...

  4. hdu 4961 Boring Sum (思维 哈希 扫描)

    题目链接 题意:给你一个数组,让你生成两个新的数组,A要求每个数如果能在它的前面找个最近的一个是它倍数的数,那就变成那个数,否则是自己,C是往后找,输出交叉相乘的和 分析: 这个题这种做法是O(n*s ...

  5. 函数fil_io

    /********************************************************************//** Reads or writes data. This ...

  6. 基于XMPP的即时通信系统的建立(六)— 开发环境搭建

    服务器端 新建空工程 使用Eclipse新建名为openfire的空java工程. 导入源代码 这里使用的是openfire的openfire_src_3_10_3.zip源码. 导入后将目录src/ ...

  7. 【笨嘴拙舌WINDOWS】编码历史

    在介绍历史之前,有必要将一个经常使用的词语"标准"解释一下: " 标准是"为了在一定的范围内获得最佳秩序,经协商一致制定并由公认机构批准,共同使用的和重复使用的 ...

  8. C语言深入学习系列 - 字节对齐&内存管理

    用C语言写程序时需要知道是大端模式还是小端模式. 所谓的大端模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中:所谓的小端模式,是指数据的低位保存在内存的低地址中,而数据的高 ...

  9. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  10. 解决Jsoup网页抓取过程中需要cookie的问题

    最近在做城觅网的信息抓取,发现城觅网上海与北京的url是一样的.那怎样才确定信息的来源呢?折腾了半天,才发现城觅网是使用cookie的,如果你把网站的cookie禁用了,就无法在上海与北京之间切换了. ...