Submission #930378

# Submission time Handle Problem Language Result Execution time Memory
930378 2024-02-19T13:43:21 Z vjudge1 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
75 ms 12428 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mxN = 1e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
const int MOD = 1e9 + 7;

int pref[mxN], nex[mxN], prv[mxN], seg[4 * mxN], lazy[4 * mxN];
pair<int,int> arr[mxN];

void push(int ind, int l, int r) {
    if (lazy[ind]) {
        seg[ind] = (r - l + 1) * lazy[ind] % MOD;
        if (l != r) {
            lazy[2*ind] = lazy[ind];
            lazy[2*ind+1] = lazy[ind];
        }
        lazy[ind] = 0;
    }
}

void update(int lo, int hi, int val, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind, l, r);
    if (lo > r || l > hi) {
        return;
    }

    if (lo <= l && r <= hi) {
        lazy[ind] = val;
        push(ind, l, r);
        return;
    }

    int mid = (l + r) / 2;
    update(lo, hi, val, 2*ind, l, mid);
    update(lo, hi, val, 2*ind+1, mid+1, r);
}

int query(int pos, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind, l, r);
    if (l == r) {
        return seg[ind];
    }

    int mid = (l + r) / 2;
    if (pos <= mid) {
        return query(pos, 2*ind, l, mid);
    }
    else {
        return query(pos, 2*ind+1, mid+1, r);
    }
}

int nc2(int n) {
    return n * (n - 1) / 2;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    int n;
    cin >> n;
    
    vector<pair<int,int>> pos;
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i].first;
        pos.push_back({arr[i].first, i});
    }
    sort(pos.begin(), pos.end());

    for (int i = 1; i <= n; ++i) {
        cin >> arr[i].second;
        pref[i] = pref[i - 1] + arr[i].second;
    }
    
    vector<int> st = {n + 1};
    for (int i = n; i >= 1; --i) {
        while (st.size() && arr[i].first <= arr[st.back()].first) {
            st.pop_back();
        }
        assert(st.size());
        nex[i] = st.back();
        st.push_back(i);
    }
    
    st = {0};
    for (int i = 1; i <= n; ++i) {
        while (st.size() && arr[i].first <= arr[st.back()].first) {
            st.pop_back();
        }
        assert(st.size());
        prv[i] = st.back();
        st.push_back(i);
    }

    int ans = 0;
    for (auto [h, i] : pos) {
        int sub = query(i); 
        int w = (pref[nex[i] - 1] - pref[prv[i]]) % MOD;

        ans += (nc2(h + 1) - sub) % MOD * (nc2(w + 1) % MOD) % MOD;
        ans %= MOD;

        update(prv[i] + 1, nex[i] - 1, nc2(h + 1) % MOD);
    }
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 25 ms 7588 KB Output is correct
4 Correct 53 ms 10596 KB Output is correct
5 Correct 52 ms 10440 KB Output is correct
6 Correct 45 ms 10616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 6 ms 3296 KB Output is correct
3 Correct 28 ms 7688 KB Output is correct
4 Correct 58 ms 10440 KB Output is correct
5 Correct 55 ms 10436 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 6 ms 3296 KB Output is correct
4 Correct 36 ms 7644 KB Output is correct
5 Correct 62 ms 10448 KB Output is correct
6 Correct 57 ms 10440 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 6 ms 3552 KB Output is correct
9 Correct 27 ms 7884 KB Output is correct
10 Correct 54 ms 12244 KB Output is correct
11 Correct 56 ms 12428 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2648 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 26 ms 7584 KB Output is correct
12 Correct 52 ms 10436 KB Output is correct
13 Correct 50 ms 10440 KB Output is correct
14 Correct 50 ms 10536 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 6 ms 3296 KB Output is correct
17 Correct 27 ms 7636 KB Output is correct
18 Correct 55 ms 10588 KB Output is correct
19 Correct 55 ms 10440 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 6 ms 3552 KB Output is correct
22 Correct 28 ms 7868 KB Output is correct
23 Correct 57 ms 12188 KB Output is correct
24 Correct 56 ms 12228 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 2 ms 2568 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 7 ms 3296 KB Output is correct
31 Correct 7 ms 3296 KB Output is correct
32 Correct 31 ms 7664 KB Output is correct
33 Correct 31 ms 7640 KB Output is correct
34 Correct 70 ms 11220 KB Output is correct
35 Correct 62 ms 10440 KB Output is correct
36 Correct 64 ms 10452 KB Output is correct
37 Correct 68 ms 10444 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 66 ms 10580 KB Output is correct
40 Correct 75 ms 11508 KB Output is correct
41 Correct 62 ms 10696 KB Output is correct
42 Correct 56 ms 12008 KB Output is correct