Codeforces Round #361 (Div. 2) D
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
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的序列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的更多相关文章
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
- 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 ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- 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 ...
- 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 ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- 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 ...
- Codeforces Round #361 (Div. 2) C
C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...
- Codeforces Round #361 (Div. 2) B
B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...
随机推荐
- eclipse逐步调试
Eclipse 的单步调试 1.设置断点在程序里面放置一个断点,也就是双击需要放置断点的程序左边的栏目上.2.调试(1)点击"打开透视图"按钮,选择调试透视图,则打开调试透视图界面 ...
- Curator Zookeeper分布式锁
Curator Zookeeper分布式锁 pom.xml中添加如下配置 <!-- https://mvnrepository.com/artifact/org.apache.curator/c ...
- sql server 2008 R2配置管理
安装vs2013后,sql server 2008R2配置管理提示“远程过程调用失败” 这是因为vs2013自带的Microsoft SQL Server 2012Local DB与之冲突. 通过升级 ...
- microsoft office professional plus2013激活
激活工具一般使用KMS8,KMS8不支持零售版的激活, 而office professional plus2013零售版,需要先转化为VOL版 需要以下两步: 1.将word转化为vol版 链接: h ...
- python笔记7:日期和时间
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示. 时间 ...
- retrofit使用随记
1.请求接口 public interface RetrofitApi { /*注册*/ /*登录*/ @FormUrlEncoded //post请求带这个 @POST("url" ...
- 《learning hard C#学习笔记》读书笔记(20)异步编程
20.1 什么是异步编程异步编程就是把耗时的操作放进一个单独的线程中进行处理. 20.2 同步方式存在的问题 namespace 异步编程 { public partial class For ...
- Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)
#写libsvm格式 数据 write libsvm #!/usr/bin/env python #coding=gbk # ================================= ...
- 【JavaScript】ES6 新语法
function* 声明 function* 声明(function关键字后跟一个星号)定义一个generator(生成器)函数,返回一个Generator对象. 生成器是一种可以从中退出并在之后重新 ...
- ORA-27492 无法运行作业,调度程序不可用
ORA-27492:无法运行作业;调度程序不可用 ORA-06512: at "SYS.DBMS_ISCHED", line 185 ORA-06512: AT SYS.DBMS_ ...