codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接:
2 seconds
512 megabytes
standard input
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?
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.
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
6
1 2 3 2 1 4
6 7 1 2 3 2
2
3
3 3 3
1 1 1
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+二分)的更多相关文章
- 689D Friends and Subsequences RMQ+二分
题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...
- 【22.48%】【codeforces 689D】Friends and Subsequences
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- *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 ...
- 玲珑杯 Round 19 B Buildings (RMQ + 二分)
DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
随机推荐
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- NOIP2014D2T2寻找道路(Spfa)
洛谷传送门 这道题可以把边都反着存一遍,从终点开始深搜,然后把到不了的点 和它们所指向的点都去掉. 最后在剩余的点里跑一遍spfa就可以了. ——代码 #include <cstdio> ...
- 使用Sencha Architect开发Sencha Touch应用的整理
官网:http://www.sencha.com/ 其实官网上的文档都很清楚了,不过整理一下总比较好 第一步,软件准备 注: 以下软件的安装本着这样两条原则 一是不要安装在中文目录下 二是不要安装在带 ...
- POJ 3036 Honeycomb Walk
http://poj.org/problem?id=3036 在每一个格子可以转移到与这个各自相邻的六个格子 那么设置转移变量 只需要六个 int d[6][2] = {-1, 0, -1, 1, 0 ...
- msp430项目编程05
msp430中项目---TFT彩屏显示(续) 1.TFT彩屏工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习
- 简述WEB项目前端脚本的一次重构历程,labJs,requireJs实践[转载]
重构前的状态: 大量的js代码混在繁多的Jsp文件中,对第三方的js库依赖也很杂乱.虽然在部分交互性较强的页面中,将js代码分离到了独立的js文件中,但是代码结构及依赖管理依然很乱.不说新人来了 ...
- 免费第三方API平台整合
各大平台免费接口,非常适用 http://developer.51cto.com/art/201412/458778.htm 绝对干货:供个人开发者赚钱免费使用的一些好的API接口http://www ...
- 关于MySQL的boolean和tinyint(1)
原文:http://blog.csdn.net/woshixuye/article/details/7089508 MySQL保存boolean值时用1代表TRUE,0代表FALSE.boolean在 ...
- C# DataGridView,右键单击RowHeader时显示右键菜单怎么做?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { ...
- [转] Scalers:刻意练习的本质就是持续行动+刻意学习
原文: http://www.scalerstalk.com/1264-peak-conscious ------------------------------------------------- ...