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. 【iOS】Interface Builder 预览

    Interface Builder 为最顶层视图提供了 Simulated Metrics,预览用户界面的各种外观设置效果,例如顶部有导航栏或底部有标签栏的效果,如图所示:

  2. 【Android Studio】Frameworks detected: Android framework is detected in the project Configure

    刚开始在 Mac 上用 Android Studio, 打开第一个项目就遇到了问题,描述如下: 上午9:: Frameworks detected: Android framework is dete ...

  3. .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

    前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...

  4. linux杂货铺

    vmware虚拟机克隆后网卡不能使用 解决方法如下 cat /etc/udev/rules.d/70-persistent-net.rules1.将eth0这行注释掉或者删除,这里记载的还是克隆系统时 ...

  5. WPF:事件委托对于不同界面间通信的应用

    界面1内设定点击事件,生成Path用事件传出public partial class TemplateWindow : Window     {         internal delegate v ...

  6. AUTOSAR学习之RTE - 基本概念

    1.什么是RTE? The Run-Time Environment (RTE) is at the heart of the AUTOSAR ECU architecture. The RTE is ...

  7. 使用 OpenSSL为WindowsServer远程桌面(RDP)创建自签名证书 (Self-signed SSL certificate)

    前言 笔者查阅很多资料,才写成此文章,如有错误,请读者们及时提出. 一般大家使用远程桌面(Remote Desktop)连接Windows Server时,总会有一个警告提示,如图1 图1 出现此警告 ...

  8. JavaScript的event对象

    JavaScript的event对象中 event.target指代的是:触发事件的元素 event.currentTarget指代的是:事件绑定的元素 <!DOCTYPE html> & ...

  9. Selenium+java - 日期控件的处理

    前言 一般的日期控件都是input标签下弹出来的,设置日期使用selenium中的sendKeys 方法就可以解决. 但是我们也会碰到下面的时间日期控件(这个时候这个文本框是不允许我们输入时间的)如图 ...

  10. 修改 jupyter notebook的默认文件夹位置

    安装好Anaconda 3以后,就可以使用Jupyter notebook了,但是我们打开Jupyter notebook后,发现界面是一个默认的目录,这个目录在哪里?如果想把自己写的程序文件保存在自 ...