思路分析
开两个$biset$,$ans$用来维护那一位可以作为子段的开头,tmp维护$A$中哪些可以对应当前$B_i$
由$tmp$可以推出目前哪些位置可以作为子段的开头($tmp>>B_i->pos$)可以将数组从大到小排序,这样每次$tmp$就可以直接继承之前的了
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int N = 150010;
bitset<N>ans,tmp;
pair<int,int>a[N],b[N];
int cmp(pair<int,int>x,pair<int,int>y){
return x.fi>y.fi;
}
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i].fi;
a[i].se=i;
}
for(int i=1;i<=m;i++){
cin>>b[i].fi;
b[i].se=i;
}
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+n,cmp);
ans.set();
tmp.reset();
for(int i=1,j=1;i<=m;i++){
while(j<=n && a[j].fi>=b[i].fi){
tmp.set(a[j].se);
j++;
}
ans &= tmp>>b[i].se;
}
cout<<ans.count()<<endl;
return 0;
}