【22.48%】【codeforces 689D】Friends and Subsequences
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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 exactly n(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.
Examples
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
Note
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.
【题解】
用ST算法搞(也就是RMQ算法);
这可以搞定任意两个点之间的最大值和最小值。
接着顺序枚举起点i
对于每个起点i,二分枚举它的终点t;
①如果[i..t]这段区间内a的最大值大于b的最小值,则右端点再也不能往右了。因为再往右只会让这个差距越来越大,不能让他们相等。
②如果[i..t]这段区间内a的最大值等于b的最小值,则这是一个可行的右端点。
③如果[i..t]这段区间内a的最大值小于b的最小值,则右端点可以再往右,以逼近max==min(当然也可能不存在);
总之,枚举起点i,然后找到最靠左的满足要求的右端点t1,和最靠右的满足要求的右端点t2,答案对数增加t2-t1+1
这个t1和t2可以用两个二分写出来(分开写)
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 18;
const int MAXN = 209999;
const int INF = 2e9;
int dpmax[210000][MAX + 3],dpmin[210000][MAX + 3];
int pre2[MAX + 3];
int a[MAXN], b[MAXN];
int need[MAXN];
int n;
void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
int sign =1;
if (t == '-') sign = -sign;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n);
for (int i = 1; i <= n; i++)
input(a[i]), dpmax[i][0] = a[i];
for (int i = 1; i <= n; i++)
input(b[i]), dpmin[i][0] = b[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 18; j++)
dpmin[i][j] = INF,dpmax[i][j]=-INF;
pre2[0] = 1;
for (int i = 1; i <= 18; i++)
pre2[i] = pre2[i - 1] << 1;
need[1] = 0; need[2] = 1;
int temp = 2;
for (int i = 3; i <= n; i++)//need[i]表示长度为i是2的多少次方,可以理解为[log2i]
if (pre2[temp] == i)
need[i] = need[i - 1] + 1, temp++;
else
need[i] = need[i - 1];
for (int l = 1; pre2[l] <= n; l++)//利用st算法搞出区间最大和最小值
for (int i = 1;i <= n;i++)
if (i + pre2[l] - 1 <= n)
dpmax[i][l] = max(dpmax[i][l - 1], dpmax[i + pre2[l - 1]][l - 1]);
for (int l = 1; pre2[l] <= n; l++)
for (int i = 1; i <= n; i++)
if (i + pre2[l] - 1 <= n)
dpmin[i][l] = min(dpmin[i][l - 1], dpmin[i + pre2[l - 1]][l - 1]);
long long ans = 0;
for (int i = 1; i <= n; i++){
int l = i, r = n;
//找最左边的
int numl = -1;
while (l <= r){
int m = (l + r) >> 1;
int len = need[m-i+1];
int themax = max(dpmax[i][len], dpmax[m - pre2[len] + 1][len]);
int themin = min(dpmin[i][len], dpmin[m - pre2[len] + 1][len]);
if (themax > themin)
r = m-1;
else
if (themax == themin){
numl = m;
r = m - 1;
}
else
if (themax < themin)
l = m + 1;
}
//找最右边的
int numr = -1;
l = i, r = n;
while (l <= r) {
int m = (l + r) >> 1;
int len = need[m - i + 1];
int themax = max(dpmax[i][len], dpmax[m - pre2[len] + 1][len]);
int themin = min(dpmin[i][len], dpmin[m - pre2[len] + 1][len]);
if (themax > themin)
r = m - 1;
else
if (themax == themin) {
numr = m;
l = m + 1;
}
else
if (themax < themin)
l = m + 1;
}
if (numl != -1)
ans += (numr - numl + 1);
}
printf("%I64d\n", ans);
return 0;
}
【22.48%】【codeforces 689D】Friends and Subsequences的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766E】Mahmoud and a xor trip
[题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...
- 【codeforces 733F】Drivers Dissatisfaction
[题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- 【codeforces 22C】 System Administrator
[题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...
随机推荐
- 并发,three
引言 很久没有跟大家再聊聊并发了,今天LZ闲来无事,跟大家再聊聊并发.由于时间过去的有点久,因此LZ就不按照常理出牌了,只是把自己的理解记录在此,如果各位猿友觉得有所收获,就点个推荐或者留言激励下LZ ...
- Day1:第一个python小程序
Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...
- (7)Launcher3客制化之,改动单屏幕后,Fix在Hotseat拖动应用删除报错
改动单屏幕后,在workspace里面拖动图标.到删除button上松开的时候,报错问题. 而且无法再次显示拖动的图标. 拖动松开手时候触发 public void onDropCompleted(f ...
- 像Bootstrap一样比较热门的前端框架有哪些
像Bootstrap一样比较热门的前端框架有哪些 一.总结 一句话总结:框架大同小异,可以多去各自官网看看效果(比较一下各自的不同点(也就是提供的不同的功能)),然后根据需求选择用哪个.我觉得boot ...
- C语言深度剖析-----内存管理的艺术
动态内存分配 为什么使用动态内存分配 例:记录卖出的商品 卖出商品最多只能记录1000个 两种改进的方法 都需要动态内存分配 第二种方法需要重置内存 calloc和realloc realloc重置内 ...
- mybatis-generator使用
开发工具:eclipse,依赖环境 :maven 首先在eclipse marketplace中安装mybatis-generator,如图: 安装后需要重启. 新建一个maven项目MybatisG ...
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...
- SaltStack快速部署及测试
测试环境:CentOS6.6 X86_64 # cat /etc/hosts 192.168.199.61 Ansible 192.168.199.60 Nginx1 192.168.199.62 N ...
- 6、修改应用程序数码相框以支持自动关闭LCD
1. 修改数码相框以自动关闭LCD关闭LCD : 在读取触摸屏的函数中判断:如果15S内无数据,执行: echo auto > /sys/devices/platform/mylcd/power ...
- ITFriend月刊-第1期-2014年6月.pdf
ITFriend上线一个月了,积累了不少优质内容,本周进行了整理,制作了PDF格式的电子书. 欢迎大家下载阅读. 下载地址: CSDN下载:http://download.csdn.net/detai ...