Submission #197799

# Submission time Handle Problem Language Result Execution time Memory
197799 2020-01-23T08:54:47 Z DS007 Election Campaign (JOI15_election_campaign) C++14
100 / 100
407 ms 43380 KB
#include <bits/stdc++.h>
using namespace std;

struct query {
    int a, b, c;
};

const int N = 1e5;
vector<int> adj[N];
long long subtree[N], dp[N];
vector<query> q[N];
bool check[N];
int parent[N];
int n, m;

vector<int> euler, depth;
int first[N], last[N];
int c = 0;

int sparse[N * 2][18];
long long t[N * 8];

void dfs(int v, int p = -1, int d = 0) {
    assert(first[v] == 0);
    first[v] = c++;
    parent[v] = p;
    euler.push_back(v);
    depth.push_back(d);

    for (int i : adj[v]) {
        if (i != p) {
            dfs(i, v, d + 1);
            euler.push_back(v);
            depth.push_back(d);
            c++;
        }
    }

    last[v] = c - 1;
}

int merge(int a, int b) {
    return depth[a] < depth[b] ? a : b;
}

void build_sparse() {
    for (int i = 0; i < euler.size(); i++)
        sparse[i][0] = i;

    for (int i = 1; i <= log2(euler.size()); i++) {
        for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
            sparse[j][i] = merge(sparse[j][i - 1], sparse[j + (1 << (i - 1))][i - 1]);
    }
}

int query_lca(int u, int v) {
    if (u == v) return u;
    int f1 = min(first[u], first[v]), f2 = max(first[v], first[u]), diff = f2 - f1;
    int dx = log2(diff);
    return euler[merge(sparse[f1][dx], sparse[f2 - (1 << dx)][dx])];
}

bool cmp(query u, query v) {
    int lca1 = query_lca(u.a, u.b), lca2 = query_lca(v.a, v.b);
    if (depth[first[lca1]] == depth[first[lca2]])
        return lca1 < lca2;
    return depth[first[lca1]] < depth[first[lca2]];
}

long long query_tree(int v, int l, int r, int tl, int tr) {
    if (tl > tr)
        return 0;
    else if (l == tl && r == tr)
        return t[v];

    int mid = (l + r) / 2;
    return query_tree(v * 2, l, mid, tl, min(mid, tr)) + query_tree(v * 2 + 1, mid + 1, r, max(tl, mid + 1), tr);
}

void update(int v, int l, int r, int tl, long long val) {
    if (l == r && l == tl) {
        t[v] = val;
        return;
    }

    int mid = (l + r) / 2;
    if (tl <= mid)
        update(v * 2, l, mid, tl, val);
    else
        update(v * 2 + 1, mid + 1, r, tl, val);

    t[v] = t[v * 2] + t[v * 2 + 1];
}

long long calc(int v, int p) {
    if (check[v])
        return dp[v];

    for (int i : adj[v]) {
        if (i != p)
            subtree[v] += calc(i, v);
    }
    dp[v] = subtree[v];

    check[v] = true;
    return dp[v];
}

void solve(int v, int p = -1) {
    for (int i : adj[v]) {
        if (i != p) {
            solve(i, v);
            subtree[v] += dp[i];
            dp[v] += dp[i];
        }
    }

    for (auto i : q[v]) {
        long long sum = query_tree(1, 0, euler.size() - 1, /*first[v]*/0, first[i.a]) + query_tree(1, 0, euler.size() - 1, /*first[v]*/0, first[i.b]) + subtree[v] + i.c;
        dp[v] = max(dp[v], sum);
        // dp[v] = max(dp[v], subtree[v] + query_tree(1, 0, euler.size() - 1, first[v], first[i.a]) + i.c) + query_tree(1, 0, euler.size() - 1, first[v], first[i.b]);
    }

    update(1, 0, euler.size() - 1, first[v], subtree[v] - dp[v]);
    if (v != 0)
        update(1, 0, euler.size() - 1, last[v] + 1, dp[v] - subtree[v]);
}

