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 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 exactlyn(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<cstdio>
#include<set>
#include<iostream>
using namespace std;
const int maxn = 200010;
#define ll long long
int a[maxn], b[maxn];

int main() {
    int n;
    while(~scanf("%d", &n)) {
        multiset<int> sa[2];
        multiset<int> sb[2];
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);
        for(int i = 0; i < n; i++) scanf("%d", &b[i]);
        int r1, r2;
        r1 = r2 = n - 1;
        ll ans = 0;
        for(int i = n - 1; i >= 0; i--) {
            sa[0].insert(a[i]); sa[1].insert(a[i]);
            sb[0].insert(b[i]); sb[1].insert(b[i]);
            while(!sa[0].empty() && *sa[0].rbegin() > *sb[0].begin()) {
                sa[0].erase(sa[0].find(a[r1]));
                sb[0].erase(sb[0].find(b[r1--]));
            }
            while(!sa[1].empty() && *sa[1].rbegin() >= *sb[1].begin()) {
                sa[1].erase(sa[1].find(a[r2]));
                sb[1].erase(sb[1].find(b[r2--]));
            }
            ans += r1 - r2;
        }
        printf("%I64d\n", ans);
    }
}

 

转载于:https://www.cnblogs.com/lonewanderer/p/5656656.html

Qt Tab Widget 实战:如何打造一个可动态增删的标签页管理器(附完整代码) 本文深入探讨了使用Qt框架中的Tab Widget控件构建动态标签页管理器的实战方法。通过分析动态增删的核心挑战,提供了完整的C++代码示例,涵盖信号槽机制、页面生命周期管理、上下文菜单实现以及状态持久化等关键功能,帮助开发者打造高效、用户友好的桌面应用界面。 阅读详情

相关推荐

Unity技能系统核心:从理论到实践构建可扩展的Buff管理器

本文深入探讨了Unity游戏开发中构建可扩展技能系统的核心——Buff管理器。通过剖析三层架构(数据、逻辑、渲染)与独立组件管理模式,提供了从Buff数据定义、管理器核心实现到可视化编辑器搭建的完整实践方案,旨在解决技能系统维护难题,提升开发效率与代码质量。

weixin_29002209的博客 263

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

NineFailure的博客 501

LED背光驱动电路解析:从MP3394芯片到液晶屏灯管控制

本文深入解析了以MP3394芯片为核心的LED背光驱动电路工作原理。从升压电路的能量转换到多路LED的均流控制,详细阐述了其作为液晶屏背光“大脑”的关键作用,并提供了黑屏、闪屏、亮度不均等常见故障的实战检修思路与排查方法。

agile9scrum的博客 855

Codeforces - Friends and Subsequences

题目链接:Codeforces - Friends and Subsequences 很显然max和min的前缀和具有单调性。 所以我们枚举左端点,二分右端点即可。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define int long long usin...

青烟绕指柔的博客 506

primetime如何做voltage scaling

存在这样的情况,标准单元库只有ssgnp0p72vm40c和ssgnp0p9vm40c的libs,但是我们希望它工作在ssgnp0p76vm40c,这样的话就需要用ssgnp0p72vm40c和ssgnp0p9vm40c的libs来scaling出ssgnp0p76vm40c的lib。

qq_34326957的博客 1706

DeepStream实战:多路视频AI分析从CPU瓶颈到GPU高吞吐

视频流智能分析在智慧交通和边缘计算中愈发关键,但传统CPU拉流解码加GPU推理的链路,常面临CPU占用过高、GPU利用率低下的窘境。DeepStream作为NVIDIA推出的流式推理框架,基于GStreamer插件生态,以NVMM内存管理让视频数据在GPU侧高效流转,配合NVDEC硬件解码、NvStreamMux多路批处理与TensorRT优化,大幅降低CPU负担并提升多路视频并行吞吐。针对1080p甚至更高分辨率的RTSP流,它能将多路帧汇聚为batch批量推理,从而支撑实时车辆识别、道路分析等生产级应用

weixin_29051245的博客 191

【CodeForces - 689D】Friends and Subsequences(RMQ,二分 或单调队列)

题干: 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...

xuanweiace的博客 516

别再只会插卡了!用示波器实测SIM卡上电时序与通信波形(附故障排查)

本文深入解析了SIM卡通信的电气信号与时序规则,通过示波器实测波形揭示SIM卡上电时序与通信协议的关键细节。从VCC、CLK、RST、DATA四路信号的协同工作,到ATR(Answer to Reset)的解析,再到常见故障的波形诊断与排查方法,为硬件工程师提供了实用的调试指南。文章还分享了高级调试技巧与现代化工具链搭建建议,帮助提升物联网设备SIM卡通信的可靠性与效率。

weixin_42567279的博客 346

[Codeforces 689D] Friends and Subsequences (二分+稀疏表)

Codeforces - 689D 给定两个序列 AA,BB,求问有多少个区间 [l,r][l,r]满足 max(A[l,r])=min(B[l,r])max(A[l,r]) = min(B[l,r]) 考虑枚举区间左端点,移动右端点的过程中 max(A[l,r])max(A[l,r])肯定越来越大,min(B[l,r])min(B[l,r])越来越小 用 d=max(A[l,r])−

MisDeer's blog 710

智能车图像处理20-进阶篇12--正入十字补线1

这篇文章主要讲述基本的正入十字补线方法。

taiyuezyh的博客 6974

msfvenom制作windows/linux/android/ios木马

