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 ...
随机推荐
- Docker部署ELK 日志归集
ELK ELK是Elasticsearch.Logstash.Kibana的缩写,使用ELK的原因是因为公司使用Spring cloud部署了多个微服务,不同的微服务有不同的日志文件,当生产上出现问题 ...
- Redis 学习笔记(篇七):Redis 持久化
因为 Redis 是内存数据库,它将自己的数据储存在内存里面,所以如果不想办法将储存在内存中的数据库状态保存到磁盘里面,那么一旦服务器进程退出,服务器中的数据也将会丢失,为了解决这个问题,Redis ...
- python多线程详解
目录 python多线程详解 一.线程介绍 什么是线程 为什么要使用多线程 二.线程实现 threading模块 自定义线程 守护线程 主线程等待子线程结束 多线程共享全局变量 互斥锁 递归锁 信号量 ...
- Codis与RedisCluster的原理详解
背景介绍 我们先来看一下为什么要做集群,如果我们要部署一个单节点Redis,很明显会遇到单点故障的问题. 首先能想到解决单点故障的方法,就是做主从,但是当有海量存储需求时,单一的主从结构就会出问题,说 ...
- Flink 源码解析 —— 如何获取 JobGraph?
JobGraph https://t.zsxq.com/naaMf6y 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac 上搭建 F ...
- .netcore持续集成测试篇之Xunit结合netcore内存服务器发送post请求
系列目录 Web项目中,很多与用户数据交互的请求都是Post请求,想必大家都用过HttpClient构造过post请求,这里并不对HttpClient做详细介绍,只介绍一些常用的功能.并结合AutoF ...
- Nunit与Xunit介绍
Nunit安装 首先说下,nunit2.X与3.X版本需要安装不同的vs扩展. nunit2.x安装 安装如上3个,辅助创建nunit测试项目与在vs中运行单元测试用例 . 1.Nunit2 Test ...
- java中的各种锁详细介绍
转自:https://blog.csdn.net/axiaoboge/article/details/84335452 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高 ...
- java字符串详解
一.String 类的定义 public final class String implements java.io.Serializable, Comparable<String>, C ...
- Apache 配置 https
本人当前的Apache版本是: 由于我是yum安装的http,默认的http配置文件我就不多说了, 下面开始记录一下自己的线上配置过程: 1,进入/etc/httpd/conf.d目录,新建证书放 ...