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. C#操作XML总结

    1.using System.Xml; using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xm ...

  2. Session的属性

    Session的属性  Session在网络应用中被称为会话.具体到web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间,因 ...

  3. Linux 系统中堆栈的使用方法

    本节内容概要描述了Linux内核从开机引导到系统正常运行过程中对堆栈的使用方式.这部分内容的说明与内核代码关系比较密切,可以先跳过.在开始阅读相应代码时再回来仔细研究. Linux 0.12系统中共使 ...

  4. MySQLFabric连接的编码问题

    今天解决的一个小问题.最终的解决方案很简单,主要是讲一下解决问题的思路. 测试人员在服务器上测试,页面提交的中文内容存入数据库中以后,是乱码. 开发人员在本机上测试,没有问题. 服务器上使用的是Mys ...

  5. UILabel 设置行间距

    UILabel *label1 = [[UILabel alloc]initWithFrame:frame]; label1.numberOfLines = 0; [self.view addSubv ...

  6. js立即调用的函数表达式

    1.多种实现 // 下面2个括弧()都会立即执行 (function () { /* code */ } ()); // 推荐使用这个 (function () { /* code */ })(); ...

  7. 【Java EE 学习 75 上】【数据采集系统第七天】【二进制运算实现权限管理】【权限分析和设计】

    一.权限计算相关分析 1.如何存储权限 首先说一下权限保存的问题,一个系统中最多有多少权限呢?一个大的系统中可能有成百上千个权限需要管理.怎么保存这么多的权限?首先,我们使用一个数字中的一位保存一种权 ...

  8. 一个 Q-learning 算法的简明教程

    本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文翻译,第二部分为英文原文.翻译时为方便 ...

  9. ORA-12638:身份证明检索失败

    本地Sqlplus 连一远程数据库,出现 ORA-12638: 身份证明检索失败,pl/sql developer 也是同样的问题,tnsping 是没有问题的. 找到本地的sqlnet.ora文件, ...

  10. WiX Toolset 教程索引页

    注意:虽然WiX Toolset功能强大,但其学习曲线相对较高.请慎重选择: 若没有足够时间.没心思搞的请绕行至inno setup.installshield.nisi.setupfactory.. ...