枚举,二分,$RMQ$。

对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的。

因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置${{p_1}}$,使得$\mathop {\max }\limits_{i = L}^{{p_1}} {a_i} = \mathop {\min }\limits_{i = L}^{{p_1}} {b_i}$;再次二分找到最后一个位置${{p_2}}$,使得$\mathop {\max }\limits_{i = L}^{{p_2}} {a_i} = \mathop {\min }\limits_{i = L}^{{p_2}} {b_i}$。那么以$L$为左端点的区间,有${{p_2}}-{{p_1}}+1$个。查询区间最值的话可以倍增预处理一下。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ; while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int a[maxn],b[maxn],n;
int MAX[maxn][],MIN[maxn][]; void RMQ_init()
{
for(int i=;i<n;i++) MAX[i][]=a[i],MIN[i][]=b[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++)
MAX[i][j]=max(MAX[i][j-],MAX[i+(<<(j-))][j-]),
MIN[i][j]=min(MIN[i][j-],MIN[i+(<<(j-))][j-]);
} int RMQ_MAX(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
return max(MAX[L][k],MAX[R-(<<k)+][k]);
} int RMQ_MIN(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
return min(MIN[L][k],MIN[R-(<<k)+][k]);
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) scanf("%d",&b[i]);
RMQ_init(); LL ans=;
for(int i=;i<n;i++)
{
if(b[i]<a[i]) continue;
int p1=-,p2=-;
int L=i,R=n-;
while(L<=R)
{
int mid=(L+R)/;
int mx=RMQ_MAX(i,mid),mn=RMQ_MIN(i,mid);
if(mx>mn) R=mid-;
else if(mx==mn) R=mid-,p1=mid;
else L=mid+;
} L=i,R=n-;
while(L<=R)
{
int mid=(L+R)/;
int mx=RMQ_MAX(i,mid),mn=RMQ_MIN(i,mid);
if(mx>mn) R=mid-;
else if(mx==mn) L=mid+,p2=mid;
else L=mid+;
} if(p1==-) continue;
ans=ans+(LL)(p2-p1+);
}
printf("%lld\n",ans); return ;
}

CodeForces 689D Friends and Subsequences的更多相关文章

  1. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  2. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  3. codeforces 689D D. Friends and Subsequences(RMQ+二分)

    题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...

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

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

  5. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  6. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  7. 【codeforces 803F】Coprime Subsequences

    [题目链接]:http://codeforces.com/contest/803/problem/F [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] ...

  8. 689D Friends and Subsequences RMQ+二分

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

  9. [Codeforces 946F]Fibonacci String Subsequences

    Description 题库链接 定义 \(F(x)\) 为 \(F(x-1)\) 与 \(F(x-2)\) 的连接(其中 \(F(0) = "0",F(1) = "1& ...

随机推荐

  1. Unity Container

    Unity Container中的几种注册方式与示例 2013-12-08 22:43 by 小白哥哥, 22 阅读, 0 评论, 收藏, 编辑 1.实例注册 最简单的注册方式就是实例注册,Unity ...

  2. C/C++基础知识总结——数据的共享与保护

    1. 标识符的作用域与可见性 1.1 作用域 标识符的作用域包括:函数原型作用域.局部作用域.类作用域.命名空间作用域 (1) 函数原型作用域:函数的参与的作用域就是从函数的开始到结束 (2) 局部作 ...

  3. [转]Patching the Mach-o Format the Simple and Easy Way

    From:http://secureallthethings.blogspot.jp/2014/08/patching-mach-o-format-simple-and-easy.html I'm r ...

  4. PHP实现一个简单url路由功能

    如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染.很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的 ...

  5. sql基础篇

    再跟SQL谈一谈--基础篇   1.简介 2.DDL & DML 3.SELECT ①DISTINCT ②WHERE ③AND & OR ④ORDER BY 4.INSERT 5.UP ...

  6. 搜索广告与广告网络Demand技术-探索与利用

    探索与利用(Explore and exploit) 点击率预测中还有一个重要的问题,就是探索与利用,它在工程中解决的并不好,我这章把现在论文中的常见的几种方法介绍一下.探索与利用它是所有互联网应用都 ...

  7. jQuery焦点图切换特效插件封装

    网站焦点图是一种网站内容的展现形式,可简单理解为一张图片或多张图片展现在网页上就是网站焦点图.在网站很明显的位置,用图片组合播放的形式,类似焦点新闻的意思只不过加上了图片.一般多使用在网站首页版面或频 ...

  8. 一步步学习Python-django开发-Mac下搭建Python-Django环境

    首先确定学习那一个版本,以之前对Python的接触,Python2.x 的版本仍然占据很大的市场.但作为初学者我还是决定学习新的版本,相信未来会逐渐转向3.x版本的. 其实搭建Python不是一件很难 ...

  9. 图像资源Images Assets

    图像资源Images Assets 开始之前,首先回顾一下iOS7初体验(1)——第一个应用程序HelloWorld中的一张图,如下所示: 本文便分享一下Images.xcassets的体验~_~ 1 ...

  10. 基于Log4net插件

    基本代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using log ...