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. Home 安转beta版软件

    今天想装测试版的cocoapods,用 brew install cocoapods 后,总是安装稳定版,就是1.1.0,不是最新的beta版,发现用下面这个命令可以装最新beta版 brew ins ...

  2. freemarker 中文乱码

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker ...

  3. iOS 系统数字键盘左下角加确定按钮

    首先在 viewWillAppear 方法中注册监听相应的键盘通知,并且要在 viewWillDisappear 方法中注销通知- (void)viewWillAppear:(BOOL)animate ...

  4. 如何在Eclipse下查看JDK源代码

    在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 "window"-> "Preferences" -> "Java&quo ...

  5. 【笔记】ztree的使用

    引用的js和css: <!-- zTreeJS --><script type="text/javascript" src="jquery/jquery ...

  6. Http请求中请求头Content-Type 为 form-data、x-www-form-urlencoded、raw、binary的区别

    参考文献:http://blog.csdn.net/ye1992/article/details/49998511

  7. SQL语法和运算符(一)

    一个数据库通常包含一个或多个表.每个表由一个名字标识,表包含带有数据的记录(行). 一些最重要的SQL命令(SQL对大小写不敏感): 一.SQL语法 select:从数据库中提取数据 update:更 ...

  8. MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)

    聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { ...

  9. Hadoop单机模式安装-(2)安装Ubuntu虚拟机

    网络上关于如何单机模式安装Hadoop的文章很多,按照其步骤走下来多数都失败,按照其操作弯路走过了不少但终究还是把问题都解决了,所以顺便自己详细记录下完整的安装过程. 此篇主要介绍在虚拟机设置完毕后, ...

  10. 再探JS数组原生方法—没想到你是这样的数组

    最近作死又去做了一遍javascript-puzzlers上的44道变态题,这些题号称"JS语言专业八级"的水准,建议可以去试试,这里我不去解析这44道题了, ...