Submission #328717

# Submission time Handle Problem Language Result Execution time Memory
328717 2020-11-17T18:26:30 Z dolphingarlic Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
86 ms 21880 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const ll MOD = 1e9 + 7;

int n, m = 0;
ll h[100002], w[100002];
ll node_h[100002], node_w[100002], ans = 0;
pair<ll, int> srt[100002];
int cmp[100002], nd[100002], cnt = 0;
set<int> graph[100002];

int find(int A) {
    while (A != cmp[A]) cmp[A] = cmp[cmp[A]], A = cmp[A];
    return A;
}

void onion(int A, int B) {
    if (find(A) == find(B)) return;
    node_w[nd[find(B)]] += node_w[nd[find(A)]];
    if (node_w[nd[find(B)]] >= MOD) node_w[nd[find(B)]] -= MOD;
    graph[nd[find(B)]].insert(nd[find(A)]);
    nd[find(A)] = nd[find(B)];
    cmp[find(A)] = cmp[find(B)];
}

ll c2(ll num) { return num * (num - 1) % MOD * 500000004 % MOD; }

void dfs(int node, ll depth, ll prv = 0) {
    ans += c2(node_w[node] + 1) * (c2(depth - prv + 1) + (depth - prv) * prv % MOD) % MOD;
    if (ans >= MOD) ans -= MOD;
    for (int i : graph[node]) dfs(i, node_h[i], depth);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        srt[i] = {h[i], i};
    }
    for (int i = 1; i <= n; i++) cin >> w[i];
    sort(srt + 1, srt + n + 1, greater<pair<ll, int>>());

    iota(cmp + 1, cmp + n + 1, 1);
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && srt[j].first == srt[i].first) j++;
        for (int k = i; k < j; k++) {
            int x = srt[k].second;
            if (k == i || find(x) != find(srt[k - 1].second)) {
                nd[x] = ++cnt;
                node_h[cnt] = h[x];
                node_w[cnt] = w[x];
            } else {
                node_w[cnt] += w[x];
                if (node_w[cnt] >= MOD) node_w[cnt] -= MOD;
            }
            if (h[x] < h[x + 1]) onion(x + 1, x);
            if (h[x] <= h[x - 1]) onion(x - 1, x);
        }
    }

    dfs(cnt, node_h[cnt]);
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5108 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 23 ms 10220 KB Output is correct
4 Correct 40 ms 13676 KB Output is correct
5 Correct 43 ms 15340 KB Output is correct
6 Correct 35 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5112 KB Output is correct
2 Correct 7 ms 5612 KB Output is correct
3 Correct 19 ms 8044 KB Output is correct
4 Correct 34 ms 10860 KB Output is correct
5 Correct 38 ms 10988 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 6 ms 5612 KB Output is correct
4 Correct 19 ms 8044 KB Output is correct
5 Correct 35 ms 10860 KB Output is correct
6 Correct 38 ms 10988 KB Output is correct
7 Correct 4 ms 5248 KB Output is correct
8 Correct 8 ms 6764 KB Output is correct
9 Correct 22 ms 10220 KB Output is correct
10 Correct 47 ms 21740 KB Output is correct
11 Correct 46 ms 21868 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 5 ms 5228 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5192 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 4 ms 5248 KB Output is correct
15 Correct 4 ms 5228 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5228 KB Output is correct
11 Correct 24 ms 10348 KB Output is correct
12 Correct 39 ms 13676 KB Output is correct
13 Correct 43 ms 15340 KB Output is correct
14 Correct 35 ms 12140 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 7 ms 5632 KB Output is correct
17 Correct 19 ms 8044 KB Output is correct
18 Correct 39 ms 10860 KB Output is correct
19 Correct 37 ms 10988 KB Output is correct
20 Correct 4 ms 5228 KB Output is correct
21 Correct 8 ms 6764 KB Output is correct
22 Correct 23 ms 10092 KB Output is correct
23 Correct 48 ms 21756 KB Output is correct
24 Correct 48 ms 21868 KB Output is correct
25 Correct 3 ms 5100 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5228 KB Output is correct
28 Correct 4 ms 5228 KB Output is correct
29 Correct 4 ms 5228 KB Output is correct
30 Correct 9 ms 6252 KB Output is correct
31 Correct 9 ms 6252 KB Output is correct
32 Correct 33 ms 11116 KB Output is correct
33 Correct 36 ms 11116 KB Output is correct
34 Correct 78 ms 17024 KB Output is correct
35 Correct 64 ms 17004 KB Output is correct
36 Correct 79 ms 17132 KB Output is correct
37 Correct 86 ms 17132 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 85 ms 17132 KB Output is correct
40 Correct 78 ms 17260 KB Output is correct
41 Correct 59 ms 18156 KB Output is correct
42 Correct 47 ms 21880 KB Output is correct