Submission #224693

# Submission time Handle Problem Language Result Execution time Memory
224693 2020-04-18T15:37:21 Z PeppaPig Election Campaign (JOI15_election_campaign) C++14
100 / 100
263 ms 30584 KB
#include <bits/stdc++.h>

#define long long long
#define iii tuple<int, int, int>

using namespace std;

const int N = 1e5+5;

long t[N];

void update(int x, long k) {
    for(int i = x; i < N; i += i & -i)
        t[i] += k;
}

void update(int x, int y, long k) {
    update(x, k), update(y + 1, -k);
}

long query(int x) {
    long ret = 0;
    for(int i = x; i; i -= i & -i) ret += t[i];
    return ret;
}

int n, q;
int par[N][18], dep[N], in[N], out[N];
vector<int> g[N];
vector<iii> Q[N];

void preprocess(int u, int p) {
    static int idx = 0;
    par[u][0] = p, dep[u] = dep[p] + 1, in[u] = ++idx;
    for(int i = 1; i <= 17; i++) par[u][i] = par[par[u][i-1]][i-1];
    for(int v : g[u]) if(v != p) preprocess(v, u);
    out[u] = idx;
}

int lca(int a, int b) {
    if(dep[a] < dep[b]) swap(a, b);
    for(int i = 17; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
    if(a == b) return a;
    for(int i = 17; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
    return par[a][0];
}

long dp[N];

void solve(int u, int p) {
    long sum = 0;
    for(int v : g[u]) if(v != p) {
        solve(v, u);
        sum += dp[v];
    }
    dp[u] = sum;
    for(iii t : Q[u]) {
        int a, b, c; tie(a, b, c) = t;
        long now = 0;
        if(a != u) now += query(in[a]);
        if(b != u) now += query(in[b]);
        dp[u] = max(dp[u], sum + now + c);
    }
    update(in[u], out[u], sum - dp[u]);
}

int main() {
    scanf("%d", &n);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    preprocess(1, 0);

    scanf("%d", &q);
    for(int i = 1, a, b, c; i <= q; i++) {
        scanf("%d %d %d", &a, &b, &c);
        Q[lca(a, b)].emplace_back(a, b, c);
    }
    solve(1, 0);
    printf("%lld\n", dp[1]);

    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 110 ms 18424 KB Output is correct
6 Correct 64 ms 26104 KB Output is correct
7 Correct 107 ms 23288 KB Output is correct
8 Correct 89 ms 18680 KB Output is correct
9 Correct 102 ms 21752 KB Output is correct
10 Correct 80 ms 18680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 152 ms 28024 KB Output is correct
5 Correct 158 ms 28280 KB Output is correct
6 Correct 145 ms 28152 KB Output is correct
7 Correct 156 ms 28024 KB Output is correct
8 Correct 146 ms 28024 KB Output is correct
9 Correct 138 ms 28152 KB Output is correct
10 Correct 151 ms 28024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 152 ms 28024 KB Output is correct
5 Correct 158 ms 28280 KB Output is correct
6 Correct 145 ms 28152 KB Output is correct
7 Correct 156 ms 28024 KB Output is correct
8 Correct 146 ms 28024 KB Output is correct
9 Correct 138 ms 28152 KB Output is correct
10 Correct 151 ms 28024 KB Output is correct
11 Correct 21 ms 6008 KB Output is correct
12 Correct 156 ms 28024 KB Output is correct
13 Correct 157 ms 28128 KB Output is correct
14 Correct 149 ms 28024 KB Output is correct
15 Correct 153 ms 28152 KB Output is correct
16 Correct 143 ms 28152 KB Output is correct
17 Correct 156 ms 28156 KB Output is correct
18 Correct 154 ms 28024 KB Output is correct
19 Correct 144 ms 28152 KB Output is correct
20 Correct 164 ms 28024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 19884 KB Output is correct
2 Correct 148 ms 28024 KB Output is correct
3 Correct 245 ms 25592 KB Output is correct
4 Correct 140 ms 22448 KB Output is correct
5 Correct 224 ms 26616 KB Output is correct
6 Correct 143 ms 22380 KB Output is correct
7 Correct 263 ms 26420 KB Output is correct
8 Correct 192 ms 22264 KB Output is correct
9 Correct 139 ms 30200 KB Output is correct
10 Correct 260 ms 25488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 110 ms 18424 KB Output is correct
6 Correct 64 ms 26104 KB Output is correct
7 Correct 107 ms 23288 KB Output is correct
8 Correct 89 ms 18680 KB Output is correct
9 Correct 102 ms 21752 KB Output is correct
10 Correct 80 ms 18680 KB Output is correct
11 Correct 9 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 8 ms 5248 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 8 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 110 ms 18424 KB Output is correct
6 Correct 64 ms 26104 KB Output is correct
7 Correct 107 ms 23288 KB Output is correct
8 Correct 89 ms 18680 KB Output is correct
9 Correct 102 ms 21752 KB Output is correct
10 Correct 80 ms 18680 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 152 ms 28024 KB Output is correct
15 Correct 158 ms 28280 KB Output is correct
16 Correct 145 ms 28152 KB Output is correct
17 Correct 156 ms 28024 KB Output is correct
18 Correct 146 ms 28024 KB Output is correct
19 Correct 138 ms 28152 KB Output is correct
20 Correct 151 ms 28024 KB Output is correct
21 Correct 21 ms 6008 KB Output is correct
22 Correct 156 ms 28024 KB Output is correct
23 Correct 157 ms 28128 KB Output is correct
24 Correct 149 ms 28024 KB Output is correct
25 Correct 153 ms 28152 KB Output is correct
26 Correct 143 ms 28152 KB Output is correct
27 Correct 156 ms 28156 KB Output is correct
28 Correct 154 ms 28024 KB Output is correct
29 Correct 144 ms 28152 KB Output is correct
30 Correct 164 ms 28024 KB Output is correct
31 Correct 207 ms 19884 KB Output is correct
32 Correct 148 ms 28024 KB Output is correct
33 Correct 245 ms 25592 KB Output is correct
34 Correct 140 ms 22448 KB Output is correct
35 Correct 224 ms 26616 KB Output is correct
36 Correct 143 ms 22380 KB Output is correct
37 Correct 263 ms 26420 KB Output is correct
38 Correct 192 ms 22264 KB Output is correct
39 Correct 139 ms 30200 KB Output is correct
40 Correct 260 ms 25488 KB Output is correct
41 Correct 9 ms 5248 KB Output is correct
42 Correct 8 ms 5248 KB Output is correct
43 Correct 8 ms 5248 KB Output is correct
44 Correct 8 ms 5248 KB Output is correct
45 Correct 8 ms 5248 KB Output is correct
46 Correct 8 ms 5248 KB Output is correct
47 Correct 8 ms 5248 KB Output is correct
48 Correct 8 ms 5248 KB Output is correct
49 Correct 8 ms 5248 KB Output is correct
50 Correct 8 ms 5248 KB Output is correct
51 Correct 199 ms 22520 KB Output is correct
52 Correct 151 ms 30456 KB Output is correct
53 Correct 262 ms 25812 KB Output is correct
54 Correct 144 ms 22708 KB Output is correct
55 Correct 184 ms 22316 KB Output is correct
56 Correct 170 ms 30584 KB Output is correct
57 Correct 227 ms 26488 KB Output is correct
58 Correct 147 ms 22704 KB Output is correct
59 Correct 203 ms 22520 KB Output is correct
60 Correct 150 ms 30456 KB Output is correct
61 Correct 231 ms 26616 KB Output is correct
62 Correct 143 ms 22480 KB Output is correct
63 Correct 199 ms 22240 KB Output is correct
64 Correct 153 ms 30456 KB Output is correct
65 Correct 246 ms 26420 KB Output is correct
66 Correct 149 ms 22692 KB Output is correct
67 Correct 189 ms 22252 KB Output is correct
68 Correct 156 ms 30456 KB Output is correct
69 Correct 223 ms 25336 KB Output is correct
70 Correct 147 ms 22876 KB Output is correct