Friends and Subsequences
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?
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.
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
6
1 2 3 2 1 4
6 7 1 2 3 2
2
3
3 3 3
1 1 1
0
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.
分析:RMQ+二分;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=2e5+;
const int dis[][]={,,-,,,-,,};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,p[maxn],a[][maxn],b[][maxn];
ll ans;
void init()
{
for(int i=;i<n;i++)p[i]=+p[i/];
for(int i=;i<;i++)
for(int j=;j+(<<i)-<n;j++)
a[i][j]=max(a[i-][j],a[i-][j+(<<(i-))]),b[i][j]=min(b[i-][j],b[i-][j+(<<(i-))]);
return;
}
int getma(int l,int r)
{
int x=p[r-l+];
return max(a[x][l],a[x][r-(<<x)+]);
}
int getmi(int l,int r)
{
int x=p[r-l+];
return min(b[x][l],b[x][r-(<<x)+]);
}
int getl(int now)
{
int l=now-,r=n;
while(r-l>)
{
int mid=(l+r)>>;
if(getma(now,mid)<getmi(now,mid))l=mid;
else r=mid;
}
return r;
}
int getr(int now)
{
int l=now-,r=n;
while(r-l>)
{
int mid=(l+r)>>;
if(getma(now,mid)<=getmi(now,mid))l=mid;
else r=mid;
}
return r;
}
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n-)scanf("%d",&a[][i]);
rep(i,,n-)scanf("%d",&b[][i]);
init();
rep(i,,n-)ans+=getr(i)-getl(i);
printf("%lld\n",ans);
return ;
}
Friends and Subsequences的更多相关文章
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
随机推荐
- drawableLayout的使用(转载讲的比较清晰的文章)
创建drawbler的布局文件 初始化drawbler的列表 响应drawable列表点击事件 现在侧滑菜单使用很多,大都是通过SlidingMenu实现.现在也可以通过DrawerLayout 创建 ...
- php file取重复
function FetchRepeatMemberInArray($array) { // 获取去掉重复数据的数组 $unique_arr = array_unique ( $array ); // ...
- 其他应用和技巧-用try和catch来让程序更友好
---------------------------- <script type="text/javascript"> //进行 ...
- Java编译成功,用java 运行class时出现错误解决方法
java -classpath class file's address; filename
- windows 装 centos
windows下压缩一下空间 直接装centos 找到分配给linux的/boot目录,然后修改grub/menu.lst文件,更换一下位置即可
- java和.net的类比
原文地址在http://www.seguetech.com/blog/2013/06/03/dotnet-vs-java-how-to-pick
- springsecurity源码查看网址
http://www.boyunjian.com/javasrc/org.springframework.security/spring-security-web/3.1.3.RELEASE/_/or ...
- PHP中字符串转换为数值 可能会遇到的坑
今天看到一个老外最喜欢的一段代码 <?php $string = 'zero'; $zero = 0; echo ($string == $zero) ? 'Why? Just why?!' : ...
- vb6 控件未注册问题解决
打开项目时弹出如题错误. 另附一个帖子:http://bbs.csdn.net/topics/390580540,这个帖子讨论的不错,可以提供很多思路. 解决办法:http://rewwensoftw ...
- 源码篇:SDWebImage
攀登,一步一个脚印,方能知其乐 源码篇:SDWebImage 源码来源:https://github.com/rs/SDWebImage 版本: 3.7 SDWebImage是一个开源的第三方库,它提 ...