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 ...
随机推荐
- Why validation set ?
Let's assume that you are training a model whose performance depends on a set of hyperparameters. In ...
- 使用iperf测试网卡吞吐性能
原 使用iperf测试网卡吞吐性能 2018年12月17日 12:38:41 lancewoo 阅读数:138 首先配置待测试的两个网卡的网络地址到同一网段,保证ping对方的IP地址时可以通.两 ...
- t-io 集群解决方案以及源码解析
t-io 集群解决方案以及源码解析 0x01 概要说明 本博客是基于老谭t-io showcase中的tio-websocket-showcase 示例来实现集群.看showcase 入门还是挺容易的 ...
- sql注入工具:sqlmap命令
sqlmap是一款专业的sql注入工具, 让你告别人工注入, 程序高效自动注入 前提是你有找到注入点 , 工具的官方网站:http://sqlmap.org/ kali系统默认安装sqlmap, 不需 ...
- 【原】Linux环境下Shell调用MySQL并实现定时任务
对于一些周期性事务,我们可以在Linux下,使用shell脚本调用mysql数据库存储过程,并设置定时任务. 本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...使用cron ...
- Mosquitto --topic
订阅树的概念 Mosquitto通过订阅树的方式来管理所有的topic以及客户端的订阅关系,它首先将所有的topic按照/分割并组织成一棵树结构,从根节点到树中的每个节点即组成该节点所对 ...
- james2.3 配置收件 之 MariaDB数据库配置
james我们公司一直都是使用的2.3这个稳定版本,现在已经有3.0了,不过无所谓,能用就行 基于2.3,来进行一些配置,主要是接受邮件,之前的博文如何安装的,这里不多做介绍了,链接参考:https: ...
- InputStream为什么不能被重复读取?
最近上传阿里云的时候同一个文件上传两个服务地址,第一个文件读取以后第二个再去读取就拿不到了.代码如下: //内网上传OSS获取key值 String ossKey = OSSClientUtil.ge ...
- MySQL高性能优化系列-目录
MySQL高性能优化系列-目录 (1)Mysql高性能优化规范建议 (2)电商数据库表设计 (3)MySQL分区表使用方法 (4)MySQL执行计划分析 (5)电商场景下的常见业务SQL处理 (6)M ...
- 10个最好的免费PS图象处理软件方案
说到照片和图像编辑/操纵,真的没有更好的应用,Adobe PS图象处理软件. 摄影师和创意工作室会同意这是总理的照片编辑应用期. 不幸的是,PS图象处理软件还配备了一个陡峭的学习曲线和价格标签,我们必 ...