枚举,二分,$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. MongoDB应用介绍之前

    MongoDb企业应用实战(一) 写在MongoDB应用介绍之前   故事背景: 本人有幸,经老友( 现为x知名快递公司技术总监 ) 推荐进入中国前三大民营快递公司之一工作,在此非常感谢他,在此也非常 ...

  2. Hadoop将本地文件复制到Hadoop文件系统

    代码: package com.hadoop; import java.io.BufferedInputStream; import java.io.FileInputStream; import j ...

  3. Dotfuscator注册码和XenoCode注册码

    .net加密工具注册码:Dotfuscator注册码如下:     Your   serial   number   is   14705     Your   Confirmation   Numb ...

  4. 用django搭建一个简易blog系统(翻译)(一)

    Django 入门 原始网址: http://www.creativebloq.com/netmag/get-started-django-7132932 代码:https://github.com/ ...

  5. struts整合spring整合hibernate

    1, web.xml中配置spring监听类 <listener> <listener-class>org.springframework.web.context.Contex ...

  6. Web API CSRF保护实现

    Web API CSRF保护实现 这次自己实现了类似jQuery中ajax调用的方法,并且针对RESTFul进行了改造和集成,实现的A2D AJAX接口如下: $.ajax.RESTFulGetCol ...

  7. 如何优化运行在webkit上的web app

    如何优化运行在webkit上的web app 近来公司有个web app 项目运行在移动版android系统上,发现在电脑上跑的很流畅的web页面在移动版webkit上非常不流畅.根本无法和nativ ...

  8. T-SQL 临时表、表变量、UNION

    T-SQL 临时表.表变量.UNION 这次看一下临时表,表变量和Union命令方面是否可以被优化呢? 阅读导航 一.临时表和表变量 二.本次的另一个重头戏UNION 命令 一.临时表和表变量 很多数 ...

  9. Python Tutorial - Parse JSON Objects with Python

    This tutorial is copied from youtube.com Here is the link: http://www.youtube.com/watch?v=RXqo3lC-JP ...

  10. MongoDb注意事项

    1.保证足够大的内存. 2.Mongodb跟mysql一样,对于常用的查询条件,该建索引的建索引.不要认为内存够用,数据就会在内存,有时,内存够用,数据也不一定在内存. 3.使用副本集,一个主库,进行 ...