CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/H
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 max(alar)==min(blbr) is satisfied.
Sample Input
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
Hint
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]满足max(alar)==min(blbr);
题解:
RMQ:O(nlgn)预处理 O(1)求出任意区间的min/max;
在固定区间左端点情况下:
由于最大值最小值均具有单调性;
用两次二分操作分别求出第一次和最后一次满足min==max的右端点,作差累加即可;
注意:两次二分操作的差别和意义.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n, A[maxn], B[maxn];
int d_min[maxn][30];
int d_max[maxn][30];
void RMQ_init() {
for(int i=0; i<n; i++) d_max[i][0] = A[i], d_min[i][0] = B[i];
for(int j=1; (1<<j)<=n; j++)
for(int i=0; i+(1<<j)-1<n; i++) {
d_min[i][j] = min(d_min[i][j-1], d_min[i+(1<<(j-1))][j-1]);
d_max[i][j] = max(d_max[i][j-1], d_max[i+(1<<(j-1))][j-1]);
}
}
int RMQ_min(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
return min(d_min[L][k], d_min[R-(1<<k)+1][k]);
}
int RMQ_max(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
return max(d_max[L][k], d_max[R-(1<<k)+1][k]);
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d",&n) != EOF)
{
for(int i=0; i<n; i++) scanf("%d",&A[i]);
for(int i=0; i<n; i++) scanf("%d",&B[i]);
RMQ_init();
LL ans = 0;
for(int i=0; i<n; i++) {
if(A[i] > B[i]) continue;
int first_r=-1, last_r=-1;
int l=i,r=n-1,mid;
while(l <= r) {
mid = (l+r) / 2;
if(RMQ_max(i,mid) == RMQ_min(i,mid)) first_r = mid;
if(RMQ_max(i,mid) >= RMQ_min(i,mid)) r = mid-1;
else l = mid+1;
}
if(first_r == -1) continue;
l=i; r=n-1;
while(l <= r) {
mid = (l+r) / 2;
if(RMQ_max(i,mid) > RMQ_min(i,mid))
r = mid-1;
else l = mid+1, last_r = mid;
}
ans += last_r - first_r + 1;
}
printf("%I64d\n", ans);
}
return 0;
}
CodeForces 689D Friends and Subsequences (RMQ+二分)的更多相关文章
- 689D Friends and Subsequences RMQ+二分
题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- CodeForces 689D Friends and Subsequences
枚举,二分,$RMQ$. 对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的. 因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置$ ...
- CF 689D - Friends and Subsequences
689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...
- *HDU3486 RMQ+二分
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
- HDU 5089 Assignment(rmq+二分 或 单调队列)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
随机推荐
- .net 程序员成长路线图?
https://www.zhihu.com/question/25474641 得看赵四本, @赵劼 推荐的. CLR via C# .net Essentials C# in Depth Frame ...
- TeeChart的X轴为时间,多个Y轴的显示
最后上代码 public partial class Test : Form { private TChart tChart = new TChart(); ; public Test() { Ini ...
- 1176. Hyperchannels(欧拉回路)
1176 给定一有向图 求其反图的欧拉回路 路径输反了 一直WA.. #include <iostream> #include<cstdio> #include<cstr ...
- svn is already under version control问题解决
svn ci 时出现 xx is already under version control,然后无法提交,出现这个问题的原因是你所提交的文件或目录是其他SVN的东西,即下面有.svn的目录,需要先把 ...
- sql给数据库加锁问题
加锁是在操作数据时进行了,不能事后加锁. 例: begin tran insert 表 with(TABLOCKX) --加锁 (字段列表) ...
- ioctl用法详解 (网络)
本函数影响由fd参数引用的一个打开的文件. #include#include int ioctl( int fd, int request, .../* void *arg */ );返回0:成功 ...
- Android 获取本机WIFI及3G网络IP
获取本机WIFIprivate String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemServic ...
- java web 学习十三(使用session防止表单重复提交)
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- Java SE 6 新特性: 对脚本语言的支持
2006 年底,Sun 公司发布了 Java Standard Edition 6(Java SE 6)的最终正式版,代号 Mustang(野马).跟 Tiger(Java SE 5)相比,Musta ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 page_should_contain_list(self, locator, message='', loglevel='INFO')
def page_should_contain_list(self, locator, message='', loglevel='INFO'): """Verifies ...