Codeforces Round #361 (Div. 2) D. Friends and Subsequences (二分+RMQ)

POJ-2949 Word Rings(SPFA DFS实现 + 环上平均权值最大) Description A word ring is a sequence of words where the last two letters of each word are the same as the first two letters of the next word (and the last two letters of the last word are the same a 阅读详情

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.


题意:给你两个数列A,B,问你有多少组区间[l,r]满足其在A中的最大值等于B中的最小值。


分析:枚举区间的起始位置,通过简单分析可以得出,以l为起点的所求区间的r必定是连续的一段,且这段长度内的max = min,事先RMQ处理一下,随后二分出左右端点记入答案。

#include <cstdio>
#include <queue>    
#include <vector>    
#include <cstdio>    
#include <utility>    
#include <cstring>    
#include <iostream>    
#include <algorithm>    
#define INF 0x3f3f3f3f
using namespace std;
long long ans;
int n,a[200007],b[200007];
int Fma[200007][20];
int Fmi[200007][20];
int qumax(int l,int r)
{
	int leng = log2(r-l+1);
	return max(Fma[l][leng],Fma[r-(1 << leng)+1][leng]);
}
int qumin(int l,int r)
{
	int leng = log2(r-l+1);
	return min(Fmi[l][leng],Fmi[r-(1 << leng)+1][leng]);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	for(int i = 1;i <= n;i++) scanf("%d",&b[i]);
	for(int i = 1;i <= n;i++)
	{
		Fma[i][0] = a[i];
		Fmi[i][0] = b[i];
	}
	for(int i = 1;(1 << i) <= n;i++)
	 for(int j = 1;j + (1 << i) -1 <= n;j++)
	 {
		Fma[j][i] = max(Fma[j][i-1],Fma[j+(1 << (i-1))][i-1]);
		Fmi[j][i] = min(Fmi[j][i-1],Fmi[j+(1 << (i-1))][i-1]);
	 }
	for(int i = 1;i <= n;i++)
	{
		int s = i;int t = n;
		while(s != t)
		{
	    	int mid = (s+t)/2;
			if(qumax(i,mid) >= qumin(i,mid)) t = mid;
			else s = mid+1;		
		}
		if(qumax(i,s) != qumin(i,s)) continue;
		int l = s;
		s = i,t = n;
		while(s != t)
		{
	    	int mid = (s+t)/2+1;
			if(qumax(i,mid) <= qumin(i,mid)) s = mid;
			else t = mid-1;		
		}
		int r = s;
		ans += r-l+1ll;
	} 
	cout<<ans<<endl;
}



DDR5内存Training全解析:从信号完整性到CS训练模式的实战指南 本文深入解析了DDR5内存Training的全过程,从信号完整性挑战到CS训练模式(CSTM)的实战应用。文章详细阐述了Training在DDR5高频时代的关键作用,对比了In-Band与OOB两种架构,并拆解了Intel MRC的核心训练步骤,为硬件与固件工程师提供了诊断与优化系统稳定性的实用指南。 阅读详情

相关推荐

Gitlab+Jenkins+Docker+Harbor+K8s+Rancher集群搭建CICD平台

本文以诺依项目为例详细记录了Kubernetes集群的部署过程以及配套DevOps环境的搭建。主要内容包括:1) 配置8个节点的软硬件环境;2) 基础环境准备(网络、防火墙、Docker等);3) 三节点Kubernetes集群部署;4) GitLab代码仓库安装;5) Harbor私有镜像仓库搭建;6) Jenkins持续集成工具配置;7) 基于若依项目的CI/CD实践;8) Rancher集群管理工具部署。

hickman2023的博客 1225

Codeforces Round #361 (Div. 2) D. Friends and Subsequences RMQ+二分

链接: http://codeforces.com/contest/689/problem/D 题意: 给出两个数组,问有多少个区间l,r满足 题解: 先用st表预先处理任意一个区间的最值,然后枚举每个左端点,二分找到满足条件的最近和最远的右端点即可 代码: 31 int n; 32 int a[MAXN], b[MAXN]; 33 int st_min[MAXN]...

花海 190

从SAR信号到洪涝地图:基于Sentinel-1数据的水体快速提取实战

本文详细介绍了如何利用Sentinel-1 SAR数据快速提取水体信息并生成洪涝地图的实战方法。通过预处理五部曲和SDWI指数计算,结合具体案例展示了SAR技术在洪涝灾害应急监测中的高效性和准确性,为救援决策提供关键支持。

weixin_29201313的博客 483

D. Friends and Subsequences Codeforces Round #361 (Div. 2)RMQ+二分 && 单调队列

D. Friends and Subsequences Codeforces Round #361 (Div. 2) RMQ+二分,单调队列

小蒟蒻的博客 480

4相5线步进电机驱动原理

步进电机驱动

while(1) 3万+

AURIX-TC3xx-GTM详解六-DPLL(Digital PLL Module)-寄存器1-DPLL寄存器

主要有下图这些寄存器2、

m0_69929716的博客 449

codeforces Round #361 D. Friends and Subsequences (ST表,二分)

D. 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 tog

lov_vol 1033

从零开始安装pytorch(包含GPU和CPU版本的安装)

Pytorch是。

qiaoxinyu1989的博客 8万+

Codeforces Round #361 (Div. 2) D Friends and Subsequences

题目链接:http://codeforces.com/contest/689/problem/D 题意:给定两个长度相同的数组,a和b。问你在相同的区间中amax=bmin的区间有多少个,数组的范围为200000. 解法:我们可以枚举起始点,然后用二分查到amax和bmin相等的左右端点,然后进行区间累加就可以了。对于最大值最小值的查询我们可以采用线段树和RMQ来优化。线段树的查询复杂度是lo

