codeforces 361 D - Friends and Subsequences
原题:
Description
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of while !Mike can instantly tell the value of .
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r)(1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs is satisfied.
How many occasions will the robot count?
Input
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.
Output
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
Sample Input
6
1 2 3 2 1 4
6 7 1 2 3 2
2
3
3 3 3
1 1 1
0
Hint
The occasions in the first sample case are:
1.l = 4,r = 4 since max{2} = min{2}.
2.l = 4,r = 5 since max{2, 1} = min{2, 3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
提示: 暴力比对所有区间时间复杂度是(n^2)无法通过。
观察发现,如果固定区间左边界L,右边界R一次递增,a_max【L,R】是不减的,即有序。 同样b_min【L,R】是不增的,有序。
所以就可以先枚举左端点,再用二分法去寻找右端点的合法(符合题意的)区间,这个区间会是一个连续的范围。
二分的时候注意 RMQ_a(L,mid) = RMQ_b(L,mid) 的时候如何处理决定了最终结果是右端点的左边界还是右边界。
求区间最值是使用了RMQ算法。(一个很精妙的算法,我之前的博客里有写。)
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream> using namespace std; #define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))
#define ll long long const int inf = 0x7fffffff;
const int maxn=2e5+; int a[maxn],b[maxn];
int d_a[maxn][];
int d_b[maxn][]; void RMQ_init_a(int *A,int n)
{
for(int i=; i<n; i++) d_a[i][]=A[i];
for(int j=; (<<j)- < n; j++)
for(int i=; i+(<<j)- < n; i++)
d_a[i][j]=MAX( d_a[i][j-], d_a[i + (<<(j-))][j-] );
} int RMQ_a(int L, int R)
{
int k=;
while( <<(k+) <= R-L+ ) k++;
return MAX( d_a[L][k], d_a[R-(<<k) + ][k] );
} void RMQ_init_b(int *A,int n)
{
for(int i=; i<n; i++) d_b[i][]=A[i];
for(int j=; (<<j)- < n; j++)
for(int i=; i+(<<j)- < n; i++)
d_b[i][j]=MIN( d_b[i][j-], d_b[i + (<<(j-))][j-] );
} int RMQ_b(int L, int R)
{
int k=;
while( <<(k+) <= R-L+ ) k++;
return MIN( d_b[L][k], d_b[R-(<<k) + ][k] );
} int main()
{
int n;
cin>>n;
for(int i=; i<n; i++) scanf("%d",a+i);
for(int i=; i<n; i++) scanf("%d",b+i);
RMQ_init_a(a,n);
RMQ_init_b(b,n);
int left, right;
ll ans = ;
for(int L=; L<n; L++)
{
int left = inf, right = -inf;
//求左边界
int l = L;
int r = n-;
while(l <= r)
{
int mid=(l + r)/;
if(RMQ_a(L,mid) > RMQ_b(L,mid)) //左半部分
r=mid-;
else if(RMQ_a(L,mid) < RMQ_b(L,mid)) //右半部分
l=mid+;
else
{
left = min(left, mid);
r=mid-;
}
}
// printf("left = %d\n",left);
//求右边界
l = L;
r = n-;
while(l <= r)
{
int mid=(l + r)/;
if(RMQ_a(L,mid) > RMQ_b(L,mid)) //左半部分
{
r=mid-;
}
else if(RMQ_a(L,mid) < RMQ_b(L,mid)) //右半部分
l=mid+;
else
{
right = max(right, mid);
l=mid+;
}
}
// printf("right = %d\n",right);
if(left != inf)
ans += right - left + ;
}
cout<<ans<<endl; return ;
}
codeforces 361 D - Friends and Subsequences的更多相关文章
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- codeforces 361 D. Levko and Array(dp+二分)
题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...
- codeforces 361 C. Levko and Array Recovery(暴力+思维)
题目链接:http://codeforces.com/contest/361/problem/C 题意:对一个数列有这么两个操作 1.(1,l,r,p)..将区间[l,r]所有数都加上p 2.(2,l ...
- 套题 codeforces 361
A题((Mike and Cellphone) 看起来好像需要模拟数字键位的运动,可是,只要判断出那些必然YES的数字组合不就好了么 #include <cstdio> #include ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- 【codeforces 314C】Sereja and Subsequences
[题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...
- CodeForces 689 D Friends and Subsequences
Friends and Subsequences 题解: 如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求 ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
随机推荐
- nat转换
实验目的: (1) 了解nat转换 (2) 了解nat转换配置命令 (3) 了解哪些是私有ip地址哪些不是私有ip地址 实验工具: 华为eNSP模拟器和Wireshar 实验拓 ...
- python数据结构与算法——图的最短路径(Floyd-Warshall算法)
使用Floyd-Warshall算法 求图两点之间的最短路径 不允许有负权边,时间复杂度高,思路简单 # 城市地图(字典的字典) # 字典的第1个键为起点城市,第2个键为目标城市其键值为两个城市间的直 ...
- Flume 实战(1) -- 初体验
前言: Flume-ng是数据收集/聚合/传输的组件, Flume-ng抛弃了Flume OG原本繁重的zookeeper和Master, Collector, 其整体的架构更加的简洁和明了. 其基础 ...
- 【NOIP2005】过河
感觉这题好玄--最后看了chty的代码才过,我现在这样必须看题解才能A题怎么办嘛qaq 原题: 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...
- QSqlTableModel 使用方法(转)
Qt QSqlTableModel 使用心得 连接数据库 执行sql查询,条件显示,排序 获取记录数,列数以及记录内容,字段内容 新增,修改,删除,恢复 其它 1---------------连接数据 ...
- 抓包工具PowerSniff-0.1
做这个程序的意图是wireshark插件编写复杂(虽然也支持lua),而轻量级的工具如smartsniff,minisniff不支持插件化数据分析,各种工具用下来或多或少不顺手.以前写的外挂也都是手工 ...
- nginx入门到精通目录
1.nginx入门篇 nginx安装与基础配置 nginx优化配置分析与说明 nginx模块结构 2.nginx功能篇 配置nginx的gzip功能 配置nginx的rewrite功能 配置nginx ...
- Docker之功能汇总
Docker-给容器做端口映射 基本的命令是 -P(大写) :Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口基本的命令是 -p(小写) :Docker则可以指定要 ...
- SQL:查找被锁的表,以及锁表的SQL语句(重点推荐)
--死锁检测 use master Select * --找到SPID exec sp_lock --根据SPID找到OBJID ) --根据OBJID找到表名 1.DatabaseName 同于你要 ...
- access remote libvirtd
访问远程libvirtd服务因为是在一个可信环境中运行,所以可以忽略安全方面的操作,步骤如下:(1)更改libvirtd配置 1.1 更改/ect/sysconfig/libvirtd文件,打开 ...