42592023-03-19 23:47:28PetiMaximum felosztáscpp17Accepted 100/100216ms46560 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2236 KiB
2Accepted4ms3008 KiB
subtask210/10
3Accepted3ms2444 KiB
4Accepted3ms2592 KiB
5Accepted3ms2812 KiB
6Accepted3ms3068 KiB
7Accepted3ms3236 KiB
8Accepted3ms3424 KiB
subtask315/15
9Accepted3ms3392 KiB
10Accepted3ms3416 KiB
11Accepted3ms3524 KiB
12Accepted3ms3496 KiB
13Accepted3ms3800 KiB
14Accepted3ms4028 KiB
15Accepted3ms3992 KiB
16Accepted3ms4284 KiB
17Accepted3ms4292 KiB
18Accepted3ms4216 KiB
subtask415/15
19Accepted4ms5020 KiB
20Accepted4ms4880 KiB
21Accepted4ms5104 KiB
22Accepted4ms4720 KiB
23Accepted4ms5016 KiB
24Accepted4ms4924 KiB
25Accepted4ms4948 KiB
26Accepted4ms4952 KiB
27Accepted6ms5292 KiB
28Accepted4ms5300 KiB
29Accepted4ms5160 KiB
30Accepted4ms5156 KiB
31Accepted4ms5128 KiB
32Accepted4ms5124 KiB
33Accepted4ms5392 KiB
34Accepted4ms5340 KiB
35Accepted4ms5220 KiB
36Accepted4ms5488 KiB
37Accepted4ms5080 KiB
38Accepted4ms5292 KiB
subtask560/60
39Accepted197ms39848 KiB
40Accepted162ms36000 KiB
41Accepted165ms35820 KiB
42Accepted187ms27824 KiB
43Accepted197ms25812 KiB
44Accepted98ms45368 KiB
45Accepted136ms35232 KiB
46Accepted195ms32632 KiB
47Accepted158ms42400 KiB
48Accepted207ms32572 KiB
49Accepted178ms42532 KiB
50Accepted216ms34344 KiB
51Accepted137ms31224 KiB
52Accepted108ms39552 KiB
53Accepted112ms41660 KiB
54Accepted148ms26972 KiB
55Accepted119ms35408 KiB
56Accepted179ms37608 KiB
57Accepted208ms37588 KiB
58Accepted114ms46140 KiB
59Accepted101ms46560 KiB
60Accepted119ms35720 KiB
61Accepted108ms35824 KiB
62Accepted188ms35064 KiB
63Accepted156ms31760 KiB
64Accepted166ms36752 KiB
65Accepted157ms31676 KiB
66Accepted175ms37920 KiB
67Accepted108ms44808 KiB
68Accepted184ms33176 KiB
69Accepted128ms39800 KiB
70Accepted159ms28192 KiB
71Accepted148ms28356 KiB