2020牛客多校 2-G Greater and Greater

描述

Greater and Greater
给出a、b两个串,长分别为n<=1.5e5,m<=4e4,求a中有多少长为m的连续子串t,使得ti>=bi

题解

官方题解:

2020牛客多校 2-G Greater and Greater_第1张图片
S的情况最多有m个,将b串和下标一起排序,根据下标,就能确定m种S,之后二分确定ai对应的是哪一个S

代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

const int Inf=0x3f3f3f3f;
const double eps=1e-7;
const int maxn=2e5+50;
const int maxm=4e4+50;

bitset<maxm> cur,S[maxm];
vector< pair<int,int> > vc;
int a[maxn], b[maxm];
int n,m;

int find(int x){
    if(x < vc[0].first) return 0;
    int l=0,r=m-1, ret=0;
    while (l<=r){
        int mid=(l+r)>>1;
        if(vc[mid].first <= x){
            ret = mid;
            l = mid+1;
        }
        else 
            r = mid-1;
    }
    return ret+1;
}   

int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    for(int i=1; i<=m; i++){
        scanf("%d",&b[i]);
        vc.push_back({b[i], i});
    }
    sort(vc.begin(), vc.end());
    for(int i=0; i<vc.size(); i++){
        S[i+1] = S[i];
        S[i+1].set( vc[i].second );
    }

    for(int i=n; i>n-m+1; i--){
        cur = cur>>1;
        cur.set(m);
        cur = cur&S[find(a[i])];
        // printf("(%d\n",find(a[i]));
    }
    int ans=0;
    for(int i=n-m+1; i>=1; i--){
        cur = cur>>1;
        cur.set(m);
        cur = cur&S[find(a[i])];
        ans += cur[1];
            // cout<<" [ "<
    }
    cout<<ans<<endl;
}

你可能感兴趣的:(思维好题)