Submission #224683

# Submission time Handle Problem Language Result Execution time Memory
224683 2020-04-18T15:08:30 Z PeppaPig Election Campaign (JOI15_election_campaign) C++14
30 / 100
1000 ms 30076 KB
#include <bits/stdc++.h>

#define long long long
#define iii tuple<int, int, int>
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 1e5+5;

int n, q;

struct FenwickTree {
    long t[N];
    void update(int x, long k) {
        if(x == 0) return;
        for(int i = x; i <= n; i += i & -i)
            t[i] += k;
    }
    long query(int l, int r) {
        long ret = 0;
        for(int i = r; i; i -= i & -i) ret += t[i];
        for(int i = l - 1; i; i -= i & -i) ret -= t[i];
        return ret;
    }
} T;

vector<int> g[N];
vector<iii> Q[N];

int dep[N], par[N], rot[N], spi[N], pos[N], anc[N][18];

int dfs(int u, int p) {
    dep[u] = dep[p] + 1, par[u] = anc[u][0] = p;
    for(int i = 1; i <= 17; i++) anc[u][i] = anc[anc[u][i-1]][i-1];
    int sz = 0; pii ret(0, -1);
    for(int &v : g[u]) if(v != p) {
        int z = dfs(v, u);
        sz += z, ret = max(ret, pii(z, v));
    }
    spi[u] = ret.y;
    return sz;
}

void init_hld() {
    dfs(1, 0);
    for(int i = 1, idx = 0; i <= n; i++) if(spi[par[i]] != i) 
        for(int j = i; j != -1; j = spi[j])
            pos[j] = ++idx, rot[j] = i;
}

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

int pre(int a, int b) {
    for(int i = 17; ~i; i--) if(dep[anc[a][i]] > dep[b])
        a = anc[a][i];
    return a;
}

long dp[N], ans;

long query_path(int a, int b) {
    long ret = spi[a] != -1 ? dp[spi[a]] : 0;
    while(rot[a] != rot[b]) {
        ret += T.query(pos[rot[a]], pos[a]) - dp[rot[a]];
        if(spi[par[rot[a]]] != -1) ret += dp[spi[par[rot[a]]]];
        a = par[rot[a]];
    }
    ret += T.query(pos[b], pos[a]);
    return ret;
}

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 = sum;
        if(a != u) {
            int pa = pre(a, u);
            now += query_path(a, pa) - dp[pa];
        }
        if(b != u) {
            int pb = pre(b, u);
            now += query_path(b, pb) - dp[pb];
        }
        dp[u] = max(dp[u], now + c);
    }
    ans = max(ans, dp[u]);
    if(rot[u] == u) T.update(pos[par[u]], 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);
    }
    init_hld();

    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", ans);

    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:108: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:114:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:116: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 7 ms 4992 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 109 ms 19448 KB Output is correct
6 Correct 65 ms 27896 KB Output is correct
7 Correct 120 ms 25464 KB Output is correct
8 Correct 77 ms 19320 KB Output is correct
9 Correct 110 ms 23544 KB Output is correct
10 Correct 79 ms 19064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 186 ms 29972 KB Output is correct
5 Correct 190 ms 29892 KB Output is correct
6 Correct 157 ms 29944 KB Output is correct
7 Correct 175 ms 29944 KB Output is correct
8 Correct 180 ms 29912 KB Output is correct
9 Correct 158 ms 29944 KB Output is correct
10 Correct 177 ms 29944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 186 ms 29972 KB Output is correct
5 Correct 190 ms 29892 KB Output is correct
6 Correct 157 ms 29944 KB Output is correct
7 Correct 175 ms 29944 KB Output is correct
8 Correct 180 ms 29912 KB Output is correct
9 Correct 158 ms 29944 KB Output is correct
10 Correct 177 ms 29944 KB Output is correct
11 Correct 23 ms 6016 KB Output is correct
12 Correct 199 ms 29944 KB Output is correct
13 Correct 176 ms 29920 KB Output is correct
14 Correct 166 ms 29944 KB Output is correct
15 Correct 177 ms 29944 KB Output is correct
16 Correct 169 ms 29848 KB Output is correct
17 Correct 175 ms 29816 KB Output is correct
18 Correct 179 ms 29944 KB Output is correct
19 Correct 169 ms 30076 KB Output is correct
20 Correct 177 ms 29944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 20900 KB Output is correct
2 Correct 166 ms 30020 KB Output is correct
3 Execution timed out 1096 ms 27640 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 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 109 ms 19448 KB Output is correct
6 Correct 65 ms 27896 KB Output is correct
7 Correct 120 ms 25464 KB Output is correct
8 Correct 77 ms 19320 KB Output is correct
9 Correct 110 ms 23544 KB Output is correct
10 Correct 79 ms 19064 KB Output is correct
11 Correct 9 ms 5248 KB Output is correct
12 Correct 9 ms 5248 KB Output is correct
13 Correct 9 ms 5344 KB Output is correct
14 Correct 9 ms 5248 KB Output is correct
15 Correct 9 ms 5248 KB Output is correct
16 Correct 9 ms 5248 KB Output is correct
17 Correct 9 ms 5248 KB Output is correct
18 Correct 9 ms 5248 KB Output is correct
19 Correct 9 ms 5248 KB Output is correct
20 Correct 9 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 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 109 ms 19448 KB Output is correct
6 Correct 65 ms 27896 KB Output is correct
7 Correct 120 ms 25464 KB Output is correct
8 Correct 77 ms 19320 KB Output is correct
9 Correct 110 ms 23544 KB Output is correct
10 Correct 79 ms 19064 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 186 ms 29972 KB Output is correct
15 Correct 190 ms 29892 KB Output is correct
16 Correct 157 ms 29944 KB Output is correct
17 Correct 175 ms 29944 KB Output is correct
18 Correct 180 ms 29912 KB Output is correct
19 Correct 158 ms 29944 KB Output is correct
20 Correct 177 ms 29944 KB Output is correct
21 Correct 23 ms 6016 KB Output is correct
22 Correct 199 ms 29944 KB Output is correct
23 Correct 176 ms 29920 KB Output is correct
24 Correct 166 ms 29944 KB Output is correct
25 Correct 177 ms 29944 KB Output is correct
26 Correct 169 ms 29848 KB Output is correct
27 Correct 175 ms 29816 KB Output is correct
28 Correct 179 ms 29944 KB Output is correct
29 Correct 169 ms 30076 KB Output is correct
30 Correct 177 ms 29944 KB Output is correct
31 Correct 319 ms 20900 KB Output is correct
32 Correct 166 ms 30020 KB Output is correct
33 Execution timed out 1096 ms 27640 KB Time limit exceeded
34 Halted 0 ms 0 KB -