Friends and Subsequences

题解:

如果左端点来说, 那么对于a[i]来说是向上的一条折线, b[i]来说是向下的一条折线, 那么如果这2个折线求交点个数的话, 我们可以二分去求第一个 a[i] == b[i] 的地方, 求最后一个a[i] == b[i]的地方。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4. #define LL long long
  5. #define ULL unsigned LL
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define lson l,m,rt<<1
  10. #define rson m+1,r,rt<<1|1
  11. #define lch(x) tr[x].son[0]
  12. #define rch(x) tr[x].son[1]
  13. #define max3(a,b,c) max(a,max(b,c))
  14. #define min3(a,b,c) min(a,min(b,c))
  15. typedef pair<int,int> pll;
  16. const int inf = 0x3f3f3f3f;
  17. const int _inf = 0xc0c0c0c0;
  18. const LL INF = 0x3f3f3f3f3f3f3f3f;
  19. const LL _INF = 0xc0c0c0c0c0c0c0c0;
  20. const LL mod = (int)1e9+;
  21. const int N = 2e5 + ;
  22. int a[N], b[N];
  23. int Log[N];
  24. int Max[N][];
  25. int Min[N][];
  26. void init(int n) {
  27. for(int i = -(Log[]=-); i < N; i++)
  28. Log[i] = Log[i - ] + ((i & (i - )) == );
  29. for(int i = ; i <= n; ++i) Max[i][] = a[i], Min[i][] = b[i];
  30. for(int j = ; j <= Log[n]; j++)
  31. for(int i = ; i+(<<j)- <= n; i++)
  32. Max[i][j] = max(Max[i][j-], Max[i+(<<(j-))][j-]),
  33. Min[i][j] = min(Min[i][j-], Min[i+(<<(j-))][j-]);
  34. }
  35. int QMin(int l, int r) {
  36. int k = Log[r - l + ];
  37. return min(Min[l][k], Min[r-(<<k)+][k]);
  38. }
  39. int QMax(int l, int r){
  40. int k = Log[r - l + ];
  41. return max(Max[l][k], Max[r-(<<k)+][k]);
  42. }
  43. int main(){
  44. int n;
  45. scanf("%d", &n);
  46. for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
  47. for(int i = ; i <= n; ++i) scanf("%d", &b[i]);
  48. init(n);
  49. LL ans = ;
  50. for(int i = ; i <= n; ++i){
  51. int l = i, r = n;
  52. while(l <= r){
  53. int m = l+r >> ;
  54. if(QMin(i, m) > QMax(i, m)) l = m + ;
  55. else r = m - ;
  56. }
  57. if(QMin(i, l) != QMax(i, l)) continue;
  58. int tl = l;
  59. l = i, r = n;
  60. while(l <= r){
  61. int m = l+r >> ;
  62. if(QMin(i, m) >= QMax(i, m)) l = m + ;
  63. else r = m - ;
  64. }
  65. ans += l - tl;
  66. }
  67. cout << ans << endl;
  68. return ;
  69. }

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. nginx 301跳转https后post请求失效问题解决

    app本地请求是http端口,后来升级https强制301跳转,设置如下 server { listen 80; server name www.XXX.com; rewrite ^/(.*)$ ht ...

  2. 制造资源计划(Manufacturing Resource Planning,Mrp II)

        制造资源计划(Manufacturing Resource Planning,Mrp II)       概括: 以物料需求计划(MRP)为核心的企业生产管理计划系统,MRP II 是以工业工 ...

  3. 灰度级分层(一些基本的灰度变换函数)基本原理及Python实现

    1. 基本原理 灰度级分层通常用于突出感兴趣的特定灰度范围内的亮度.灰度级分层有两大基本方法. 将感兴趣的灰度范围内的值显示为一个值(比如0),而其他范围的值为另外一个值(255). 将感兴趣的灰度范 ...

  4. SpringBoot-Admin的使用

    [**前情提要**]Spring Boot Actuator 提供了对单个 Spring Boot 应用的监控,信息包含应用状态.内存.线程.堆栈等,比较全面的监控了 Spring Boot 应用的整 ...

  5. 【Kubernetes 系列五】在 AWS 中使用 Kubernetes:EKS

    目录 1. 概述 2. 版本 3. 预备 3.1. 操作环境 3.2. 角色权限 3.2.1. CloudFormation 完全权限 3.2.2. EKS 读写权限 3.2.3. EC2 相关权限 ...

  6. Java并发之内存模型(JMM)浅析

    背景 学习Java并发编程,JMM是绕不过的槛.在Java规范里面指出了JMM是一个比较开拓性的尝试,是一种试图定义一个一致的.跨平台的内存模型.JMM的最初目的,就是为了能够支多线程程序设计的,每个 ...

  7. Mybatis框架(9)---Mybatis自定义插件生成雪花ID做为表主键项目

    Mybatis自定义插件生成雪花ID做为主键项目 先附上项目项目GitHub地址 spring-boot-mybatis-interceptor 有关Mybatis雪花ID主键插件前面写了两篇博客作为 ...

  8. @Validated和@Valid区别

    注解地方 @Validated:可以用在类型.方法和方法参数上.但是不能用在成员属性(字段)上 @Valid:可以用在方法.构造函数.方法参数和成员属性(字段)上 两者是否能用于成员属性(字段)上直接 ...

  9. Android进阶之绘制-自定义View完全掌握(一)

    Android的UI设计可以说是决定一个app质量的关键因素,因为人们在使用app的时候,最先映入眼帘的就是app的界面了,一个美观.充实的界面能够给用户带来非常好的体验,会在用户心中留下好的印象. ...

  10. 存储型XSS的发现经历和一点绕过思路

    再次骚扰 某SRC提现额度竟然最低是两千,而已经有750的我不甘心呐,这不得把这2000拿出来嘛. 之后我就疯狂的挖这个站,偶然发现了一个之前没挖出来的点,还有个存储型XSS! 刚开始来到这个之前挖过 ...