博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
阅读量:5297 次
发布时间:2019-06-14

本文共 2198 字,大约阅读时间需要 7 分钟。

D. Friends and Subsequences

题目连接:

Description

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

Sample Input

6

1 2 3 2 1 4
6 7 1 2 3 2

Sample Output

2

Hint

题意

给你一个a数组和一个b数组

问你有多少对(l,r)满足,a数组中max(L,R)恰好等于b数组中的min(L,R)

题解

暴力枚举L,然后二分相等的那个区间就好了。

因为max肯定是递增的,min是递减的

那个相等的区间可以二分出来。

代码

#include
using namespace std;const int maxn = 2e5+7;int n;int a[maxn],b[maxn];struct RMQ{ const static int RMQ_size = maxn; int n; int ArrayMax[RMQ_size][21]; int ArrayMin[RMQ_size][21]; void build_rmq(){ for(int j = 1 ; (1<
<= n ; ++ j) for(int i = 0 ; i + (1<
b[i])continue; int l=i,r=n,ansl=i; while(l<=r){ int mid=(l+r)/2; if(s1.QueryMax(i,mid)>=s2.QueryMin(i,mid))r=mid-1,ansl=mid; else l=mid+1; } if(s1.QueryMax(i,ansl)>s2.QueryMin(i,ansl))continue; l=i,r=n; int ansr=i; while(l<=r){ int mid=(l+r)/2; if(s1.QueryMax(i,mid)>s2.QueryMin(i,mid))r=mid-1,ansr=mid; else l=mid+1; } ans+=ansr-ansl; } cout<
<

转载于:https://www.cnblogs.com/qscqesze/p/5651384.html

你可能感兴趣的文章
AVL数
查看>>
全栈12期的崛起之捡点儿有用的说说
查看>>
属性动画
查看>>
标识符
查看>>
路由跟踪工具0trace
查看>>
给大家分享一张CSS选择器优选级图谱 !
查看>>
Win7中不能调试windows service
查看>>
boost库使用:vs2013下boost::container::vector编译出错解决
查看>>
通过httplib2 探索的学习的最佳方式
查看>>
快来熟练使用 Mac 编程
查看>>
Node.js 入门:Express + Mongoose 基础使用
查看>>
plsql使用,为什么可以能看见其他用户的表
查看>>
一步步教你轻松学奇异值分解SVD降维算法
查看>>
使用pager进行分页
查看>>
UVA - 1592 Database
查看>>
Fine Uploader文件上传组件
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
consonant combination
查看>>