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 ...
随机推荐
- 修改mysql用户名密码 和 PHPmysqlAdmin对应密码修改
本地的mysql运行时,可能会用到修改用户名密码: mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('输入新密码');不存在修改用户啊 ...
- Android:给ViewPager添加切换效果
原文参照开发者官网:http://developer.android.com/training/animation/screen-slide.html#viewpager 以App的引导页为例: 首先 ...
- ;function($,undefined) 前面的分号是什么用处
;function($,undefined) 前面的分号是什么用处 ;(function($){$.extend($.fn...现般在一些 JQuery 函数前面有分号,在前面加分号可以有多种用途:1 ...
- dedecms搜索框制作
<form method=" name="kwtype"> <table width="> <tr> <td widt ...
- Java:String和Date、Timestamp之间的转换
一.String与Date(java.util.Date)互转 1.1 String -> Date String dateStr = "2016-9-28 12:25:55" ...
- Unity3d 引擎原理详细介绍
体系结构 为了更好地理解游戏的软件架构和对象模型,它获得更好的外观仅有一名Unity3D的游戏引擎和编辑器是非常有用的,它的主要原则. Unity3D 引擎 Unity3D的是一个屡获殊荣的工具,用于 ...
- spring mvc 4数据校验 validator
注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC http://jinnianshilongnian.iteye.com/blog/1733708Spring4新特性——集成B ...
- [转] Makefile经典教程(掌握这些足够)
目录(?)[-] Makefile 介绍 1 Makefile的规则 2 一个示例 3 make是如何工作的 4 makefile中使用变量 5 让make自动推导 6 另类风格的makefile 7 ...
- IOCP之客户端及消息传递
上篇说到IOCP的精简实现,这篇来讲IOCP客户端和消息传递 在ConnectEx代码之前,CreateIoCompletionPort的第三个参数,把socket句柄+0x01000000作为传递 ...
- linux 下 文件权限和文件主
文件与文件夹的权限和所有者 1.chmod -R 755 file 777 含义与来源: 777含义:分别为:所有者.同组用户.其他用户 7的来源:文件有三种操作模式:读4.写2.执行1,分别值为42 ...