原题:

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. Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ;

    Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ; 如果你在网上search这个 ...

  2. div滚动条弹出层效果 (所需要的css文件和js文件,都已经上传到文件里面了progressbar.rar)

    <%--总的弹出层--%> <div class="tcck" id="joinclub" style="display:none& ...

  3. 移动开发框架,Hammer.js&nbsp;移动设备触摸手势js库

    hammer.js是一个多点触摸手势库,能够为网页加入Tap.DoubleTap.Swipe.Hold.Pinch.Drag等多点触摸事件,免去自己监听底层touchstart.touchmove.t ...

  4. Android画图Path的使用

    /**       * Paint类介绍       *        * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色,       * 样式等绘制信息,指定了如何绘制文本 ...

  5. 设计模式-UML类图的各符号含义(转)

    UML类图的各符号含义 类图基本符号可拆分为虚线,箭头,实线,空心右三角,实心右三角,空心菱形和实心菱形.由这些基本的图形进行组合构成了类图的基本符号.这里要注意这几个符号的顺序,代表了类与类之间关系 ...

  6. Maven根据不同个环境打包, 获取不同的配置文件等等

    http://www.cnblogs.com/tartis/p/5391079.html <project xmlns="http://maven.apache.org/POM/4.0 ...

  7. 用vs2010编译和调试多个arx版本的arx项目

    翻译自dev guide<Compile and Debug Mixed-mode projects>    默认vs2010是使用ARX2014,开发AutoCAD2014使用的ARX, ...

  8. 6、java中的构造代码块

    /* 演示构造代码块的应用 */ class Person { String name; int age; //构造代码块 { cry(); } Person(String name, int age ...

  9. [zz]Lessons from Pixar: Why Software Developers Should Be Storytellers

    http://firstround.com/article/lessons-from-pixar-why-software-developers-should-be-story-tellers Whe ...

  10. ADF_ManagedBean的概念和管理(概念)

    20150623 Created By BaoXinjian