101242024-03-27 17:48:49111Maximum felosztáscpp17Time limit exceeded 40/1001.103s137052 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define MOD 1000000007

struct MI{
	int x;
	
	MI():x(0){
	}
	
	MI(int x):x(x){
	}
	
	MI operator*(MI o){
		return x*o.x%MOD;
	}
	
	void operator=(MI o){
		x=o.x;
	}
	
	void operator+=(MI o){
		x+=o.x;
		if(x>=MOD){
			x-=MOD;
		}
	}
	
	operator int(){
		return x;
	}
};

struct ST{
	int n;
	vector<MI>a,b,c;
	
	ST(int n):n(n),a(n*8),b(n*8),c(n*8){
	}
	
	void push(int i,int l,int r){
		if(c[i]==1){
			a[i]=0;
			b[i]=0;
			c[i]=0;
			c[i*2+1]=1;
			c[i*2+2]=1;
		}
		else{
			a[i]+=b[i]*MI(r-l+1);
			if(c[i*2+1]){
				a[i*2+1]=0;
				b[i*2+1]=0;
				c[i*2+1]=0;
			}
			if(c[i*2+2]){
				a[i*2+2]=0;
				b[i*2+2]=0;
				c[i*2+2]=0;
			}
			b[i*2+1]+=b[i];
			b[i*2+2]+=b[i];
			b[i]=0;
		}
	}
	
	void add(int i,int l,int r,int ll,int rr,int x){
		if(l>rr||r<ll){
			return;
		}
		push(i,l,r);
		if(l>=ll&&r<=rr){
			b[i]+=x;
			push(i,l,r);
			return;
		}
		push(i,l,r);
		add(i*2+1,l,(l+r)/2,ll,rr,x);
		add(i*2+2,(l+r)/2+1,r,ll,rr,x);
		a[i]=a[i*2+1]+a[i*2+2];
	}
	
	void add(int l,int r,int x){
		add(0,0,n-1,l,r,x);
	}
	
	MI sum(int i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return 0;
		}
		push(i,l,r);
		if(l>=ll&&r<=rr){
			return a[i];
		}
		MI ans=0;
		ans+=sum(i*2+1,l,(l+r)/2,ll,rr);
		ans+=sum(i*2+2,(l+r)/2+1,r,ll,rr);
		return ans;
	}
	
	MI sum(int l,int r){
		return sum(0,0,n-1,l,r);
	}
	
	void clear(){
		fill(a.begin(),a.end(),0);
		fill(b.begin(),b.end(),0);
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	vector<int>v(N+1),w(M+1);
	map<int,vector<int>>m;
	for(int i=1;i<=N;i++){
		cin>>v[i];
		m[v[i]].push_back(i);
	}
	for(int i=1;i<=M;i++){
		cin>>w[i];
	}
	vector<ST>dp(2,ST(N+1));
	dp[0].add(0,0,1);
	for(int i=1;i<=M;i++){
		dp[i&1].clear();
		for(int j:m[w[i]]){
			MI x=0;
			int k;
			for(k=j;k>0&&v[k]<=w[i];k--){
			}
			x+=dp[i&1^1].sum(k,j-1);
			for(k=j;k<=N&&(k==j||v[k]<w[i]);k++){
			}
			dp[i&1].add(j,k-1,x);
		}
		// for(int j=1;j<=N;j++){
			// cout<<setw(4)<<dp[i&1].sum(j,j)<<' ';
		// }
		// cout<<'\n';
	}
	cout<<dp[M&1].sum(N,N)<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2128 KiB
2Accepted9ms4960 KiB
subtask210/10
3Accepted3ms3144 KiB
4Accepted3ms3372 KiB
5Accepted3ms3480 KiB
6Accepted3ms3664 KiB
7Accepted3ms4008 KiB
8Accepted3ms3932 KiB
subtask315/15
9Accepted4ms3920 KiB
10Accepted4ms4160 KiB
11Accepted4ms4408 KiB
12Accepted3ms4356 KiB
13Accepted3ms4324 KiB
14Accepted3ms4316 KiB
15Accepted4ms4584 KiB
16Accepted4ms4736 KiB
17Accepted4ms4684 KiB
18Accepted4ms5092 KiB
subtask415/15
19Accepted17ms7024 KiB
20Accepted10ms6980 KiB
21Accepted6ms7048 KiB
22Accepted6ms6784 KiB
23Accepted4ms7320 KiB
24Accepted4ms7284 KiB
25Accepted10ms7324 KiB
26Accepted9ms7324 KiB
27Accepted25ms7416 KiB
28Accepted21ms7412 KiB
29Accepted4ms7196 KiB
30Accepted6ms7072 KiB
31Accepted8ms7040 KiB
32Accepted8ms7044 KiB
33Accepted14ms7056 KiB
34Accepted14ms7472 KiB
35Accepted10ms7556 KiB
36Accepted9ms7496 KiB
37Accepted6ms7380 KiB
38Accepted6ms7484 KiB
subtask50/60
39Time limit exceeded1.062s69984 KiB
40Time limit exceeded1.074s67604 KiB
41Time limit exceeded1.103s67792 KiB
42Time limit exceeded1.044s63696 KiB
43Time limit exceeded1.07s62952 KiB
44Time limit exceeded1.069s72252 KiB
45Time limit exceeded1.064s67500 KiB
46Time limit exceeded1.049s66528 KiB
47Time limit exceeded1.06s70988 KiB
48Time limit exceeded1.072s66540 KiB
49Time limit exceeded1.064s71096 KiB
50Time limit exceeded1.077s67352 KiB
51Time limit exceeded1.06s65612 KiB
52Accepted584ms135388 KiB
53Accepted301ms137052 KiB
54Accepted568ms123148 KiB
55Accepted382ms131072 KiB
56Time limit exceeded1.07s68644 KiB
57Time limit exceeded1.067s68576 KiB
58Time limit exceeded1.06s72628 KiB
59Time limit exceeded1.014s72560 KiB
60Accepted356ms131088 KiB
61Time limit exceeded1.062s67364 KiB
62Time limit exceeded1.054s67232 KiB
63Time limit exceeded1.069s65824 KiB
64Time limit exceeded1.042s67944 KiB
65Time limit exceeded1.07s65744 KiB
66Time limit exceeded1.07s68672 KiB
67Time limit exceeded1.07s71988 KiB
68Time limit exceeded1.074s66704 KiB
69Time limit exceeded1.077s69948 KiB
70Time limit exceeded1.046s64368 KiB
71Time limit exceeded1.042s64224 KiB