msfvenom选项如下 -p, --payload<payload> 指定 payload(攻击荷载)。如果使用自定义payload,请使用'-'或者stdin指定。 -l, --list [module_typel列出指定模块的所有可用资源。模块类型包括:payloads, encoders, nops, all -n, --nopsled <length>为payload预先指定一个NOP滑动长度。 -f, --format <format>指定输出格式(使

Cwillchris的博客 2742

Codeforces 689 D - Friends and Subsequences(数据结构 RMQ +二分)

题目链接 看过来 题意 给定两个长度为nnn的数列a,ba,ba,b,问有多少个区间[L,R](0≤L≤R&amp;lt;n)[L,R](0≤L≤R&amp;lt;n)[L,R] (0\leq L \leq Rmax(al,al+1,...,ar)=min(bl,bl+1,...,br)max(al,al+1,...,ar)=min(bl,bl+1,...,br)max(a_l,a_{l+1},.....

MasterAn的博客 396

【论文解读--Xdog】新型电驱式四足机器人研制与测试

系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录系列文章目录前言一、四足机器人体型大小和驱动方式的比较二、四足机器人设计一些重要的结论三、系统控制框架四、系统硬件框架五、力位混合控制(阻抗控制)六、侧向平衡控制(和VMC如出一辙)七、电机驱动系统研制(FOC磁场定向控制)总结 前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对新型电驱式四足机器人研制与测试做个简单的介绍,具体内容后续再更,其他模块可以参

qq_35635374的博客 3740

CF689D Friends and Subsequences(ST表+二分)

题目传送门 题意 给定序列 aaa 和序列 bbb,长度均为 nnn。问有多少组 (l,r)(l,r)(l,r),满足 1≤l≤r≤n1\le l\le r\le n1≤l≤r≤n 且 max⁡i=lrai=min⁡i=lrbi\max_{i=l}^r a_i=\min_{i=l}^r b_ii=lmaxr​ai​=i=lminr​bi​ 1≤n≤2×1051\le n\le 2\times 10^51≤n≤2×105,∣ai∣,∣bi∣≤109|a_i|,|b_i|\le 10^9∣ai​∣,∣bi​∣

m0_51841071的博客 482

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

WINCC7.3破解文件

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

【学习笔记】线段树二分

这是真正的线段树上二分

cqbzlydd的博客 7047

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

CodeForces 689 D Friends and Subsequences

Friends and Subsequences 题解: 如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求第一个 a[i] == b[i] 的地方, 求最后一个a[i] == b[i]的地方。 代码: #include<bits/stdc++.h> using...

dengpangbu3703的博客 154

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

weixin_30677073的博客 145

codeforces 689 D. Friends and Subsequences

ST 表 + 二分

m0_69735030的博客 182

Codeforces D. Friends and Subsequences [RMQ + erfen]

这道题的题目链接 题目的大意是给出两个数组ai,bi" role="presentation">ai,biai,bia_i,b_i,判断有多少个区间[l,r]" role="presentation">[l,r][l,r][l,r],使得数组a在这个区间中的最大值,等于数组B在区间中的最小值 首先不对数组进行操作我们可以用ST表来实现取最值的操作。其次,我们要知道假设固定区间的左端点,最大值是

limn204的专栏 269

Codeforces 689D Friends and Subsequences

题目链接:http://codeforces.com/problemset/problem/689/D 题意:给两个长度为n的数组a,b,求有多少个区间[l,r]满足max(al,al+1,...ar) = min( bl,bl+1,...br )。 思路:随着右端点的增大,max()的值是不减的,而min()的值是不增的,所以如果我们固定了左端点,就可以通过二分来确定右端点两

DS-K的博客 738

Codeforces Round #361(div 2)

A题题目意思很简单,问一种拨号的方式(拨号手势)是不是能拨出唯一的号码(例如253就不是唯一的,因为586也是可以的) 记录电话上每个格子上下左右是否还有格子,一个拨号手势是唯一的当且仅当,所拨号码的所有格子在同一个方向不同时有格子相邻。 那么直接mark,判断一下即可: #include <cstdio> using namespace std; char s[1000...

weixin_30240349的博客 144

Friends and Subsequences【Codeforces 689 D】【线段树+二分答案】

Codeforces Round #361 (Div. 2) D 我们现在有两个长度为N的区间,我们现在想知道满足在第一个区间的最大值与第二个区间的最小值相等的区间的这样的区间个数有多少个? 首先,我们可以知道,区间的最大值如果按某个端点来升序的话一定是线性关系的,同理最小值,所以我们完全可以枚举一维,然后二分一维,又有查询区间最值的复杂度是log()级别的,所以总的复杂度就是。 ...

既然弱小,就只顾变强就是了 277

Codeforces Round #361 (Div. 2) ABCDE题解

A. Mike and Cellphone 给出按手机键盘的顺序,判断号码是否唯一。 必须同时有按键出现在键盘的边缘,号码才会唯一。0也算边缘。 #include #include #include #include #include #include #include #include #include #include #include #include #inc

Noooooorth的专栏 742

CF689D:Friends and Subsequences(ST表 + 二分)

D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Mike and !Mike are old childhoo

junior19的博客 532

codeforces_689D. Friends and Subsequences(RMQ+二分)

D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Mike and !Mike are old childhoo

凳子 536

Codeforces-689 D Friends and Subsequences(RMQ+二分)

http://codeforces.com/problemset/problem/689/D 题目大意: 给两个序列a,b   问有多少个相同的区间 在这个区间内a序列的最大值等于b序列的最小值  分析: 首先要解决区间问题  当然用RMQ就行  预处理O(n*log(n))    询问O(1)      然后就是

MM__1997的博客 777
上一篇: Hdu 4734 F(X) 数位dp
下一篇: Codeforces Round #271 (Div. 2) F.Ant colony(线段树 + 统计区间内某数的个数)
dexicuo5991
博客等级 码龄11年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值