4259 | 2023-03-19 23:47:28 | Peti | Maximum felosztás | cpp17 | Accepted 100/100 | 216ms | 46560 KiB |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
struct segment_tree{
vector<long long> st, add;
vector<bool> prop;
int maxn;
void init(int n){
maxn = (1<<(32-__builtin_clz(n)));
st.assign(maxn*2, 0);
add.assign(maxn*2, 0);
prop.assign(maxn*2, false);
}
void upd(int x, int l, int r){
if(prop[x]) st[x] = 0;
else if(l+1 < r) st[x] = (st[2*x] + st[2*x+1] + (r-l) * add[x])%mod;
else st[x] = add[x];
}
void push(int x, int l, int r){
if(!prop[x]) return;
if(l+1 < r){
prop[2*x] = prop[2*x+1] = true;
int m = (l+r)/2;
upd(2*x, l, m);
upd(2*x+1, m, r);
}
st[x] = add[x] = 0;
prop[x] = false;
}
void update(int L, int R, long long c, int x, int l, int r){
push(x, l, r);
if(r <= L || R <= l) return;
if(L <= l && r <= R){
add[x] = (add[x] + c) % mod;
upd(x, l, r);
return;
}
int m = (l+r)/2;
update(L, R, c, 2*x, l, m);
update(L, R, c, 2*x+1, m, r);
upd(x, l, r);
}
void update(int l, int r, long long c) { update(l, r, c, 1, 0, maxn); }
long long query(int L, int R, int x, int l, int r){
push(x, l, r);
if(r <= L || R <= l) return 0;
if(L <= l && r <= R) return st[x];
int m = (l+r)/2;
return (query(L, R, 2*x, l, m) + query(L, R, 2*x+1, m, r) + (min(r, R)-max(l, L)) * add[x]) % mod;
}
long long query(int l, int r) { return query(l, r, 1, 0, maxn); }
void reset() { prop[1] = true; }
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin>>n>>m;
map<int, vector<int>> mp;
vector<int> v(n), elott(n), utan(n);
for(int i = 0; i < n; i++){
cin>>v[i];
mp[v[i]].push_back(i);
}
stack<int> s;
s.push(-1);
for(int i = 0; i < n; i++){
while(s.top() != -1 && v[s.top()] < v[i]) s.pop();
elott[i] = s.top();
s.push(i);
}
s = stack<int>();
s.push(n);
for(int i = n-1; i >= 0; i--){
while(s.top() != n && v[s.top()] <= v[i]) s.pop();
utan[i] = s.top();
s.push(i);
}
segment_tree st1, st2;
st1.init(n+1);
st2.init(n+1);
st1.update(0, 1, 1);
for(int i = 0; i < m; i++){
int x;
cin>>x;
for(int j : mp[x]){
st2.update(j+1, utan[j]+1, st1.query(elott[j]+1, j+1));
}
swap(st1, st2);
st2.reset();
}
cout<<st1.query(n, n+1)<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2236 KiB | ||||
2 | Accepted | 4ms | 3008 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2444 KiB | ||||
4 | Accepted | 3ms | 2592 KiB | ||||
5 | Accepted | 3ms | 2812 KiB | ||||
6 | Accepted | 3ms | 3068 KiB | ||||
7 | Accepted | 3ms | 3236 KiB | ||||
8 | Accepted | 3ms | 3424 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 3ms | 3392 KiB | ||||
10 | Accepted | 3ms | 3416 KiB | ||||
11 | Accepted | 3ms | 3524 KiB | ||||
12 | Accepted | 3ms | 3496 KiB | ||||
13 | Accepted | 3ms | 3800 KiB | ||||
14 | Accepted | 3ms | 4028 KiB | ||||
15 | Accepted | 3ms | 3992 KiB | ||||
16 | Accepted | 3ms | 4284 KiB | ||||
17 | Accepted | 3ms | 4292 KiB | ||||
18 | Accepted | 3ms | 4216 KiB | ||||
subtask4 | 15/15 | ||||||
19 | Accepted | 4ms | 5020 KiB | ||||
20 | Accepted | 4ms | 4880 KiB | ||||
21 | Accepted | 4ms | 5104 KiB | ||||
22 | Accepted | 4ms | 4720 KiB | ||||
23 | Accepted | 4ms | 5016 KiB | ||||
24 | Accepted | 4ms | 4924 KiB | ||||
25 | Accepted | 4ms | 4948 KiB | ||||
26 | Accepted | 4ms | 4952 KiB | ||||
27 | Accepted | 6ms | 5292 KiB | ||||
28 | Accepted | 4ms | 5300 KiB | ||||
29 | Accepted | 4ms | 5160 KiB | ||||
30 | Accepted | 4ms | 5156 KiB | ||||
31 | Accepted | 4ms | 5128 KiB | ||||
32 | Accepted | 4ms | 5124 KiB | ||||
33 | Accepted | 4ms | 5392 KiB | ||||
34 | Accepted | 4ms | 5340 KiB | ||||
35 | Accepted | 4ms | 5220 KiB | ||||
36 | Accepted | 4ms | 5488 KiB | ||||
37 | Accepted | 4ms | 5080 KiB | ||||
38 | Accepted | 4ms | 5292 KiB | ||||
subtask5 | 60/60 | ||||||
39 | Accepted | 197ms | 39848 KiB | ||||
40 | Accepted | 162ms | 36000 KiB | ||||
41 | Accepted | 165ms | 35820 KiB | ||||
42 | Accepted | 187ms | 27824 KiB | ||||
43 | Accepted | 197ms | 25812 KiB | ||||
44 | Accepted | 98ms | 45368 KiB | ||||
45 | Accepted | 136ms | 35232 KiB | ||||
46 | Accepted | 195ms | 32632 KiB | ||||
47 | Accepted | 158ms | 42400 KiB | ||||
48 | Accepted | 207ms | 32572 KiB | ||||
49 | Accepted | 178ms | 42532 KiB | ||||
50 | Accepted | 216ms | 34344 KiB | ||||
51 | Accepted | 137ms | 31224 KiB | ||||
52 | Accepted | 108ms | 39552 KiB | ||||
53 | Accepted | 112ms | 41660 KiB | ||||
54 | Accepted | 148ms | 26972 KiB | ||||
55 | Accepted | 119ms | 35408 KiB | ||||
56 | Accepted | 179ms | 37608 KiB | ||||
57 | Accepted | 208ms | 37588 KiB | ||||
58 | Accepted | 114ms | 46140 KiB | ||||
59 | Accepted | 101ms | 46560 KiB | ||||
60 | Accepted | 119ms | 35720 KiB | ||||
61 | Accepted | 108ms | 35824 KiB | ||||
62 | Accepted | 188ms | 35064 KiB | ||||
63 | Accepted | 156ms | 31760 KiB | ||||
64 | Accepted | 166ms | 36752 KiB | ||||
65 | Accepted | 157ms | 31676 KiB | ||||
66 | Accepted | 175ms | 37920 KiB | ||||
67 | Accepted | 108ms | 44808 KiB | ||||
68 | Accepted | 184ms | 33176 KiB | ||||
69 | Accepted | 128ms | 39800 KiB | ||||
70 | Accepted | 159ms | 28192 KiB | ||||
71 | Accepted | 148ms | 28356 KiB |