101212024-03-27 17:33:48111Maximum felosztáscpp17Time limit exceeded 40/1001.08s53264 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){
		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(){
		for(int i=0;i<n*8;i++){
			a[i]=0;
			b[i]=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;
			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
1Accepted3ms2092 KiB
2Accepted23ms4028 KiB
subtask210/10
3Accepted7ms2876 KiB
4Accepted3ms3084 KiB
5Accepted7ms2940 KiB
6Accepted7ms3340 KiB
7Accepted3ms3500 KiB
8Accepted3ms3568 KiB
subtask315/15
9Accepted4ms3804 KiB
10Accepted4ms3948 KiB
11Accepted4ms3880 KiB
12Accepted4ms3888 KiB
13Accepted3ms4040 KiB
14Accepted3ms3980 KiB
15Accepted17ms4016 KiB
16Accepted17ms4024 KiB
17Accepted8ms4128 KiB
18Accepted8ms3976 KiB
subtask415/15
19Accepted34ms5592 KiB
20Accepted26ms5552 KiB
21Accepted12ms5656 KiB
22Accepted48ms5416 KiB
23Accepted12ms5808 KiB
24Accepted35ms5828 KiB
25Accepted20ms5832 KiB
26Accepted19ms5736 KiB
27Accepted324ms5812 KiB
28Accepted280ms5864 KiB
29Accepted16ms5776 KiB
30Accepted174ms5924 KiB
31Accepted48ms5980 KiB
32Accepted156ms5860 KiB
33Accepted167ms5988 KiB
34Accepted199ms5992 KiB
35Accepted114ms6224 KiB
36Accepted100ms6236 KiB
37Accepted28ms5760 KiB
38Accepted41ms6144 KiB
subtask50/60
39Time limit exceeded1.064s50608 KiB
40Time limit exceeded1.067s48540 KiB
41Time limit exceeded1.024s48452 KiB
42Time limit exceeded1.059s44440 KiB
43Time limit exceeded1.075s43600 KiB
44Time limit exceeded1.044s52768 KiB
45Time limit exceeded1.08s47908 KiB
46Time limit exceeded1.064s46952 KiB
47Time limit exceeded1.067s51380 KiB
48Time limit exceeded1.072s46800 KiB
49Time limit exceeded1.067s51288 KiB
50Time limit exceeded1.077s47824 KiB
51Time limit exceeded1.064s46048 KiB
52Time limit exceeded1.064s49972 KiB
53Time limit exceeded1.055s51068 KiB
54Time limit exceeded1.067s43976 KiB
55Time limit exceeded1.07s48064 KiB
56Time limit exceeded1.059s49448 KiB
57Time limit exceeded1.08s49460 KiB
58Time limit exceeded1.072s53264 KiB
59Time limit exceeded1.064s53180 KiB
60Time limit exceeded1.072s48204 KiB
61Time limit exceeded1.047s47880 KiB
62Time limit exceeded1.075s47772 KiB
63Time limit exceeded1.059s46340 KiB
64Time limit exceeded1.044s48440 KiB
65Time limit exceeded1.07s46340 KiB
66Time limit exceeded1.047s49308 KiB
67Time limit exceeded1.075s52296 KiB
68Time limit exceeded1.07s46952 KiB
69Time limit exceeded1.072s49948 KiB
70Time limit exceeded1.07s44536 KiB
71Time limit exceeded1.044s44708 KiB