CodeForces 689D Friends and Subsequences
枚举,二分,$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的更多相关文章
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
- CF 689D - Friends and Subsequences
689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- 【22.48%】【codeforces 689D】Friends and Subsequences
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- CodeForces - 314C Sereja and Subsequences (树状数组+dp)
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...
- CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...
- 【codeforces 803F】Coprime Subsequences
[题目链接]:http://codeforces.com/contest/803/problem/F [题意] 给你一个序列; 问你这个序列里面有多少个子列; 且这个子列里面的所有数字互质; [题解] ...
- 689D Friends and Subsequences RMQ+二分
题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...
- [Codeforces 946F]Fibonacci String Subsequences
Description 题库链接 定义 \(F(x)\) 为 \(F(x-1)\) 与 \(F(x-2)\) 的连接(其中 \(F(0) = "0",F(1) = "1& ...
随机推荐
- 历年noip复赛试题整合
早晨打算把历年的试题都过一遍,整理一下大概会往哪个方向考,考什么,不说太多,开始吧 2013: Day1: T1 转圈游戏 : 快速幂(关键在于要会打 快速幂) 思路:因为每次都进m位,相当于每次x加 ...
- 设计适用于SOA的类型(二)
接着上次的,以扩展String类型为例,继续根据自己的使用习惯闭门造车! 这一次我重载了运算符 = 和 + ,我觉得这样更方便我的使用.首先先贴代码: /// <summary> // ...
- Python基础-类的探讨(class)
Python基础-类的探讨(class) 我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法 Python ...
- ios7上隐藏status bar
在iOS7上 对于设置status bar 又有了点点的改变 1.对于 UIViewController 加入了动态改变 status bar style的方法 - (UIStatusBarStyle ...
- 关于MyEclipse SVN显示资源历史记录乱码问题
使用SVN查看历史记录进行文件对比的时候,有时会出现乱码问题,如下图: 解决办法:打开Window-->Preferences 如下: 原来默认的是GBK,改为UTF-8即可
- WCF服务承载
WCF服务承载(笔记) 自托管(也做自承载) 承载 WCF 服务最灵活.最便捷的方法就是进行自承载.要能够自承载服务,必须满足两个条件.第一,需要 WCF 运行时:第二,需要可以承载 Servic ...
- LINUX下编译安装最新版本mysql
通过参考其他文章 1.下载安装mysql-5.5.30.tar.gz与cmake.2.8.11.2.tar.gz (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ ...
- iOS 指南针的制作 附带源码
iOS 指南针的制作 附带源码 代码下载地址: http://vdisk.weibo.com/s/HK4yE http://pan.baidu.com/share/link?shareid=7 ...
- IDEA异常解决: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
有时候解决问题不仅仅是解决问题.-----jstarseven 最近因为开发需要,需要搭建一个ssm开发框架,采用了开发工具IDEA. 整合完了SSM开发框架之后,发布的时候出现org.apache. ...
- 结构-行为-样式-Js排序算法之 直接插入排序
最新因工作原因需要接触到算法,之前学习C++的时候有接触过算法,Javascript中实现算法其实也是大同小异.下面我讲下第一个实现的排序算法--直接插入排序.基本实现思路:假定一个数组中前n(n&g ...