101262024-03-27 18:05:18111Maximum felosztáscpp17Time limit exceeded 40/1001.077s141668 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,k;
	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*2+1]=0;
			a[i*2+2]=0;
			b[i*2+1]=0;
			b[i*2+2]=0;
			c[i*2+1]=1;
			c[i*2+2]=1;
			c[i]=0;
		}
		a[i*2+1]+=b[i]*MI((l+r)/2-l+1);
		a[i*2+2]+=b[i]*MI(r-(l+r)/2);
		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,MI x){
		if(l>rr||r<ll){
			return;
		}
		if(l>=ll&&r<=rr){
			a[i]+=x*MI(r-l+1);
			b[i]+=x;
			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,MI 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;
		}
		if(l>=ll&&r<=rr){
			return a[i];
		}
		push(i,l,r);
		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(){
		c[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;
			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
1Accepted3ms1956 KiB
2Accepted6ms4860 KiB
subtask210/10
3Accepted3ms2908 KiB
4Accepted3ms3108 KiB
5Accepted3ms3044 KiB
6Accepted3ms3296 KiB
7Accepted3ms3640 KiB
8Accepted3ms3640 KiB
subtask315/15
9Accepted3ms3568 KiB
10Accepted3ms3528 KiB
11Accepted3ms3636 KiB
12Accepted3ms3808 KiB
13Accepted3ms3792 KiB
14Accepted3ms4040 KiB
15Accepted3ms3996 KiB
16Accepted3ms3996 KiB
17Accepted3ms4252 KiB
18Accepted3ms4452 KiB
subtask415/15
19Accepted6ms6512 KiB
20Accepted6ms6588 KiB
21Accepted4ms6924 KiB
22Accepted4ms6532 KiB
23Accepted4ms6808 KiB
24Accepted4ms6780 KiB
25Accepted4ms6756 KiB
26Accepted4ms6912 KiB
27Accepted7ms6952 KiB
28Accepted6ms7100 KiB
29Accepted4ms7104 KiB
30Accepted4ms7092 KiB
31Accepted4ms7052 KiB
32Accepted4ms7008 KiB
33Accepted6ms7044 KiB
34Accepted6ms6972 KiB
35Accepted6ms7092 KiB
36Accepted4ms7504 KiB
37Accepted4ms7192 KiB
38Accepted4ms7388 KiB
subtask50/60
39Accepted224ms136152 KiB
40Accepted200ms131864 KiB
41Accepted204ms131992 KiB
42Accepted215ms123936 KiB
43Accepted250ms122636 KiB
44Accepted138ms140972 KiB
45Accepted187ms131112 KiB
46Accepted252ms129276 KiB
47Accepted209ms138240 KiB
48Accepted238ms129028 KiB
49Accepted244ms138260 KiB
50Accepted273ms130464 KiB
51Accepted187ms127348 KiB
52Accepted247ms135184 KiB
53Accepted238ms136940 KiB
54Accepted481ms123292 KiB
55Accepted382ms131008 KiB
56Accepted238ms133708 KiB
57Accepted261ms133608 KiB
58Time limit exceeded1.062s72412 KiB
59Accepted777ms141668 KiB
60Accepted171ms131232 KiB
61Time limit exceeded1.059s67656 KiB
62Time limit exceeded1.036s67496 KiB
63Time limit exceeded1.07s65896 KiB
64Time limit exceeded1.065s68068 KiB
65Time limit exceeded1.077s65936 KiB
66Time limit exceeded1.065s68888 KiB
67Accepted808ms140212 KiB
68Time limit exceeded1.075s66804 KiB
69Accepted930ms135596 KiB
70Accepted529ms124568 KiB
71Accepted666ms124572 KiB