枚举,二分,$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. 受益匪浅的材料orz

    看到了xhr大神制作的材料 仰慕良久,受益匪浅,必将永记在心,蒟蒻长跪不起 考前一定要膜拜!

  2. C#通过接口与线程通信(捕获线程状态)介绍

    C#通过接口与线程通信(捕获线程状态)介绍 摘要:本文介绍C#通过接口与线程通信(捕获线程状态),并提供简单的示例代码供参考. 提示:本文所提到的线程状态变化,并不是指线程启动.暂停.停止,而是说线程 ...

  3. mac 下nginx加入开机启动

    通过brew install nginx后设置开机启动项 sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemonssudo launchc ...

  4. mac 下安装oh my zsh

    1.直接从github上下载 git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh  2.拷贝到账户目录下 cp ~/. ...

  5. Python日期操作

    1. 日期输出格式化 所有日期.时间的api都在datetime模块内. 1. datetime => string now = datetime.datetime.now() now.strf ...

  6. [实验]通过内核Patch去掉iOS-v4.3.3的沙盒特性

    环境: 1.Mac OS X 10.9.2 2.xcode 5.1.1 3.gcc 4.8 4.redsn0w 0.9.15b3 前提: 1.获取 iOS 4.3.3 的kernelcache,并解密 ...

  7. 生成GUID字符串

    //生成GUID字符串 string loginToken = System.Guid.NewGuid().ToString();

  8. Mocking framework

    [译] 什么是Mocking framework?它有什么用? 原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why ...

  9. 读取同一文件夹下多个txt文件中的特定内容并做统计

    读取同一文件夹下多个txt文件中的特定内容并做统计 有网友在问,C#读取同一文件夹下多个txt文件中的特定内容,并把各个文本的数据做统计. 昨晚Insus.NET抽上些少时间,来实现此问题,加强自身的 ...

  10. JavaScript的基本类型总结

    看了让你有收获的JavaScript的基本类型总结   Javascript是一种弱类型语言,没有明确的类型分类:网上分类的方式比较多,个人感觉不比去特别的追究细分是什么什么类型,若是能够明确的分出类 ...