long long solve(query u) {
    int lca = query_lca(u.a, u.b);
    if (!check[lca]) {
        calc(lca, parent[lca]);
    }

    long long sum = query_tree(1, 0, euler.size() - 1, first[lca], first[u.a]) + query_tree(1, 0, euler.size() - 1, first[lca], first[u.b]) + 2 * (dp[lca] - subtree[lca]) + subtree[lca] + u.c;
    if (sum > dp[lca]) {
        dp[lca] = sum;
        update(1, 0, euler.size() - 1, first[lca], subtree[lca] - dp[lca]);
        if (lca != 0)
            update(1, 0, euler.size() - 1, last[lca] + 1, dp[lca] - subtree[lca]);
    }

    assert(sum >= 0);
    return sum;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs(0);
    build_sparse();

    cin >> m;
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        q[query_lca(a, b)].push_back({a, b, c});
    }
    // for (int i = 0; i < m; i++) cout << query_lca(q[i].a, q[i].b) << endl;

    solve(0);
    cout << *max_element(dp, dp + n);
}

Compilation message

election_campaign.cpp: In function 'void build_sparse()':
election_campaign.cpp:47:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < euler.size(); i++)
                     ~~^~~~~~~~~~~~~~
election_campaign.cpp:51:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
                         ~~^~~~~~~~~~~~~~
election_campaign.cpp:51:62: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < euler.size() && j - 1 + (1 << i) < euler.size(); j++)
                                             ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 2 ms 5112 KB Output is correct
