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 ...
随机推荐
- 清除浮动clearfix
css用clearfix清除浮动 更多2013/11/4 来源:css学习浏览量:11901 学习标签: css clearfix 本文导读:写css 时总为浮动而烦恼,如果用了浮动,浮动的父层不会跟 ...
- ThinkPad 禁用 触摸板
执行 xinput wowk@wowk:~$ xinput ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTES ...
- cp 覆盖 \cp a test\a
使用cp命令覆盖文件总是提示要输入yes或no,一个两个就算了,大量的文件复制就不行了,即使加上-f参数也无法强行覆盖.苦思冥想不得解,终于在查阅了众多资料后让我找到了解决方法,这里写出来,让有同样困 ...
- english 释词
english 释词 [amount of & number of]the amount of /the number of指……的数量an amount of/a number of 指“大 ...
- Encoding filter 编码过滤器
1.首先编写过滤器,实际上就是继承了filter接口的一个类,实现其中的init doChain 和destroy方法 package com.util; import java.io.IOExc ...
- spring XML格式
使用spring遇见一个很坑的问题,在XML中 这样配置: <method name="newSoapParam2" parameters="java.lang.S ...
- pom 的scope标签分析
一.compile:编译范围compile是默认的范围:如果没有提供一个范围,编译范围依赖在所有的classpath 中可用,同时它们也会被打包.而且这些dependency会传递到依赖的项目中. 二 ...
- C/C++ - 结构体实际申请的空间
C/C++ - 结构体实际申请的空间 如下的结构体,sizeof()大小,实际申请的空间以及理论上申请最佳空间 struct Spot { int x; int y; bool visible; in ...
- IO流---字符流(FileWriter, FileReader ,BufferedWriter,BufferedReader)
IO Input Output IO流用来处理设备之间的数据传输. java对数据的操作是通过流来实现的. 流按流向分:输入流,输出流 是相对内存而言的.把硬盘的数据读取到内存中就是输入 ...
- CRT堆
实验环境:win764位旗舰版.VS2010旗舰版 每个进程都有一个默认堆,在进程初始化的时候会创建这个默认堆,可以通过GetProcessHeap()获取默认堆的句柄.使用CRT时,也会有一个CRT ...