101222024-03-27 17:37:06111Maximum felosztáscpp17Time limit exceeded 40/1001.083s103632 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){
	}
	
	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;
	
	ST(int n):n(n),a(n*8),b(n*8){
	}
	
	void push(int i,int l,int r){
		if(b[i]==-1){
			a[i]=0;
			b[i]=0;
			b[i*2+1]=-1;
			b[i*2+2]=-1;
		}
		else{
			a[i]+=b[i];
			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);
		// for(int i=l;i<=r;i++){
			// a[i]+=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);
		// MI x=0;
		// for(int i=l;i<=r;i++){
			// x+=a[i];
		// }
		// return x;
	}
	
	void clear(){
		b[0]=-1;
	}
};

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;
			for(int k=j;k>0&&v[k]<=w[i];k--){
				x+=dp[i&1^1].sum(k-1,k-1);
			}
			for(int k=j;k<=N&&(k==j||v[k]<w[i]);k++){
				dp[i&1].add(k,k,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
1Accepted3ms2112 KiB
2Accepted12ms4164 KiB
subtask210/10
3Accepted7ms2928 KiB
4Accepted3ms3020 KiB
5Accepted8ms3112 KiB
6Accepted7ms3324 KiB
7Accepted3ms3536 KiB
8Accepted3ms3724 KiB
subtask315/15
9Accepted4ms3944 KiB
10Accepted4ms4212 KiB
11Accepted4ms4016 KiB
12Accepted4ms4276 KiB
13Accepted3ms4524 KiB
14Accepted3ms4724 KiB
15Accepted16ms4752 KiB
16Accepted16ms4756 KiB
17Accepted8ms4868 KiB
18Accepted8ms4612 KiB
subtask415/15
19Accepted10ms5964 KiB
20Accepted14ms6108 KiB
21Accepted10ms6352 KiB
22Accepted52ms5916 KiB
23Accepted12ms6308 KiB
24Accepted37ms6428 KiB
25Accepted9ms6508 KiB
26Accepted8ms6512 KiB
27Accepted307ms6520 KiB
28Accepted266ms6512 KiB
29Accepted16ms6512 KiB
30Accepted195ms6508 KiB
31Accepted45ms6400 KiB
32Accepted171ms6428 KiB
33Accepted153ms6412 KiB
34Accepted202ms6508 KiB
35Accepted104ms6516 KiB
36Accepted96ms6604 KiB
37Accepted26ms6232 KiB
38Accepted45ms6600 KiB
subtask50/60
39Time limit exceeded1.06s51160 KiB
40Time limit exceeded1.06s49084 KiB
41Time limit exceeded1.052s49068 KiB
42Time limit exceeded1.075s45180 KiB
43Time limit exceeded1.062s44332 KiB
44Accepted615ms103632 KiB
45Time limit exceeded1.072s48448 KiB
46Time limit exceeded1.078s47572 KiB
47Accepted963ms100568 KiB
48Time limit exceeded1.065s47436 KiB
49Time limit exceeded1.006s100628 KiB
50Time limit exceeded1.075s48192 KiB
51Time limit exceeded1.07s46532 KiB
52Time limit exceeded1.067s50460 KiB
53Time limit exceeded1.055s51548 KiB
54Time limit exceeded1.054s44468 KiB
55Time limit exceeded1.077s48372 KiB
56Accepted578ms96104 KiB
57Accepted588ms96016 KiB
58Time limit exceeded1.069s53504 KiB
59Time limit exceeded1.065s53600 KiB
60Time limit exceeded1.067s48532 KiB
61Time limit exceeded1.072s48548 KiB
62Time limit exceeded1.067s48256 KiB
63Time limit exceeded1.052s46952 KiB
64Time limit exceeded1.075s49092 KiB
65Time limit exceeded1.075s46816 KiB
66Time limit exceeded1.075s49796 KiB
67Time limit exceeded1.083s52920 KiB
68Time limit exceeded1.059s47680 KiB
69Time limit exceeded1.064s50620 KiB
70Time limit exceeded1.078s45296 KiB
71Time limit exceeded1.05s45272 KiB