枚举,二分,$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. 【ios开发】Block编程

    1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...

  2. Android Recovery模式学习体会

        最近在学习Android的Recovery模式,感觉它和Windows的安全模式很相似.两者的工作原理都是只加载少量的系统组件(内核是必须的),使系统运行在最小模式,这样就可以在不影响当前系统 ...

  3. 在MVC中添加异常增加日志

    MVC的结构非常棒,基本你能想到注入的地方都可以找到地方,譬如IActionFilter,IResultFilter,IAuthorizationFilter以及IExceptionFilter 以下 ...

  4. 启动tomcat报host-manager does not exist or is not a readable directory异常

    新安装了一个tomcat6,安装完之后在webapps下面会有一些tomcat自带的项目(ROOT.manager.host-manager...) 把这些没用的项目删掉之后,启动tomcat 报如下 ...

  5. Spring3.2 + Hibernate4.2

    Spring3.2 + Hibernate4.2 前三篇随笔中介绍了 用原生的JDBC访问数据库.一种高效的数据库连接池druid.用Spring的JDBC框架访问数据库. 本文继续介绍第三种数据库访 ...

  6. WPF实现打印功能

    WPF实现打印功能 在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示.首先在VS中编辑一个图形(如下图所示). 将需要打印的内容放入同一个< ...

  7. mssql server 函数大全

    一.字符转换函数1.ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘'括起来,但含其它字符的字符串必须用‘'括起来使用,否则会出错.2.CHAR ...

  8. VS简单注释插件——VS插件开发续

    VS简单注释插件——VS插件开发续 前些时候,我写过一篇<VS版权信息插件——初试VS插件开发小记>分享过一个用于添加注释信息的插件,但那个插件有几个问题: 不能添加带块注释(/**/), ...

  9. tmux tutorial

    This is a great tutorial about tmux quick start: http://www.youtube.com/watch?v=wKEGA8oEWXw&nore ...

  10. Eclipse代码自动提示设置

    以前百度过如何设置Eclipse代码自动提示,但是本人记性不好,所以把这个方法写成一篇日志,这样以后就不用百度了,直接看自己的博客就是了,而且还增加了自己博客的点击量.以下是从各个地方看到总结的方法: ...