Submission #667936

# Submission time Handle Problem Language Result Execution time Memory
667936 2022-12-02T11:51:52 Z stevancv Election Campaign (JOI15_election_campaign) C++14
100 / 100
177 ms 32856 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 2e9;
vector<int> g[N];
int par[N][17], in[N], out[N], tsz;
void Dfs(int s, int e) {
    in[s] = ++tsz;
    par[s][0] = e;
    for (int i = 1; i < 17; i++) par[s][i] = par[par[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u != e) Dfs(u, s);
    }
    out[s] = ++tsz;
}
bool Ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
    if (Ancestor(u, v)) return u;
    if (Ancestor(v, u)) return v;
    for (int i = 16; i >= 0; i--) {
        if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i];
    }
    return par[u][0];
}
ll dp[N], sum[N];
vector<array<int, 3>> paths[N];
struct fenwick {
    ll bit[2 * N];
    void Add(int x, ll v) {
        for (; x < 2 * N; x += x & (-x)) bit[x] += v;
    }
    ll P(int x) {
        ll ans = 0;
        for (; x > 0; x -= x & (-x)) ans += bit[x];
        return ans;
    }
    ll Get(int l, int r) {
        if (l > r) return 0;
        return P(r) - P(l - 1);
    }
}f1, f2;
void Dfs1(int s, int e) {
    for (int u : g[s]) {
        if (u == e) continue;
        Dfs1(u, s);
        sum[s] += dp[u];
    }
    dp[s] = sum[s];
    f1.Add(in[s], sum[s]); f1.Add(out[s], -sum[s]);
    for (auto u : paths[s]) {
        int a = u[0]; int b = u[1]; int c = u[2];
        smax(dp[s], f1.Get(in[s], in[a]) + f1.Get(in[s] + 1, in[b]) - f2.Get(in[s] + 1, in[a]) - f2.Get(in[s] + 1, in[b]) + c);
    }
    f2.Add(in[s], dp[s]); f2.Add(out[s], -dp[s]);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    Dfs(1, 0);
    int m; cin >> m;
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        paths[Lca(a, b)].push_back({a, b, c});
    }
    Dfs1(1, 0);
    cout << dp[1] << en;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 5168 KB Output is correct
