Submission #997771

# Submission time Handle Problem Language Result Execution time Memory
997771 2024-06-12T19:41:23 Z RaresFelix Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
70 ms 9816 KB
#include <bits/stdc++.h>

using namespace std;

#define int ll 
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;

const int MOD = 1e9 + 7;
const int inv2 = 5e8 + 4;

struct DSU {
    int re = 0;
    vi e, s;
    DSU(int n, const vi &S0) : e(n, -1), s(S0) {}

    int repr(int u) {
        while(e[u] >= 0) u = e[u];
        return u;
    }

    void join(int u, int v) {
        u = repr(u);
        v = repr(v);
        if(u == v) return;
        if(e[u] > e[v]) swap(u, v);
        e[u] += e[v];
        e[v] = u;
        re = (re - 1ll * s[u] * (s[u] + 1) % MOD * int(5e8 + 4) % MOD) % MOD;
        re = (re - 1ll * s[v] * (s[v] + 1) % MOD * int(5e8 + 4) % MOD) % MOD;
        s[u] += s[v];
        s[u] %= MOD;
        re = (re + 1ll * s[u] * (s[u] + 1) % MOD * int(5e8 + 4) % MOD) % MOD;

        re = (re + MOD) % MOD;
    }
};

signed main() {
    int n;
    cin >> n;
    vi W(n);
    vector<ii> H(n);
    for(int i = 0; i < n; ++i) {
        cin >> H[i].first;
        H[i].second = i;
    }
    for(int i = 0; i < n; ++i)
        cin >> W[i];
    DSU Sol(n, W);
    vi on(n, 0);
    sort(H.rbegin(), H.rend());
    ll s = 0, ant = 0;
    ll re = 0, hant = 0;
    H.push_back({0, 0});
    for(int i = 0; i < n; ++i) {
        int dr = i;
        do {
            int p = H[dr].second;
            s += 1ll * W[p] * (W[p] + 1) % MOD * inv2 % MOD;
            s %= MOD;
            on[p] = 1;
            if(p && on[p - 1]) Sol.join(p, p - 1);
            if(p + 1 < n && on[p + 1]) Sol.join(p, p + 1);
            ++dr;
        } while(dr < n && H[dr].first == H[dr - 1].first);
        int h = H[i].first;
        int fact = h * (h + 1) % MOD * inv2 % MOD;
        int uh = H[dr].first;
        int f2 = uh * (uh + 1) % MOD * inv2 % MOD;
        fact = fact - f2;
        fact = (fact % MOD + MOD) % MOD;

        ll cur = (s + Sol.re) % MOD;
        re = (re + cur * fact % MOD) % MOD;
        i = dr - 1;
    }
    cout << re << "\n";
    return 0;
}

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:54:15: warning: unused variable 'ant' [-Wunused-variable]
   54 |     ll s = 0, ant = 0;
      |               ^~~
fancyfence.cpp:55:16: warning: unused variable 'hant' [-Wunused-variable]
   55 |     ll re = 0, hant = 0;
      |                ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 21 ms 4156 KB Output is correct
4 Correct 42 ms 8532 KB Output is correct
5 Correct 42 ms 9036 KB Output is correct
6 Correct 40 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 6 ms 1212 KB Output is correct
3 Correct 26 ms 4432 KB Output is correct
4 Correct 57 ms 8532 KB Output is correct
5 Correct 57 ms 8648 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 6 ms 1116 KB Output is correct
4 Correct 26 ms 4364 KB Output is correct
5 Correct 54 ms 9044 KB Output is correct
6 Correct 55 ms 9812 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 5 ms 1116 KB Output is correct
9 Correct 28 ms 4392 KB Output is correct
10 Correct 53 ms 8404 KB Output is correct
11 Correct 54 ms 9284 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 436 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 444 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 432 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 21 ms 3928 KB Output is correct
12 Correct 41 ms 7764 KB Output is correct
13 Correct 42 ms 7764 KB Output is correct
14 Correct 43 ms 8784 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 1116 KB Output is correct
17 Correct 27 ms 4424 KB Output is correct
18 Correct 70 ms 8648 KB Output is correct
19 Correct 54 ms 8528 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 6 ms 1056 KB Output is correct
22 Correct 29 ms 4408 KB Output is correct
23 Correct 61 ms 8392 KB Output is correct
24 Correct 54 ms 8528 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 6 ms 1116 KB Output is correct
31 Correct 6 ms 1116 KB Output is correct
32 Correct 29 ms 4424 KB Output is correct
33 Correct 31 ms 4444 KB Output is correct
34 Correct 59 ms 9544 KB Output is correct
35 Correct 58 ms 8276 KB Output is correct
36 Correct 62 ms 9168 KB Output is correct
37 Correct 65 ms 9556 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 62 ms 9044 KB Output is correct
40 Correct 61 ms 9804 KB Output is correct
41 Correct 65 ms 9556 KB Output is correct
42 Correct 54 ms 9816 KB Output is correct