CF 689D - Friends and Subsequences

689D - Friends and Subsequences

题意:

  • 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb(l,r)
  • 切题的感觉很爽唉
  • 同样而二分查找,找最小和最大下标满足条件
  • cf中%I64d, 一般是%lld

    代码:

#include<bits/stdc++.h>
#define ll long long
const int  maxn=200010;
int sta[maxn][18];
int stb[maxn][18];
int a[maxn];
int b[maxn];
ll res;
int n;
void build(){
    int maxl=floor(log2(n));
    for(int i=1;i<=n;i++){
        sta[i][0]=a[i];
        stb[i][0]=b[i];
    }
    int mul=1;
    for(int j=1;j<=maxl;j++){
        for(int i=1;i<=n&&(i+mul)<=n;i++){
            sta[i][j]=std::max(sta[i][j-1],sta[i+mul][j-1]);
            stb[i][j]=std::min(stb[i][j-1],stb[i+mul][j-1]);
        }
        mul=mul*2;
    }
}
int check(int x,int y){
    int len=floor(log2(y-x+1));
    int maxa=std::max(sta[x][len],sta[y-(1<<len)+1][len]);
    int minb=std::min(stb[x][len],stb[y-(1<<len)+1][len]);
    //printf("db x:%d y:%d maxa: %d minb %d\n",x,y,maxa,minb);
    return maxa-minb;
}
// find min() key=0
//find max() key=0
int b1(int begin,int end){
    int l=begin,r=end,m;
    while(l<r){
        m=l+((r-l)>>1);
        if(check(begin,m)<0) l=m+1;
        else r=m;
    }
    if(check(begin,l)==0) return l;
    return -1;
}
int b2(int begin,int end){
    int l=begin,r=end,m;
    while(l<r){
        m=l+((r-l+1)>>1);
        if(check(begin,m)<=0) l=m;
        else r=m-1;
    }
    if(check(begin,l)==0) return l;
    return -1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    build();

    res=0;
    for(int i=1;i<=n;i++){
        if(b1(i,n)==-1) continue;
        int r1=b1(i,n);
        int r2=b2(i,n);
        res+=(r2-r1+1);
    }
    printf("%I64d\n",res);
}

原文地址:https://www.cnblogs.com/fridayfang/p/9527553.html

时间: 2024-08-02 03:47:40

CF 689D - Friends and Subsequences的相关文章

CodeForces 689D Friends and Subsequences

枚举,二分,$RMQ$. 对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的. 因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置${{p_1}}$,使得$\mathop {\max }\limits_{i = L}^{{p_1}} {a_i} = \mathop {\min }\limits_{i = L}^{{p_1}} {b_i}$:再次二分找到最后一个位置${{p_2}}$,使得$\mathop {\max }\limi

CF 689D A区间最大值等于B区间最小值的区间个数统计

1 /* 2 Source :CF689D 3 题意:给出a,b两个长度为n的数组,问有多少个区间满足max(a[l,r])==min(b[l,r]) len(a)<10^5 4 思路:对于一个固定的l,max(a[l,r])是一个单调不减的序列,min(b[l,r])是一个单调不增的序列, 5 于是可以枚举区间的左端点,然后二分判断右端点的范围 6 快速查询区间的最大最小值可以利用ST表实现. 7 时间 :2018-08-14-12.26 8 */ 9 #include <bits/stdc

CF 843 A. Sorting by Subsequences

A. Sorting by Subsequences You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order,

cf 843 A Sorting by Subsequences [建图]

题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边一个出边的一张图 那么在这张图里的一个环,就代表着一个满足条件的子序列 所以只要把图建出来以后,统计图中的每一个环就可以了 Code: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #includ

【CF】Codeforces Round #361 (Div. 2)

难得有想法写一整套题解 首先想说的是,这场CF,我感觉是div2极为不错的一场(对于中档选手<因为我就出了三题,还掉了一个-- 说笑了,感觉这一场很耐人寻味.就是那种抓破头皮想不出,知道做法后细细品味,有种   哦~~~~~这样啊~~~~好神奇!!! 的感觉 首先..秀一下战绩 不多说了 都在题里 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megaby

【CF1132G】Greedy Subsequences(线段树)

[CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入进来之后\([L+1,i]\)的答案都会增加一,拿线段树维护一下就行了. #include<iostream> #include<cstdio> using namespace std; #define MAX 1000100 inline int read() { int x=0;b

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

CF with friends and user&#39;s influence considered on NYC data(updated Aug,11st)

Here is the code link: https://github.com/FassyGit/LightFM_liu/blob/master/U_F1.py I use NYC data as other experimens. The split of the training data was seperated by the timeline, and I have normalised the interaction matrix by replacing the checkin