CodeForces 689 D Friends and Subsequences
题解:
如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求第一个 a[i] == b[i] 的地方, 求最后一个a[i] == b[i]的地方。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int a[N], b[N];
int Log[N];
int Max[N][];
int Min[N][];
void init(int n) {
for(int i = -(Log[]=-); i < N; i++)
Log[i] = Log[i - ] + ((i & (i - )) == );
for(int i = ; i <= n; ++i) Max[i][] = a[i], Min[i][] = b[i];
for(int j = ; j <= Log[n]; j++)
for(int i = ; i+(<<j)- <= n; i++)
Max[i][j] = max(Max[i][j-], Max[i+(<<(j-))][j-]),
Min[i][j] = min(Min[i][j-], Min[i+(<<(j-))][j-]);
}
int QMin(int l, int r) {
int k = Log[r - l + ];
return min(Min[l][k], Min[r-(<<k)+][k]);
}
int QMax(int l, int r){
int k = Log[r - l + ];
return max(Max[l][k], Max[r-(<<k)+][k]);
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i) scanf("%d", &b[i]);
init(n);
LL ans = ;
for(int i = ; i <= n; ++i){
int l = i, r = n;
while(l <= r){
int m = l+r >> ;
if(QMin(i, m) > QMax(i, m)) l = m + ;
else r = m - ;
}
if(QMin(i, l) != QMax(i, l)) continue;
int tl = l;
l = i, r = n;
while(l <= r){
int m = l+r >> ;
if(QMin(i, m) >= QMax(i, m)) l = m + ;
else r = m - ;
}
ans += l - tl;
}
cout << ans << endl;
return ;
}
CodeForces 689 D Friends and Subsequences的更多相关文章
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- 【codeforces 314C】Sereja and Subsequences
[题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...
- codeforces 361 D - Friends and Subsequences
原题: Description Mike and !Mike are old childhood rivals, they are opposite in everything they do, ex ...
- 【22.48%】【codeforces 689D】Friends and Subsequences
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
随机推荐
- 【MySQL】日常小技巧汇总,更新中……
创建表时修改自增主键,添加 AUTO_INCREMENT=<Number> ,例如: CREATE TABLE `table_name` ( `id` int(11) unsigned N ...
- ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...
- 向.Net/Unity 程序员推荐一个十分因吹斯听的网站:sharplab.io
0x00 前言 & 过程 & 结论 今天发现了一个十分有趣的网站: https://sharplab.io/ 网站的页面并不复杂,功能大体上能够在题图中得到概括.发现它的过程也很偶然, ...
- Python基础总结之异常、调试代码第十二天开始(新手可相互督促)
年薪20万的梦想,加油! 我们在写代码的时候,控制台经常会报错,因为某种错误,导致我们的程序停止,且不再运行下面的代码. 我们看一个错误的代码示例: def add_1(): #没有参数 print( ...
- 【POJ - 1862】Stripies (贪心)
Stripies 直接上中文了 Descriptions 我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板 ...
- c++随笔之编译器编译原理
/* C++编译器原理:1)首先明白声明与定义是两个不同的概念 extern int i;是声明,int i;是定义 函数就更简单了2)编译分为: 预编译:将宏替换,include等代码拷贝过来 编译 ...
- Vue系列:Slot 插槽的使用范例
插槽对于自定义的组件开发来说,是十分强大的功能.这篇主要做个简单梳理 插槽可以分3种: 1.简单插槽 2.具名插槽 3.作用域插槽
- curl工具使用实例
curl是一个命令行工具,其基于libcurl库,用于发送网络请求,获取并展示响应数据,下面来看curl的具体用法. 1.下载网页源码 curl命令直接接URL,用于下载指定URL的网页源码,并将其显 ...
- DT-06 For AT
乐鑫官方AT指令固件- 最新1.5.4版本 此固件仅支持AT指令对模块进行操作. 1.DT-06固件的烧录 1.1打开ESP模块下载工具ESPFlashDownloadTool,选择需要下载的固件,填 ...
- 页面性能监控之performance
页面性能监测之performance author: @TiffanysBear 最近,需要对业务上的一些性能做一些优化,比如降低首屏时间.减少核心按钮可操作时间等的一些操作:在这之前,需要建立的就是 ...