Submission #224677

# Submission time Handle Problem Language Result Execution time Memory
224677 2020-04-18T15:01:15 Z PeppaPig Election Campaign (JOI15_election_campaign) C++14
30 / 100
1000 ms 29524 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 d1 = a != u ? query_path(a, pre(a, u)) - dp[pre(a, u)] : 0;
        long d2 = b != u ? query_path(b, pre(b, u)) - dp[pre(b, u)] : 0;
        dp[u] = max(dp[u], sum + d1 + d2 + 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:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:101: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:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:109: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 7 ms 5248 KB Output is correct
5 Correct 113 ms 18940 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 105 ms 24828 KB Output is correct
8 Correct 82 ms 18552 KB Output is correct
9 Correct 109 ms 23032 KB Output is correct
10 Correct 82 ms 18556 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 200 ms 29304 KB Output is correct
5 Correct 196 ms 29432 KB Output is correct
6 Correct 173 ms 29304 KB Output is correct
7 Correct 197 ms 29380 KB Output is correct
8 Correct 197 ms 29304 KB Output is correct
9 Correct 200 ms 29524 KB Output is correct
10 Correct 189 ms 29432 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 200 ms 29304 KB Output is correct
5 Correct 196 ms 29432 KB Output is correct
6 Correct 173 ms 29304 KB Output is correct
7 Correct 197 ms 29380 KB Output is correct
8 Correct 197 ms 29304 KB Output is correct
9 Correct 200 ms 29524 KB Output is correct
10 Correct 189 ms 29432 KB Output is correct
11 Correct 27 ms 5760 KB Output is correct
12 Correct 195 ms 29408 KB Output is correct
13 Correct 200 ms 29432 KB Output is correct
14 Correct 178 ms 29432 KB Output is correct
15 Correct 198 ms 29304 KB Output is correct
16 Correct 184 ms 29388 KB Output is correct
17 Correct 193 ms 29432 KB Output is correct
18 Correct 191 ms 29432 KB Output is correct
19 Correct 178 ms 29304 KB Output is correct
20 Correct 196 ms 29304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 296 ms 20272 KB Output is correct
2 Correct 171 ms 29304 KB Output is correct
3 Execution timed out 1097 ms 27512 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 7 ms 5248 KB Output is correct
5 Correct 113 ms 18940 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 105 ms 24828 KB Output is correct
8 Correct 82 ms 18552 KB Output is correct
9 Correct 109 ms 23032 KB Output is correct
10 Correct 82 ms 18556 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5376 KB Output is correct
13 Correct 8 ms 5376 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 8 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 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 7 ms 5248 KB Output is correct
5 Correct 113 ms 18940 KB Output is correct
6 Correct 66 ms 27384 KB Output is correct
7 Correct 105 ms 24828 KB Output is correct
8 Correct 82 ms 18552 KB Output is correct
9 Correct 109 ms 23032 KB Output is correct
10 Correct 82 ms 18556 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 200 ms 29304 KB Output is correct
15 Correct 196 ms 29432 KB Output is correct
16 Correct 173 ms 29304 KB Output is correct
17 Correct 197 ms 29380 KB Output is correct
18 Correct 197 ms 29304 KB Output is correct
19 Correct 200 ms 29524 KB Output is correct
20 Correct 189 ms 29432 KB Output is correct
21 Correct 27 ms 5760 KB Output is correct
22 Correct 195 ms 29408 KB Output is correct
23 Correct 200 ms 29432 KB Output is correct
24 Correct 178 ms 29432 KB Output is correct
25 Correct 198 ms 29304 KB Output is correct
26 Correct 184 ms 29388 KB Output is correct
27 Correct 193 ms 29432 KB Output is correct
28 Correct 191 ms 29432 KB Output is correct
29 Correct 178 ms 29304 KB Output is correct
30 Correct 196 ms 29304 KB Output is correct
31 Correct 296 ms 20272 KB Output is correct
32 Correct 171 ms 29304 KB Output is correct
33 Execution timed out 1097 ms 27512 KB Time limit exceeded
34 Halted 0 ms 0 KB -