101232024-03-27 17:46:45111Maximum felosztáscpp17Wrong answer 25/1001.103s136820 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,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]*(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
1Accepted3ms1956 KiB
2Accepted10ms4864 KiB
subtask210/10
3Accepted3ms3040 KiB
4Accepted3ms3048 KiB
5Accepted3ms3288 KiB
6Accepted3ms3492 KiB
7Accepted3ms3728 KiB
8Accepted3ms3996 KiB
subtask315/15
9Accepted4ms4196 KiB
10Accepted3ms4248 KiB
11Accepted3ms4172 KiB
12Accepted3ms4456 KiB
13Accepted3ms4496 KiB
14Accepted3ms4416 KiB
15Accepted4ms4360 KiB
16Accepted4ms4364 KiB
17Accepted4ms4584 KiB
18Accepted4ms4812 KiB
subtask40/15
19Accepted17ms6624 KiB
20Accepted10ms6620 KiB
21Accepted6ms6804 KiB
22Accepted6ms6856 KiB
23Accepted4ms7008 KiB
24Accepted4ms7092 KiB
25Accepted10ms7088 KiB
26Accepted10ms6996 KiB
27Accepted24ms7076 KiB
28Accepted21ms7160 KiB
29Wrong answer4ms7052 KiB
30Wrong answer6ms7044 KiB
31Accepted8ms6892 KiB
32Accepted8ms6896 KiB
33Accepted14ms7052 KiB
34Accepted14ms6964 KiB
35Accepted12ms7180 KiB
36Accepted10ms7324 KiB
37Wrong answer6ms7060 KiB
38Wrong answer6ms7204 KiB
subtask50/60
39Time limit exceeded1.103s69636 KiB
40Time limit exceeded1.065s67412 KiB
41Time limit exceeded1.062s67548 KiB
42Time limit exceeded1.072s63720 KiB
43Time limit exceeded1.082s62956 KiB
44Time limit exceeded1.067s71988 KiB
45Time limit exceeded1.077s66988 KiB
46Time limit exceeded1.046s66052 KiB
47Time limit exceeded1.062s70472 KiB
48Time limit exceeded1.046s66052 KiB
49Time limit exceeded1.065s70604 KiB
50Time limit exceeded1.065s66988 KiB
51Time limit exceeded1.082s65400 KiB
52Wrong answer583ms135096 KiB
53Wrong answer301ms136820 KiB
54Wrong answer657ms123032 KiB
55Wrong answer421ms130924 KiB
56Time limit exceeded1.072s68588 KiB
57Time limit exceeded1.062s68492 KiB
58Time limit exceeded1.054s72344 KiB
59Time limit exceeded1.059s72352 KiB
60Wrong answer518ms130920 KiB
61Time limit exceeded1.059s67192 KiB
62Time limit exceeded1.082s67084 KiB
63Time limit exceeded1.062s65752 KiB
64Time limit exceeded1.074s67920 KiB
65Time limit exceeded1.057s65648 KiB
66Time limit exceeded1.062s68560 KiB
67Time limit exceeded1.078s71556 KiB
68Time limit exceeded1.07s66288 KiB
69Time limit exceeded1.057s69132 KiB
70Time limit exceeded1.046s63856 KiB
71Time limit exceeded1.077s63684 KiB