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<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
long long f[200001][30], tua[200001], fx[200001][30], tub[200001], ty[200001];
long long n, m;
void rmq(long long c)
{
long long cc = 20;
for (long long a = 1; a <= cc; a++)
{
for (long long b = 1; b <= c; b++)
{
if (b + (1 << a) - 1 <= c)
{
f[b][a] = max(f[b][a - 1], f[b + (1 << (a - 1))][a - 1]);
fx[b][a] = min(fx[b][a - 1], fx[b + (1 << (a - 1))][a - 1]);
}
}
}
}
long long xunwenda(long long zuo, long long you)
{
long long k = log2(you - zuo + 1);
return max(f[zuo][k], f[you - (1 << k) + 1][k]);
}
long long xunwenxiao(long long zuo, long long you)
{
long long k = log2(you - zuo + 1);
return min(fx[zuo][k], fx[you - (1 << k) + 1][k]);
}
long long jc(long long z, long long y)
{
if (xunwenda(z, y) > xunwenxiao(z, y))return 0;
else if (xunwenda(z, y) == xunwenxiao(z, y))return 1;
return 2;
}
int main()
{
#define int long long
int n;
cin >> n;
for (int a = 1; a <= n; a++)scanf("%lld", &tua[a]);
for (int a = 1; a <= n; a++)scanf("%lld", &tub[a]);
for (int a = 1; a <= n; a++)f[a][0] = tua[a];
for (int a = 1; a <= n; a++)fx[a][0] = tub[a];
rmq(n);
int qw = 0;
for (int a = 1; a <= n; a++)
{
int z = a, y = n;
int tt = 0;
while (z <= y)
{
int mid = (z + y) / 2;
int qq = jc(a, mid);
if (qq == 1)
{
tt = max(tt, mid);
}
if (qq == 0)
{
y = mid - 1;
}
else
{
z = mid + 1;
}
}
ty[a] += tt;
}
for (int a = 1; a <= n; a++)
{
int z = a, y = n;
int tt = 2100000;
while (z <= y)
{
int mid = (z + y) / 2;
int qq = jc(a, mid);
if (qq == 1)
{
tt = min(tt, mid);
}
if (qq == 0 || qq == 1)
{
y = mid - 1;
}
else if (qq == 2)
{
z = mid + 1;
}
}
ty[a] -= tt;
}
for (int a = 1; a <= n; a++)
{
if (ty[a] < 0)continue;
qw += ty[a]+1;
}
cout << qw;
}