原题:

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

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.

提示: 暴力比对所有区间时间复杂度是(n^2)无法通过。

观察发现,如果固定区间左边界L,右边界R一次递增,a_max【L,R】是不减的,即有序。 同样b_min【L,R】是不增的,有序。

所以就可以先枚举左端点,再用二分法去寻找右端点的合法(符合题意的)区间,这个区间会是一个连续的范围。

二分的时候注意 RMQ_a(L,mid) = RMQ_b(L,mid) 的时候如何处理决定了最终结果是右端点的左边界还是右边界。

求区间最值是使用了RMQ算法。(一个很精妙的算法,我之前的博客里有写。)

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream> using namespace std; #define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))
#define ll long long const int inf = 0x7fffffff;
const int maxn=2e5+; int a[maxn],b[maxn];
int d_a[maxn][];
int d_b[maxn][]; void RMQ_init_a(int *A,int n)
{
for(int i=; i<n; i++) d_a[i][]=A[i];
for(int j=; (<<j)- < n; j++)
for(int i=; i+(<<j)- < n; i++)
d_a[i][j]=MAX( d_a[i][j-], d_a[i + (<<(j-))][j-] );
} int RMQ_a(int L, int R)
{
int k=;
while( <<(k+) <= R-L+ ) k++;
return MAX( d_a[L][k], d_a[R-(<<k) + ][k] );
} void RMQ_init_b(int *A,int n)
{
for(int i=; i<n; i++) d_b[i][]=A[i];
for(int j=; (<<j)- < n; j++)
for(int i=; i+(<<j)- < n; i++)
d_b[i][j]=MIN( d_b[i][j-], d_b[i + (<<(j-))][j-] );
} int RMQ_b(int L, int R)
{
int k=;
while( <<(k+) <= R-L+ ) k++;
return MIN( d_b[L][k], d_b[R-(<<k) + ][k] );
} int main()
{
int n;
cin>>n;
for(int i=; i<n; i++) scanf("%d",a+i);
for(int i=; i<n; i++) scanf("%d",b+i);
RMQ_init_a(a,n);
RMQ_init_b(b,n);
int left, right;
ll ans = ;
for(int L=; L<n; L++)
{
int left = inf, right = -inf;
//求左边界
int l = L;
int r = n-;
while(l <= r)
{
int mid=(l + r)/;
if(RMQ_a(L,mid) > RMQ_b(L,mid)) //左半部分
r=mid-;
else if(RMQ_a(L,mid) < RMQ_b(L,mid)) //右半部分
l=mid+;
else
{
left = min(left, mid);
r=mid-;
}
}
// printf("left = %d\n",left);
//求右边界
l = L;
r = n-;
while(l <= r)
{
int mid=(l + r)/;
if(RMQ_a(L,mid) > RMQ_b(L,mid)) //左半部分
{
r=mid-;
}
else if(RMQ_a(L,mid) < RMQ_b(L,mid)) //右半部分
l=mid+;
else
{
right = max(right, mid);
l=mid+;
}
}
// printf("right = %d\n",right);
if(left != inf)
ans += right - left + ;
}
cout<<ans<<endl; return ;
}

codeforces 361 D - Friends and Subsequences的更多相关文章

  1. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  2. codeforces 361 D. Levko and Array(dp+二分)

    题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...

  3. codeforces 361 C. Levko and Array Recovery(暴力+思维)

    题目链接:http://codeforces.com/contest/361/problem/C 题意:对一个数列有这么两个操作 1.(1,l,r,p)..将区间[l,r]所有数都加上p 2.(2,l ...

  4. 套题 codeforces 361

    A题((Mike and Cellphone) 看起来好像需要模拟数字键位的运动,可是,只要判断出那些必然YES的数字组合不就好了么 #include <cstdio> #include ...

  5. Codeforces Testing Round #12 C. Subsequences 树状数组

    C. Subsequences     For the given sequence with n different elements find the number of increasing s ...

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

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

  7. 【codeforces 314C】Sereja and Subsequences

    [题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...

  8. CodeForces 689 D Friends and Subsequences

    Friends and Subsequences 题解: 如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求 ...

  9. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

随机推荐

  1. XListView刷新

    package com.example.da; import java.util.ArrayList;import java.util.List; import com.badu.net.Networ ...

  2. 黑马程序员——JAVA基础之反射

      ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! -------     Java 反射是Java语言的一个很重要的特征,它使得Java具体了"动态 ...

  3. Channel States

    Introduction A channel (a call) will go through many different states during its lifetime. Here we w ...

  4. testNG设置测试的执行顺序

    在java类中,设置Test的执行顺序可以使用priority,或者enabled等属性.但是在testng.xml中,需要设置它的 preserve-order="true" 另 ...

  5. urllib2.open(req).read() 报403的错误:怎么办?

    http://www.douban.com/group/topic/18095751/ heads = {'Accept':'text/html,application/xhtml+xml,appli ...

  6. Deepin下phpunit安装,以及执行过程中所遇到的问题

    Deepin下phpunit安装,以及执行过程中所遇到的问题 安装phpunit步骤 wget https://phar.phpunit.de/phpunit.phar chmod +x phpuni ...

  7. Tcpdump非常实用的抓包12实例

    1.过滤主机---------------------------------------------------------------- - 抓取所有经过 eth1,目的或源地址是 192.168 ...

  8. R中,定义一个长度为0的向量

    定义一个长度为0的向量 > x<-c()> length(x)[1] 0 修改该向量的类型 > class(x)="numeric"> class(x ...

  9. (C#)算法题

    1. Convert string from "AAABBCC" to "A3B2C2". 当面试者提出这个问题的时候,首先需要确认题意:譬如:字符串是不是顺序 ...

  10. 把自己主要在做的几个工程都传到了GitHub上

    GitHub链接 https://github.com/MichaelSuen-thePointer 里面有4个项目,一个是我的C大程大作业,一个3600+行的字典程序,已经弃坑不再更新 还有一个叫w ...