Submission #224681

# Submission time Handle Problem Language Result Execution time Memory
224681 2020-04-18T15:03:39 Z PeppaPig Election Campaign (JOI15_election_campaign) C++14
30 / 100
1000 ms 29480 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;

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

int n, q;
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 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 108 ms 18936 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 113 ms 24824 KB Output is correct
8 Correct 82 ms 18680 KB Output is correct
9 Correct 110 ms 23032 KB Output is correct
10 Correct 92 ms 18552 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 5376 KB Output is correct
4 Correct 210 ms 29480 KB Output is correct
5 Correct 180 ms 29304 KB Output is correct
6 Correct 170 ms 29416 KB Output is correct
7 Correct 183 ms 29304 KB Output is correct
8 Correct 186 ms 29304 KB Output is correct
9 Correct 158 ms 29308 KB Output is correct
10 Correct 187 ms 29304 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 5376 KB Output is correct
4 Correct 210 ms 29480 KB Output is correct
5 Correct 180 ms 29304 KB Output is correct
6 Correct 170 ms 29416 KB Output is correct
7 Correct 183 ms 29304 KB Output is correct
8 Correct 186 ms 29304 KB Output is correct
9 Correct 158 ms 29308 KB Output is correct
10 Correct 187 ms 29304 KB Output is correct
11 Correct 25 ms 5760 KB Output is correct
12 Correct 192 ms 29408 KB Output is correct
13 Correct 191 ms 29304 KB Output is correct
14 Correct 162 ms 29432 KB Output is correct
15 Correct 204 ms 29428 KB Output is correct
16 Correct 165 ms 29392 KB Output is correct
17 Correct 188 ms 29408 KB Output is correct
18 Correct 205 ms 29420 KB Output is correct
19 Correct 161 ms 29432 KB Output is correct
20 Correct 187 ms 29304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 20400 KB Output is correct
2 Correct 157 ms 29304 KB Output is correct
3 Execution timed out 1095 ms 26744 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 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 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 108 ms 18936 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 113 ms 24824 KB Output is correct
8 Correct 82 ms 18680 KB Output is correct
9 Correct 110 ms 23032 KB Output is correct
10 Correct 92 ms 18552 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 9 ms 5376 KB Output is correct
13 Correct 9 ms 5248 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 8 ms 5248 KB Output is correct
20 Correct 8 ms 5376 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 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 108 ms 18936 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 113 ms 24824 KB Output is correct
8 Correct 82 ms 18680 KB Output is correct
9 Correct 110 ms 23032 KB Output is correct
10 Correct 92 ms 18552 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 5376 KB Output is correct
14 Correct 210 ms 29480 KB Output is correct
15 Correct 180 ms 29304 KB Output is correct
16 Correct 170 ms 29416 KB Output is correct
17 Correct 183 ms 29304 KB Output is correct
18 Correct 186 ms 29304 KB Output is correct
19 Correct 158 ms 29308 KB Output is correct
20 Correct 187 ms 29304 KB Output is correct
21 Correct 25 ms 5760 KB Output is correct
22 Correct 192 ms 29408 KB Output is correct
23 Correct 191 ms 29304 KB Output is correct
24 Correct 162 ms 29432 KB Output is correct
25 Correct 204 ms 29428 KB Output is correct
26 Correct 165 ms 29392 KB Output is correct
27 Correct 188 ms 29408 KB Output is correct
28 Correct 205 ms 29420 KB Output is correct
29 Correct 161 ms 29432 KB Output is correct
30 Correct 187 ms 29304 KB Output is correct
31 Correct 293 ms 20400 KB Output is correct
32 Correct 157 ms 29304 KB Output is correct
33 Execution timed out 1095 ms 26744 KB Time limit exceeded
34 Halted 0 ms 0 KB -