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 ...
随机推荐
- UINavigationController学习笔记
http://site.douban.com/129642/widget/notes/5513129/note/187701199/ 1-view controllers的关系:Each custom ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- VMware下Ubantu与Windows共享文件夹的方法
刚刚接触linux的同学往往喜欢在windows系统下安装一个虚拟机,然后在虚拟机上进行操作,但是windows和虚拟机上的linux系统之间的文件互传往往不太方便,今天就总结一个小技巧在window ...
- 数据库MySQL-Oracle-DB2-SQLServer分页查询
1. MySQL分页查询 (1)关键字: LIMIT beginIndex, maxRow (2)示例: LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数. 如果给出 ...
- MyEclipse中使用JUnit进行单元测试
1. 下载JUnit的jar文件,下载地址在这里 2. 在MyEclipse中新建一个要测试的项目HelloJUnit 3. 添加一个要测试的类HelloJUnit,代码如下,注意需要先建packag ...
- poj 2115 C Looooops(扩展gcd)
题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...
- cf 151 C. Win or Freeze (博弈 求大数质因子)
题目 题意: 给一个数N,两人轮流操作每次将N变为一个N的非1非自身的因数,第一个无法进行操作的人获胜问先手是否有必胜策略,如果有的话在第二行输出第一步换成哪个数,如果第一步就不能操作则输出0数据规模 ...
- UVA 11426 GCD-Extreme(II) ★ (欧拉函数)
题意 求Σ{1<=i<N} Σ{i<j<=N} GCD(i, j) (N<=4000000) 分析 原始思路 暴力求明显是不行的,我们把式子简化形式一下发现它可以 ...
- HDU 5289 Assignment (数字序列,ST算法)
题意: 给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”.k是给定的. 思路: 如果穷举,有O(n*n)复杂度.可以用ST算法先预处理每个区间最大和最小,O(n ...
- linux - markdown编辑器
1. linux可以用web-qq,http://web2.qq.com,[我们从未放弃成长,这句话挺感动我的.] (禽兽!你怎么在一开始就跑题!?) ————我只要“及时预览”———— 2. htt ...