longshanxiaoxuesheng的博客 391

戴维南和诺顿定理到底怎么选?一个实际维修案例讲透两种等效电路用法

本文通过工业电源模块维修案例,详细解析戴维南和诺顿定理在实际电路分析中的选择策略。针对电压敏感型故障、电流分配问题及功率传输异常,提供三大黄金法则,帮助工程师高效选择等效电路方法,提升故障排查效率。特别强调诺顿定理在电流分析中的优势。

weixin_31854867的博客 364

WPF进阶 | 深入 WPF 依赖项属性:理解其强大功能与应用场景

WPF进阶 | 深入 WPF 依赖项属性:理解其强大功能与应用场景,在 Windows Presentation Foundation(WPF)开发中,依赖项属性是一个核心且强大的特性,它为构建灵活、可扩展的用户界面提供了关键支持。理解并熟练运用依赖项属性,对于开发高效、健壮的 WPF 应用程序至关重要。本文将深入探讨 WPF 依赖项属性,通过丰富的代码示例和对关键概念的详细解释,帮助读者全面掌握这一重要特性及其应用场景。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 4万+

Codeforces Round #361 (Div. 2) D - Friends and Subsequences

题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足 a中最大值等于b中最小值。 思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写了 一晚上假算法。正解就是枚举一个端点,然后二分找右端点的区间,因为满足一个很神奇的单调性,然后st表维护 一下区间最值就好了。 #include<bits/std...

160

【异常】OpenClaw LLM Request Timeout 超时问题的快速定位与落地解决

摘要:OpenClaw系统在执行任务时频繁出现"LLM request timed out"异常。通过分析发现主模型自动调度导致响应延迟,且缺乏超时配置兜底。解决方案包括:1)将主模型切换为低延迟的豆包2.0 Lite;2)保留豆包2.0 Pro和Kimi作为备用;3)重启Gateway服务验证。优化后系统响应速度显著提升,未再出现超时异常。后续建议增加显式超时配置、优化任务拆分和Prompt指令,并建立网络监控机制。(148字)

奶爸的编程之路,也就一周冷个三天 1169

Codeforces Round #361 (Div. 2)D. Friends and Subsequences

链接:http://codeforces.com/contest/689/problem/D 题意:给定两个长度为n的数组a,b。求有多少个区间[l,r]有max(a[l]~a[r])==min(b[l]~b[r])。 分析:对于每一个a[i]找到它为最大的值的区间最大边界l[i]和r[i],同样处理b数组。然后我们枚举以每一个a[i]为区间max来和b数组计数,注意下去重就行了。 代码:

Fsss 542

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Codeforces Round #361 (Div. 2) D Friends and Subsequences

原题网址:http://codeforces.com/contest/689/problem/D对于每一个确定的l,随着r的增大,max ai 单调增,min bi 单调减,对于每一个l可以二分,对于区间最值可以RMQ,总复杂度nlogn。(一开始区间最值用线段树平添log)var pr1,pr2:array[0..200050,0..18] of longint; n,i,j,l,r,m:

MintGreen 416

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Toda...

weixin_30856965的博客 93

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Codeforces Round #361 (Div. 2) D. Friends and Subsequences 题解(st表+二分 or 单调队列)

题目链接 题目大意 给你两个长度为n(2e5)的数组a和数组b,要你求有多少个区间区间满足下列式子 即有多少个字串,使得a字串的最大值等于b字串中的最小值 st表+二分 首先你可以固定左端点,然后你会发现右端点变大时,a数组的最大值是非严格单调递增,而b数组的最小值是非严格单调递减的。所以就很容易想到去二分查找.枚举左端点,查找右端点。你会发现右端点可能是一段区间,然后我就不知道咋做了,其实就是两次二分就行了qwq 代码 #include<set> #include<map> #i

hunxuewangzi的博客 221

Codeforces Round #361 (Div. 2) D.Friends and Subsequences (multiset + 尺取法)

D. 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 thei...

dexicuo5991的博客 173

Codeforces Round #361 (Div. 2) D. Friends and Subsequences

原题链接 D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Mike and !M

L. 380

Codeforces Round #361 (Div. 2)

A. Mike and Cellphone 模拟即可。 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;char s[20];int main(){ int n; while(scanf("%d", &n) != EOF){

你真的介意真的在乎真的想要真的渴望的话你一定能够做到 253

LA 4253 Archery (二分)

Korea's reputation in archery is well known because the Korean archery teams have been sweeping almost all gold, silver, and bronze medals in the Olympic Games. An archery game ICPC supported by NE

WZJRJ28 1387

LA 4254 Processor (单调队列 + 二分)

题意:有n个任务,每个任务有三个参数,ri,di,wi,表示在ri di之内需要执行完工作量为wi的任务,任务可以分块执行,求执行过程中的最大速度的最小值。速度需为整数。 分析:二分判断,判断有些复杂,要用到单调队列,写的挺烂的,但是一遍过了。 #include #include #include #include #include #define MAXN 10002

WZJRJ28 1040

Ural 2038 Minimum Vertex Cover(二分图最小顶点覆盖+快速读入)

题意:给你一个二分图,让你把二分图中的点划分为三个集合:一定在最小顶点覆盖集中,一定不在,可能在。 分析:若一个匹配点和一个未匹配点之间有边,那这个匹配点一定在最小顶点覆盖集中,它的另一个匹配点一定不再其中,一个一定不再其中的点连向的所有点都一定给在最小顶点覆盖集中,我们可以就这样DFS一遍求出所有结果。 #include #include #include #include #inc

WZJRJ28 869
上一篇: BZOJ-1010 [HNOI2008]玩具装箱toy (DP斜率优化)
下一篇: Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves(二分判断)
RJ28
博客等级 码龄13年 23粉丝 365原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值