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.

Examples

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

Note

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.

求区间最大值和最小值相同的区间有多少个,因为区间越长,最大值越大,最小值越小,所以可以二分找

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#include<vector>
#include<queue>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f; const double pi=acos(-1.0);
const int N=2e5+3;
int a[N],b[N];
int dpmax[N][20],dpmin[N][20];
void first(int n)
{
mm(dpmax,0);
mm(dpmin,0);
rep(i,1,n+1)
{
dpmax[i][0]=a[i];
dpmin[i][0]=b[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
}
}
}
int fmax(int l,int r)
{
int x=0;
while(l-1+(1<<x)<=r) x++;
x--;
return max(dpmax[l][x],dpmax[r-(1<<x)+1][x]);
}
int fmin(int l,int r)
{
int x=0;
while(l-1+(1<<x)<=r) x++;
x--;
return min(dpmin[l][x],dpmin[r-(1<<x)+1][x]);
}
int main()
{
ll ans=0;
int n,le,ri,mid;
scf(n);
rep(i,1,n+1)
scf(a[i]);
rep(i,1,n+1)
scf(b[i]);
first(n);
rep(i,1,n+1)
{
int l=-1,r=-1;
le=i;
ri=n;
while(ri>=le)
{
mid=(le+ri)/2;
if(fmax(i,mid)>=fmin(i,mid))
{
if(fmax(i,mid)==fmin(i,mid))
l=mid;
ri=mid-1;
}
else
le=mid+1;
}
if(l==-1) continue;
le=i;ri=n;
while(ri>=le)
{
mid=(le+ri)/2;
if(fmax(i,mid)<=fmin(i,mid))
{
if(fmax(i,mid)==fmin(i,mid))
r=mid;
le=mid+1;;
}else
ri=mid-1;
}
ans+=r-l+1;
}
cout<<ans<<endl;
return 0;
}

C - Friends and Subsequences的更多相关文章

  1. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  4. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  5. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  7. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. 【leetcode】Distinct Subsequences(hard)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

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

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

随机推荐

  1. JS自学笔记03

    JS自学笔记03 1.函数练习: 如果函数所需参数为数组,在声明和定义时按照普通变量名书写参数列表,在编写函数体内容时体现其为一个数组即可,再传参时可以直接将具体的数组传进去 即 var max=ge ...

  2. IIS7常见错误及解决方法

    IIS7常见错误及解决方法   问题一:HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效.  详细错误信息模块 IIS We ...

  3. Lua游戏开发之时区问题

    目前大部分游戏都采用了Lua语言进行功能开发,在进行多语种发行的时候就会遇到时区显示的问题.以韩国版本为例,场景如下: 1.服务器处于固定的位置,比如放在首尔机房: 2.玩家所处的位置不确定,可能在韩 ...

  4. 毕业论文Word格式技巧

    word利用多级列表功能实现章节标题自动编号 坑:在选择级别1后,直接在当前对话框直接继续设置级别2:然后选中级别2,设置相应的格式,主要在将级别链接到样式处选择为标题2,然后要在库中显示的级别选择级 ...

  5. Python禁用GC优化性能

    Python使用的(Garbage Collection, GC)机制是引用计数(Reference Count),其原理是为每一个内存对象进行引用计数,因此当有大量的对象新建或删除时,必须要进行大量 ...

  6. James 如何作为服务在后台启动

    james 启动后是在前台运行的,就像你跑一个微服务,前台运行显然不合理,关闭ssh后就会断开,所以我们得配置在后台,使用service配置即可 配置 james/bin 下的phoenix.sh,配 ...

  7. 基于Kafka的生产者消费者消息处理本地调试

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/68174111冷血之心的博客) Kafka下载地址:http://d ...

  8. Fluent动网格【12】:扩散光顺

    扩散光顺是Fluent提供的另外一种常用的网格光顺方法.其基本原理是通过求解扩散方程得到网格节点的运动位移. 扩散光顺基本计算 扩散光顺通过求解 以下扩散方程来设置网格的节点位置. \[ \nabla ...

  9. 【原创 Hadoop&Spark 动手实践 4】Hadoop2.7.3 YARN原理与动手实践

    简介 Apache Hadoop 2.0 包含 YARN,它将资源管理和处理组件分开.基于 YARN 的架构不受 MapReduce 约束.本文将介绍 YARN,以及它相对于 Hadoop 中以前的分 ...

  10. CentOS 7 安装GitLab

    CentOS 安装GitLab CentOS 安装GitLab GitLab是一个利用Ruby on Rails开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私 ...