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的更多相关文章

  1. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  2. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  3. Codeforces Testing Round #12 C. Subsequences 树状数组

    C. Subsequences     For the given sequence with n different elements find the number of increasing s ...

  4. codeforces 689D D. Friends and Subsequences(RMQ+二分)

    题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...

  5. 【codeforces 314C】Sereja and Subsequences

    [题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...

  6. codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列

    给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...

  7. codeforces 361 D - Friends and Subsequences

    原题: Description Mike and !Mike are old childhood rivals, they are opposite in everything they do, ex ...

  8. 【22.48%】【codeforces 689D】Friends and Subsequences

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  9. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

随机推荐

  1. 【SVN】SVN Working copy is too old

    前天在使用 SVN 客户端 CornerStone 的时候遇到了这个问题,代码不能提交了…… 遇到这个问题的时候怎么办? 解决办法: 找到报错对应的文件夹,里面有个 .svn 的文件夹,去掉再 com ...

  2. QTableView表格控件区域选择-自绘选择区域

    目录 一.开心一刻 二.概述 三.效果展示 四.实现思路 1.绘制区域 2.绘制边框 3.绘制 五.相关文章 原文链接:QTableView表格控件区域选择-自绘选择区域 一.开心一刻 陪完客户回到家 ...

  3. 在ABP中灵活使用AutoMapper

    demo地址:ABP.WindowsService 该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. AutoMapper简介 Auto ...

  4. .NET读写DBF

    C# 读写DBF分为两种模式,一种为OLEDB驱动,需要安装一个文件“VFPOLEDBSetup.msi”: 一种为Odbc模式,这种几乎上不需要安装Odbc驱动 我这边用的是第一种. /// < ...

  5. 机房ping监控 smokeping+prometheus+grafana(续) 自动获取各省省会可用IP

    一.前言 1.之前的文章中介绍了如何使用smokeping监控全国各省的网络情况:https://www.cnblogs.com/MrVolleyball/p/10062231.html 2.由于之前 ...

  6. 续集:白菜的内涵,更新nand分区为ubifs,替换overlay

    在上一篇真千兆路由的极限之OPENWRT MAKE, 某品牌白菜价QCA9558/QCA9880/QCA8337N纯种组合OS搭建时记中附带了128M nand的空间图示,在ar71xx profil ...

  7. 第三章 Linux基本命令操作

    第三章  Linux基本命令操作 ¨  本节所讲内容: ¨  3.1  Linux终端介绍 Shell提示符 Bash Shell基本语法 ¨  3.2  基本命令的使用:ls.pwd.cd.hist ...

  8. 为何Spring MVC可获取到方法参数名,而MyBatis却不行?【享学Spring MVC】

    每篇一句 胡适:多谈些问题,少聊些主义 前言 Spring MVC和MyBatis作为当下最为流行的两个框架,大家平时开发中都在用.如果你往深了一步去思考,你应该会有这样的疑问: 在使用Spring ...

  9. 【KakaJSON手册】06_Model转JSON

    前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON 生成JSON和JSONString struct Car: Convertible { ...

  10. ubuntu 开机进入grub rescue> 解决办法(nvme固态硬盘)

    起因: 我是在windows下格式化了ubuntu的盘,然后重新安装ubuntu就出现了这种问题.卸载ubuntu的正确姿势,要去查一下,千万不要直接格式化. 解决方法: 1.  先使用ls命令,找到 ...