C - Friends and Subsequences
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的更多相关文章
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
随机推荐
- IO流(5)—缓冲流
1.IO体系: 抽象基类--节点流(文件流)--缓冲流(处理流的一种) InputStream --FileInputStream--BufferedInputStream OutputStream- ...
- Aizu2249 Road Construction(dijkstra优化+思路 好题)
https://vjudge.net/problem/Aizu-2249 感觉这题和2017女生赛的Deleting Edge思路很像,都是先找最短路,然后替换边的. 但是这题用最朴素的dijkstr ...
- 优先队列重载<运算符
https://vjudge.net/problem/POJ-3190 #include<iostream> #include<cstdio> #include<queu ...
- Linux软件开发常用的软件包(持续更新中)
下面是Linux开发常用的软件包: 软件包的名称 作用描述 安装方式 build-essential sudo apt-get install build-essential policycore ...
- 修复恢复"可疑"的SQLServer数据库
今天机房突然断电,DB连不上了,提示 无法打开数据库'MyDB'.恢复操作已将该数据库标记为 SUSPECT. 原因是断电导致DB文件损坏 通过SQL Server Management Studio ...
- golang time打印出的值是62135596800的来源
' 减去62135596800是将"以公元1年1月1日0点为基准"改成"以1970年1月1日0点"为基准 所以,数据库datetime的默认值 : 0000-0 ...
- WSL(Windows Subsystem for Linux)的安装与使用及 mongodb安装
有关WSL的介绍这里就不做解释了.另外,本文仅适用于win10 build 16215以及之后的版本,之前的版本可参考官方链接. (可使用“winver”命令查看windows版本号) 安装:1. ...
- H5调用本地摄像头[转]
http://www.cnblogs.com/GoodPingGe/p/4726126.html <!DOCTYPE html><html><head lang=&quo ...
- PhpStorm配置SVN的完整方法
1.安装SVN时注意选择“command line client tools"默认是不安装的 2.设置系统环境变量 3.在PhpStorm上设置如下 4.然后通过VCS就可以上传导入你的工程 ...
- [转]epoll详解
什么是epollepoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new AP ...