5 Correct 92 ms 21572 KB Output is correct
6 Correct 56 ms 29160 KB Output is correct
7 Correct 83 ms 26516 KB Output is correct
8 Correct 70 ms 21140 KB Output is correct
9 Correct 78 ms 24948 KB Output is correct
10 Correct 63 ms 21168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5332 KB Output is correct
4 Correct 85 ms 32480 KB Output is correct
5 Correct 93 ms 32528 KB Output is correct
6 Correct 81 ms 32552 KB Output is correct
7 Correct 84 ms 32528 KB Output is correct
8 Correct 85 ms 32572 KB Output is correct
9 Correct 82 ms 32496 KB Output is correct
10 Correct 87 ms 32604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5332 KB Output is correct
4 Correct 85 ms 32480 KB Output is correct
5 Correct 93 ms 32528 KB Output is correct
6 Correct 81 ms 32552 KB Output is correct
7 Correct 84 ms 32528 KB Output is correct
8 Correct 85 ms 32572 KB Output is correct
9 Correct 82 ms 32496 KB Output is correct
10 Correct 87 ms 32604 KB Output is correct
11 Correct 9 ms 6100 KB Output is correct
12 Correct 90 ms 32788 KB Output is correct
13 Correct 89 ms 32836 KB Output is correct
14 Correct 91 ms 32752 KB Output is correct
15 Correct 89 ms 32768 KB Output is correct
16 Correct 85 ms 32856 KB Output is correct
17 Correct 86 ms 32744 KB Output is correct
18 Correct 85 ms 32844 KB Output is correct
19 Correct 98 ms 32816 KB Output is correct
20 Correct 89 ms 32844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 24324 KB Output is correct
2 Correct 82 ms 32536 KB Output is correct
3 Correct 148 ms 29476 KB Output is correct
4 Correct 102 ms 24040 KB Output is correct
5 Correct 141 ms 28936 KB Output is correct
6 Correct 111 ms 24056 KB Output is correct
7 Correct 177 ms 28616 KB Output is correct
8 Correct 131 ms 24616 KB Output is correct
9 Correct 85 ms 32544 KB Output is correct
10 Correct 146 ms 27612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 5168 KB Output is correct
5 Correct 92 ms 21572 KB Output is correct
6 Correct 56 ms 29160 KB Output is correct
7 Correct 83 ms 26516 KB Output is correct
8 Correct 70 ms 21140 KB Output is correct
9 Correct 78 ms 24948 KB Output is correct
10 Correct 63 ms 21168 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 3 ms 5372 KB Output is correct
13 Correct 4 ms 5332 KB Output is correct
14 Correct 5 ms 5308 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5332 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 4 ms 5308 KB Output is correct
19 Correct 4 ms 5308 KB Output is correct
20 Correct 4 ms 5308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 3 ms 5040 KB Output is correct
4 Correct 3 ms 5168 KB Output is correct
5 Correct 92 ms 21572 KB Output is correct
6 Correct 56 ms 29160 KB Output is correct
7 Correct 83 ms 26516 KB Output is correct
8 Correct 70 ms 21140 KB Output is correct
9 Correct 78 ms 24948 KB Output is correct
10 Correct 63 ms 21168 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 5 ms 5332 KB Output is correct
14 Correct 85 ms 32480 KB Output is correct
15 Correct 93 ms 32528 KB Output is correct
16 Correct 81 ms 32552 KB Output is correct
17 Correct 84 ms 32528 KB Output is correct
18 Correct 85 ms 32572 KB Output is correct
19 Correct 82 ms 32496 KB Output is correct
20 Correct 87 ms 32604 KB Output is correct
21 Correct 9 ms 6100 KB Output is correct
22 Correct 90 ms 32788 KB Output is correct
23 Correct 89 ms 32836 KB Output is correct
24 Correct 91 ms 32752 KB Output is correct
25 Correct 89 ms 32768 KB Output is correct
26 Correct 85 ms 32856 KB Output is correct
27 Correct 86 ms 32744 KB Output is correct
28 Correct 85 ms 32844 KB Output is correct
29 Correct 98 ms 32816 KB Output is correct
30 Correct 89 ms 32844 KB Output is correct
31 Correct 128 ms 24324 KB Output is correct
32 Correct 82 ms 32536 KB Output is correct
33 Correct 148 ms 29476 KB Output is correct
34 Correct 102 ms 24040 KB Output is correct
35 Correct 141 ms 28936 KB Output is correct
36 Correct 111 ms 24056 KB Output is correct
37 Correct 177 ms 28616 KB Output is correct
38 Correct 131 ms 24616 KB Output is correct
39 Correct 85 ms 32544 KB Output is correct
40 Correct 146 ms 27612 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 3 ms 5372 KB Output is correct
43 Correct 4 ms 5332 KB Output is correct
44 Correct 5 ms 5308 KB Output is correct
45 Correct 3 ms 5204 KB Output is correct
46 Correct 4 ms 5332 KB Output is correct
47 Correct 4 ms 5204 KB Output is correct
48 Correct 4 ms 5308 KB Output is correct
49 Correct 4 ms 5308 KB Output is correct
50 Correct 4 ms 5308 KB Output is correct
51 Correct 141 ms 24880 KB Output is correct
52 Correct 85 ms 32840 KB Output is correct
53 Correct 155 ms 28200 KB Output is correct
54 Correct 101 ms 24316 KB Output is correct
55 Correct 129 ms 24644 KB Output is correct
56 Correct 95 ms 32748 KB Output is correct
57 Correct 139 ms 28816 KB Output is correct
58 Correct 102 ms 24224 KB Output is correct
59 Correct 134 ms 24820 KB Output is correct
60 Correct 85 ms 32732 KB Output is correct
61 Correct 150 ms 28856 KB Output is correct
62 Correct 109 ms 24020 KB Output is correct
63 Correct 136 ms 24516 KB Output is correct
64 Correct 89 ms 32788 KB Output is correct
65 Correct 152 ms 28652 KB Output is correct
66 Correct 103 ms 24356 KB Output is correct
67 Correct 131 ms 24484 KB Output is correct
68 Correct 92 ms 32808 KB Output is correct
69 Correct 134 ms 27592 KB Output is correct
70 Correct 105 ms 24456 KB Output is correct