3 Correct 6 ms 4968 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 181 ms 32404 KB Output is correct
6 Correct 119 ms 40036 KB Output is correct
7 Correct 156 ms 37192 KB Output is correct
8 Correct 151 ms 31220 KB Output is correct
9 Correct 165 ms 35684 KB Output is correct
10 Correct 153 ms 30992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5368 KB Output is correct
4 Correct 222 ms 40500 KB Output is correct
5 Correct 226 ms 40776 KB Output is correct
6 Correct 222 ms 40520 KB Output is correct
7 Correct 220 ms 40544 KB Output is correct
8 Correct 220 ms 40676 KB Output is correct
9 Correct 269 ms 40648 KB Output is correct
10 Correct 217 ms 40548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5368 KB Output is correct
4 Correct 222 ms 40500 KB Output is correct
5 Correct 226 ms 40776 KB Output is correct
6 Correct 222 ms 40520 KB Output is correct
7 Correct 220 ms 40544 KB Output is correct
8 Correct 220 ms 40676 KB Output is correct
9 Correct 269 ms 40648 KB Output is correct
10 Correct 217 ms 40548 KB Output is correct
11 Correct 23 ms 5924 KB Output is correct
12 Correct 241 ms 40616 KB Output is correct
13 Correct 227 ms 40672 KB Output is correct
14 Correct 227 ms 40492 KB Output is correct
15 Correct 264 ms 40672 KB Output is correct
16 Correct 217 ms 40592 KB Output is correct
17 Correct 224 ms 40544 KB Output is correct
18 Correct 221 ms 40544 KB Output is correct
19 Correct 211 ms 40500 KB Output is correct
20 Correct 221 ms 40572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 32584 KB Output is correct
2 Correct 210 ms 40568 KB Output is correct
3 Correct 301 ms 37500 KB Output is correct
4 Correct 255 ms 33924 KB Output is correct
5 Correct 283 ms 39392 KB Output is correct
6 Correct 263 ms 34228 KB Output is correct
7 Correct 282 ms 39188 KB Output is correct
8 Correct 296 ms 35044 KB Output is correct
9 Correct 217 ms 42976 KB Output is correct
10 Correct 344 ms 38256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 2 ms 5112 KB Output is correct
3 Correct 6 ms 4968 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 181 ms 32404 KB Output is correct
6 Correct 119 ms 40036 KB Output is correct
7 Correct 156 ms 37192 KB Output is correct
8 Correct 151 ms 31220 KB Output is correct
9 Correct 165 ms 35684 KB Output is correct
10 Correct 153 ms 30992 KB Output is correct
11 Correct 12 ms 5368 KB Output is correct
12 Correct 8 ms 5496 KB Output is correct
13 Correct 8 ms 5368 KB Output is correct
14 Correct 8 ms 5368 KB Output is correct
15 Correct 8 ms 5352 KB Output is correct
16 Correct 8 ms 5496 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5368 KB Output is correct
19 Correct 8 ms 5368 KB Output is correct
20 Correct 7 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 2 ms 5112 KB Output is correct
3 Correct 6 ms 4968 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 181 ms 32404 KB Output is correct
6 Correct 119 ms 40036 KB Output is correct
7 Correct 156 ms 37192 KB Output is correct
8 Correct 151 ms 31220 KB Output is correct
9 Correct 165 ms 35684 KB Output is correct
10 Correct 153 ms 30992 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 7 ms 5368 KB Output is correct
14 Correct 222 ms 40500 KB Output is correct
15 Correct 226 ms 40776 KB Output is correct
16 Correct 222 ms 40520 KB Output is correct
17 Correct 220 ms 40544 KB Output is correct
18 Correct 220 ms 40676 KB Output is correct
19 Correct 269 ms 40648 KB Output is correct
20 Correct 217 ms 40548 KB Output is correct
21 Correct 23 ms 5924 KB Output is correct
22 Correct 241 ms 40616 KB Output is correct
23 Correct 227 ms 40672 KB Output is correct
24 Correct 227 ms 40492 KB Output is correct
25 Correct 264 ms 40672 KB Output is correct
26 Correct 217 ms 40592 KB Output is correct
27 Correct 224 ms 40544 KB Output is correct
28 Correct 221 ms 40544 KB Output is correct
29 Correct 211 ms 40500 KB Output is correct
30 Correct 221 ms 40572 KB Output is correct
31 Correct 287 ms 32584 KB Output is correct
32 Correct 210 ms 40568 KB Output is correct
33 Correct 301 ms 37500 KB Output is correct
34 Correct 255 ms 33924 KB Output is correct
35 Correct 283 ms 39392 KB Output is correct
36 Correct 263 ms 34228 KB Output is correct
37 Correct 282 ms 39188 KB Output is correct
38 Correct 296 ms 35044 KB Output is correct
39 Correct 217 ms 42976 KB Output is correct
40 Correct 344 ms 38256 KB Output is correct
41 Correct 12 ms 5368 KB Output is correct
42 Correct 8 ms 5496 KB Output is correct
43 Correct 8 ms 5368 KB Output is correct
44 Correct 8 ms 5368 KB Output is correct
45 Correct 8 ms 5352 KB Output is correct
46 Correct 8 ms 5496 KB Output is correct
47 Correct 8 ms 5368 KB Output is correct
48 Correct 8 ms 5368 KB Output is correct
49 Correct 8 ms 5368 KB Output is correct
50 Correct 7 ms 5368 KB Output is correct
51 Correct 319 ms 35400 KB Output is correct
52 Correct 239 ms 43268 KB Output is correct
53 Correct 407 ms 38624 KB Output is correct
54 Correct 313 ms 34224 KB Output is correct
55 Correct 291 ms 35308 KB Output is correct
56 Correct 301 ms 43372 KB Output is correct
57 Correct 337 ms 39308 KB Output is correct
58 Correct 297 ms 34148 KB Output is correct
59 Correct 400 ms 35424 KB Output is correct
60 Correct 230 ms 43232 KB Output is correct
61 Correct 283 ms 39392 KB Output is correct
62 Correct 258 ms 33936 KB Output is correct
63 Correct 282 ms 35556 KB Output is correct
64 Correct 222 ms 43196 KB Output is correct
65 Correct 287 ms 39524 KB Output is correct
66 Correct 258 ms 34276 KB Output is correct
67 Correct 320 ms 35636 KB Output is correct
68 Correct 233 ms 43380 KB Output is correct
69 Correct 405 ms 38232 KB Output is correct
70 Correct 261 ms 34404 KB Output is correct