原题:

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. js动态添加onload、onresize、onscroll事件(另类方法)

    js动态添加onload.onresize.onscroll事件(另类方法)   window 的 onload.onresize.onscroll 事件,跟其他的事件不一样,它不能用 attachE ...

  2. sqlserver OpenRowSet 对应的三种数据库驱动

    在使用sqlserver数据库的OpenRowSet函数时,会遇到三种驱动方式: 1. MSDASQL驱动SELECT TOP 10 * FROM OPENROWSET('MSDASQL', 'DRI ...

  3. Sprint第二个冲刺(第十天)

    一.Sprint 计划会议: 现在总结一下情况,正在做的3个功能的完成程度已经达到了80%,过几天就可以完成了.也把之前做的修改界面放入fragment中,方便修改管理.效果图如下: 二.Sprint ...

  4. 20145113 实验二 Java面向对象程序设计

    20145113 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 1.初 ...

  5. opencv--图像轮廓检测

    //图像的轮廓检测上 //By MoreWindows (http://blog.csdn.net/MoreWindows) #include <opencv2/opencv.hpp> u ...

  6. nginx配置文件重写url不带index.php

    如题: 代码 location / { root /项目目录/; index index.php; if (-f $request_filename/index.php){ rewrite (.*) ...

  7. PHP如何批量生成手机号-使用PHP 如何生成一组不重复的手机号码?

    <?php //匹配手机号的正则表达式 #^(13[0-9]|14[47]|15[0-35-9]|17[6-8]|18[0-9])([0-9]{8})$# $arr = array( 130,1 ...

  8. [Freemarker] - 使用struts的component调用freemarker的ftl模板方法

    struts中的component标签,可以用来调用freemarker的ftl模板文件,使用component标签传参可以这样写: 使用property方式写法: <s:component t ...

  9. HDU3487 play with chain

    题目大意:给出1到n的有序数列,现在有两个操作: 1.CUT a b c 把第a到第b个数剪切下来,放到剩下的第c个数的后边. 2.FLIP a b  把第a到第b个数反转. 经过总共m次操作后,求现 ...

  10. 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1

    在Windows Server 2008下直接安装SQL Server 2008时,会出现如下错误: 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1 ...