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 exactlyn(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

Input
6
1 2 3 2 1 4
6 7 1 2 3 2
Output
2
Input
3
3 3 3
1 1 1
Output
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的序列a和b,问有多少个区间[L,R]满足max<a>[L,R] == min<b>[L,R]。

分析:

枚举左端点,二分找到右端点可行区间的左右边界;二分右端点需要用RMQ(RQM预处理和查询

的相关知识点需要另外了解)。左边界:要是a>=b,左移;否则右移,找到第一个a=b的点;右边界:要

是a>b,左移,否则右移,找到最后一个a=b的点;最后累加右端点可行区间长度;

AC的代码:

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
int rmq[][][];
void RMQ(int n)
{
for(int k = ; (<<k) <= n; ++k)
for(int i = ; i+(<<k) <= n; ++i)
{
rmq[][k][i] = max(rmq[][k-][i],rmq[][k-][i+(<<(k-))]);
rmq[][k][i] = min(rmq[][k-][i],rmq[][k-][i+(<<(k-))]);
}
}
int Search(int pos,int l,int r)
{
int k = log((r-l+)*1.0)/log(2.0);
if(pos) return min(rmq[pos][k][l],rmq[pos][k][r-(<<k)+]);
else return max(rmq[pos][k][l],rmq[pos][k][r-(<<k)+]);
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&rmq[][][i]);
for(int i=;i<n;i++)
scanf("%d",&rmq[][][i]);
RMQ(n);
LL ans=;
int l,r,a,b,s,e;
for(int i=;i<n;i++)
{
l=i;
r=n-;
s=-;
while(l<=r)
{
int mid=(l+r)/;
a=Search(,i,mid);
b=Search(,i,mid);
if(a>=b)
{
if(a==b) s=mid;
r=mid-;
}
else
l=mid+;
}
if(s==-) continue;
l=i;
r=n-;
e=-;
while(l<=r)
{
int mid=(l+r)/;
a=Search(,i,mid);
b=Search(,i,mid); if(a>b) r=mid-;
else
{
e=mid;
l = mid+;
}
}
ans+=(e-s+);
}
printf("%lld\n",ans);
return ;
}

另一种方法:

#include<bits/stdc++.h>
using namespace std;
int n,a[],b[];
long long ans;
deque<int>x,y;
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]);
for(int i=,j=; i<=n; i++)
{
while(!x.empty()&&a[x.back()]<=a[i]) x.pop_back();
while(!y.empty()&&b[y.back()]>=b[i]) y.pop_back();
x.push_back(i);
y.push_back(i);
while(j<=i&&a[x.front()]-b[y.front()]>)
{
j++;
while(!x.empty()&&x.front()<j) x.pop_front();
while(!y.empty()&&y.front()<j) y.pop_front();
}
if(!x.empty()&&!y.empty()&&a[x.front()]==b[y.front()])
{
ans+=min(x.front(),y.front())-j+;
}
}
printf("%lld",ans);
}

 

Codeforces Round #361 (Div. 2) D的更多相关文章

  1. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

  2. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

  3. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  4. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  5. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  6. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  7. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  8. Codeforces Round #361 (Div. 2) C

    C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...

  9. Codeforces Round #361 (Div. 2) B

    B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...

随机推荐

  1. ACM/ICPC 之 三维计算几何+暴力枚举+判重(HDU5839)

    CCPC网赛第八题,求立体几何数量,题解见注释 //立体几何-求满足要求的四面体个数 //要求1:至少4条边相等 //要求2:四条边相等时,另两条边一定不相邻(即对边) //题解:以当前边为不相邻的其 ...

  2. NodeJS 模块开发及发布详解

    NodeJS 是一门年轻的语言,扩展模块并不太全,经常我们想用某个模块但是却找不到合适的.比如前两天我需要使用hmac和sha1来做签名,就没有找到一个比较好用的模块,这时候就需要我们自己来实现相应的 ...

  3. 移居 GitHub

    博客很久没能更新了,很多代码也从博客园逐渐转移到 GitHub,欢迎新老用户光顾: https://github.com/kedebug 个人博客:http://kedebug.me/

  4. [iOS]创建一像素的线

    float sortaPixel = 1.0/[UIScreen mainScreen].scale; UIView* line = [[UIView alloc]initWithFrame:CGRe ...

  5. C/C++: C++变量和基本类型

    1. 如何选择类型的准则 当明确知晓数值不可能为负的时候,应该选择无符号类型. 使用int执行整数运算的时候,在实际应用中,short常常显得太小而long一般和int有一样的尺寸,如果数值超过了in ...

  6. File System的简单操作

    在进行这些操作之前,需要在js文件中导入fs模块 const fs = require("fs"); const是定义一个常量,比较特殊的是,使用const定义时必须赋值,一旦被赋 ...

  7. 发布报错:Error ITMS-90635 - Invalid Mach-O in bundle - submitting to App store

    发布报错:Error ITMS-90635 - Invalid Mach-O in bundle - submitting to App store 昨晚上传项目到AppStore,报了这个错,纳尼! ...

  8. octave手册

    GNU OCTAVE是一种高级语言,主要用于数值计算.它提供交互式命令行窗口,用于求解线性和非线性问题并计算出数值,并可以进行其它数值实验,还可以用来作为一个批量数据处理语言 运行Ocatve: oc ...

  9. 使用WGET参数介绍大全

    wget 是一个命令行的下载工具.对于我们这些 Linux 用户来说,几乎每天都在使用它.下面为大家介绍几个有用的 wget 小技巧,可以让你更加高效而灵活的使用 wget. $ wget -r -n ...

  10. ***HTML +CSS 总结与归纳

    一.首先W3C标准 结构.表现.动作  与  html.css.javascript相对应,它本意是结构表现分离,而且按照html规范编写结构. 标签方面: -所有标签都要小写.关闭.并且合理嵌套,i ...