题目链接:

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

题意:

在一个区间[l,r]中a的最大值等于b的最小值,问这样的区间有多少个;

思路:

枚举左端点,二分找到右端点可行区间的左右边界;
在确定右段点的左右边界时,要用RMQ,
左边界:要是amax>=bmin,左移;否则右移,找到第一个amax=bmin的点;
右边界:要是amax>bmin,左移,否则右移,找到最后一个amax=bmin的点; 累加右端点可行区间长度即可; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=;
const double eps=1e-; int a[N],b[N],MX[N][],MN[N][],n;
struct Tree
{
int l,r;
int mmax,mmin;
}tr[*N]; void build(int o,int L,int R)
{
for(int i=;i<=n;i++)
MX[i][]=a[i],MN[i][]=b[i];
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<=n;i++)
{
MX[i][j]=max(MX[i][j-],MX[i+(<<(j-))][j-]);
MN[i][j]=min(MN[i][j-],MN[i+(<<(j-))][j-]);
}
}
}
int query(int o,int L,int R,int flag)
{
if(flag)
{
int k = ;
while( (<<(k+)) <= R-L+) k ++ ;
return max(MX[L][k],MX[R-(<<k)+][k]);
}
else
{
int k = ;
while( (<<(k+)) <= R-L+) k ++ ;
return min(MN[L][k],MN[R-(<<k)+][k]);
}
}
int check(int x,int y,int flag)
{
int mx=query(,x,y,),mn=query(,x,y,);
if(flag){ if(mx==mn)return ;
else if(mx>mn)return ;
return ;}
else
{
if(mx==mn)return ;
return ;
}
}
int main()
{
read(n);
For(i,,n)read(a[i]);
For(i,,n)read(b[i]);
build(,,n);
LL ans=;
int L,R;
For(i,,n)
{
int l=i,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(!check(i,mid,))l=mid+;
else r=mid-;
}
L=l;
if(check(i,L,)==)continue;
l=L,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(i,mid,))l=mid+;
else r=mid-;
}
R=l-;
if(R>=L)ans=ans+(R-L+);
}
cout<<ans<<"\n";
return ;
}

codeforces 689D D. Friends and Subsequences(RMQ+二分)的更多相关文章

  1. 689D Friends and Subsequences RMQ+二分

    题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...

  2. 【22.48%】【codeforces 689D】Friends and Subsequences

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  3. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  5. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  7. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  8. 玲珑杯 Round 19 B Buildings (RMQ + 二分)

    DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...

  9. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

随机推荐

  1. The more, The Better(树形DP)

    Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有 ...

  2. Hadoop JVM调整解决 MapReduce 作业超时问题

    摘要:由于业务需要,在mapreduce汇总时需要关联两个基础表,一个60M左右,不影响mr运行,另一个表,大小约为380M,行数为1700万行左右,在默认配置下,一旦加载这个数据就会在reduce阶 ...

  3. POJ 3667 线段树的区间合并简单问题

    题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“ ...

  4. android L版本AAL新架构

    [DESCRIPTION] 和之前KK版本相比,在L版本上面AAL的架构也有发生一些改变. 拿掉了之前KK平台上使用的MTK LABC,使用Android原生的自动背光功能. AALService内部 ...

  5. [codeVS1204] 单词背诵

    题目描述 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的只算一个).并且在背诵的单词量尽量多的情 ...

  6. DELL IDRAC API接口开发文档翻译及client模块

    今天和DELL官网要了一份关于服务器IDRAC 版本7/8 的API开发文档,花了一天的时间,进行了翻译,不一定全部准确,但对于英语不好的人会有所帮助.也不用重复造轮子了.下载链接: IDRAC AP ...

  7. React学习及实例开发(三)——用react-router跳转页面

    本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.定义路由 1.安装react-router npm install react-router@ --sav ...

  8. java序列化,看这篇就够了

    一.序列化的含义.意义及使用场景二.序列化实现的方式1.Serializable1.1 普通序列化1.2 成员是引用的序列化1.3 同一对象序列化多次的机制1.4 java序列化算法潜在的问题1.5 ...

  9. 七天从零基础学习android(1)--配置环境

    在写这篇文的时候android开发经验还是0,是一个萌新,这是一篇记录一个萌新从零android编程基础到能编写一个记账本的开发过程(至少我是这样美好的希望着的) 首先因为是没有开发基础的,直接上百度 ...

  10. Spring的AOP AspectJ切入点语法详解(转)

    一.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指示 ...