10127 | 2024-03-27 18:09:36 | 111 | Maximum felosztás | cpp17 | Accepted 100/100 | 233ms | 146776 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),pg(N+1),nge(N+1),s(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];
}
s.clear();
for(int i=1;i<=N;i++){
while(!s.empty()&&v[s.back()]<=v[i]){
s.pop_back();
}
pg[i]=s.empty()?0:s.back();
s.push_back(i);
}
s.clear();
for(int i=N;i>=1;i--){
while(!s.empty()&&v[s.back()]<v[i]){
s.pop_back();
}
nge[i]=s.empty()?N+1:s.back();
s.push_back(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=dp[i&1^1].sum(pg[j],j-1);
dp[i&1].add(j,nge[j]-1,x);
}
}
cout<<dp[M&1].sum(N,N)<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2092 KiB | ||||
2 | Accepted | 4ms | 4876 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 3204 KiB | ||||
4 | Accepted | 3ms | 3568 KiB | ||||
5 | Accepted | 3ms | 3796 KiB | ||||
6 | Accepted | 3ms | 3780 KiB | ||||
7 | Accepted | 3ms | 3832 KiB | ||||
8 | Accepted | 3ms | 3968 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 3ms | 3916 KiB | ||||
10 | Accepted | 3ms | 4068 KiB | ||||
11 | Accepted | 3ms | 4124 KiB | ||||
12 | Accepted | 3ms | 4132 KiB | ||||
13 | Accepted | 3ms | 4436 KiB | ||||
14 | Accepted | 3ms | 4388 KiB | ||||
15 | Accepted | 3ms | 4680 KiB | ||||
16 | Accepted | 3ms | 4696 KiB | ||||
17 | Accepted | 3ms | 4720 KiB | ||||
18 | Accepted | 3ms | 4696 KiB | ||||
subtask4 | 15/15 | ||||||
19 | Accepted | 6ms | 6732 KiB | ||||
20 | Accepted | 6ms | 6936 KiB | ||||
21 | Accepted | 4ms | 7116 KiB | ||||
22 | Accepted | 4ms | 6768 KiB | ||||
23 | Accepted | 4ms | 7064 KiB | ||||
24 | Accepted | 4ms | 6884 KiB | ||||
25 | Accepted | 4ms | 6916 KiB | ||||
26 | Accepted | 4ms | 6956 KiB | ||||
27 | Accepted | 4ms | 7188 KiB | ||||
28 | Accepted | 4ms | 7228 KiB | ||||
29 | Accepted | 4ms | 7324 KiB | ||||
30 | Accepted | 4ms | 7392 KiB | ||||
31 | Accepted | 6ms | 7680 KiB | ||||
32 | Accepted | 4ms | 7732 KiB | ||||
33 | Accepted | 4ms | 7772 KiB | ||||
34 | Accepted | 4ms | 7880 KiB | ||||
35 | Accepted | 6ms | 7872 KiB | ||||
36 | Accepted | 4ms | 8096 KiB | ||||
37 | Accepted | 4ms | 7876 KiB | ||||
38 | Accepted | 4ms | 7972 KiB | ||||
subtask5 | 60/60 | ||||||
39 | Accepted | 226ms | 141980 KiB | ||||
40 | Accepted | 199ms | 137576 KiB | ||||
41 | Accepted | 203ms | 137812 KiB | ||||
42 | Accepted | 210ms | 129656 KiB | ||||
43 | Accepted | 216ms | 128092 KiB | ||||
44 | Accepted | 143ms | 146548 KiB | ||||
45 | Accepted | 172ms | 136540 KiB | ||||
46 | Accepted | 222ms | 134460 KiB | ||||
47 | Accepted | 195ms | 143552 KiB | ||||
48 | Accepted | 233ms | 134456 KiB | ||||
49 | Accepted | 200ms | 143572 KiB | ||||
50 | Accepted | 233ms | 136164 KiB | ||||
51 | Accepted | 167ms | 133008 KiB | ||||
52 | Accepted | 149ms | 140804 KiB | ||||
53 | Accepted | 173ms | 142608 KiB | ||||
54 | Accepted | 165ms | 128760 KiB | ||||
55 | Accepted | 152ms | 136580 KiB | ||||
56 | Accepted | 211ms | 139136 KiB | ||||
57 | Accepted | 223ms | 139140 KiB | ||||
58 | Accepted | 131ms | 146776 KiB | ||||
59 | Accepted | 130ms | 146724 KiB | ||||
60 | Accepted | 136ms | 136588 KiB | ||||
61 | Accepted | 128ms | 136536 KiB | ||||
62 | Accepted | 175ms | 136016 KiB | ||||
63 | Accepted | 173ms | 132896 KiB | ||||
64 | Accepted | 185ms | 137748 KiB | ||||
65 | Accepted | 175ms | 133384 KiB | ||||
66 | Accepted | 175ms | 139236 KiB | ||||
67 | Accepted | 159ms | 145580 KiB | ||||
68 | Accepted | 202ms | 135004 KiB | ||||
69 | Accepted | 152ms | 141044 KiB | ||||
70 | Accepted | 171ms | 130128 KiB | ||||
71 | Accepted | 170ms | 130076 KiB |