Submission #890617

# Submission time Handle Problem Language Result Execution time Memory
890617 2023-12-21T16:04:26 Z iskhakkutbilim Election Campaign (JOI15_election_campaign) C++17
100 / 100
221 ms 51764 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int N = 1e5 + 1;

struct Fenw{
    vector <int> fenw;
    int n;
    Fenw(int n = 0) : fenw(n + 1, 0), n(n) {}
    void upd(int i, int val){
        for (; i <= n; i += i & -i ){
            fenw[i] += val;
        }
    }
    int get(int i){
        int cnt = 0;
        for (; i > 0; i -= i & -i ){
            cnt += fenw[i];
        }
        return cnt;
    }
    int get(int l, int r){
        return get(r) - get(l - 1);
    }
} Sub, Dp;

vector <int> G[N];

int up[N][21], d[N], h[N], sub[N];

void dfs(int u, int p){
    up[u][0] = p;
    for ( int i = 1; i <= 20; i++ ){
        up[u][i] = up[up[u][i - 1]][i - 1];
    }
    sub[u] = 1;
    for ( auto &v: G[u] ){
        if ( v != p ){
            d[v] = d[u] + 1;
            dfs(v, u);
            sub[u] += sub[v];
        }
    }
    for ( auto &v: G[u] ){
        if ( v != p ){
            if ( h[u] == -1 or sub[h[u]] < sub[v] ){
                h[u] = v;
            }
        }
    }
}

int lca(int u, int v){
    if ( d[u] < d[v] ) swap(u, v);
    int dif = d[u] - d[v];
    for ( int i = 0; i <= 20; i++ ){
        if ( dif >> i & 1 ){
            u = up[u][i];
        }
    }
    if ( u == v ){
        return u;
    }
    for ( int i = 20; i >= 0; i-- ){
        if ( up[u][i] != up[v][i] ){
            u = up[u][i];
            v = up[v][i];
        }
    }
    return up[u][0];
}

int chain[N], top[N], id[N], ct = 0, rt = 1;

void hld(int u, int p){
    id[u] = ++ct;
    chain[u] = rt;
    if ( top[rt] == -1 ){
        top[rt] = u;
    }
    if ( h[u] != -1 ) hld(h[u], u);
    for ( auto &v: G[u] ){
        if ( v != p and h[u] != v ){
            ++rt; hld(v, u);
        }
    }
}

int query(int u, int v){
    if ( d[u] < d[v] ){
        return 0;
    }
    // v must be Parent of u
    int ans = 0;
    while ( chain[u] != chain[v] ){
        int nxt = top[chain[u]];
        ans += Sub.get(id[nxt], id[u]) - Dp.get(id[nxt], id[u]);
        u = up[nxt][0];
    }
    ans += Sub.get(id[v], id[u]) - Dp.get(id[v], id[u]);
    return ans;
}

int sb[N], dp[N];

vector <int> qu[N], A(N), B(N), C(N);

void dfs2(int u, int p){
    int q = 0;
    for ( auto &v: G[u] ){
        if ( v != p ){
            dfs2(v, u);
            q += dp[v];
        }
    }
    sb[u] = q;
    for ( auto &i: qu[u] ){
        int cnt = query(A[i], u) + query(B[i], u) + C[i] + sb[u];
        chmax(dp[u], cnt);
    }
    chmax(dp[u], sb[u]);
    Sub.upd(id[u], sb[u]);
    Dp.upd(id[u], dp[u]);
}

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

    memset(h, -1, sizeof(h));
    memset(top, -1, sizeof(top));
    dp[0] = -1;

    int n; cin >> n;
    Sub = Dp = Fenw(n);
    for ( int i = 1; i < n; i++ ){
        int u, v; cin >> u >> v;
        G[u].pb(v), G[v].pb(u);
    }
    dfs(1, 0); hld(1, 0);
    int m; cin >> m;
    for ( int i = 0; i < m; i++ ){
        cin >> A[i] >> B[i] >> C[i];
        qu[lca(A[i], B[i])].pb(i);
    }
    dfs2(1, 0);
    cout << dp[1];

    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 73 ms 35708 KB Output is correct
6 Correct 45 ms 47428 KB Output is correct
7 Correct 93 ms 43344 KB Output is correct
8 Correct 55 ms 35664 KB Output is correct
9 Correct 81 ms 41036 KB Output is correct
10 Correct 49 ms 35648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 4 ms 15708 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 106 ms 48912 KB Output is correct
5 Correct 109 ms 48792 KB Output is correct
6 Correct 96 ms 48876 KB Output is correct
7 Correct 103 ms 48884 KB Output is correct
8 Correct 135 ms 48976 KB Output is correct
9 Correct 89 ms 48976 KB Output is correct
10 Correct 98 ms 48940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15708 KB Output is correct
2 Correct 4 ms 15708 KB Output is correct
3 Correct 3 ms 15964 KB Output is correct
4 Correct 106 ms 48912 KB Output is correct
5 Correct 109 ms 48792 KB Output is correct
6 Correct 96 ms 48876 KB Output is correct
7 Correct 103 ms 48884 KB Output is correct
8 Correct 135 ms 48976 KB Output is correct
9 Correct 89 ms 48976 KB Output is correct
10 Correct 98 ms 48940 KB Output is correct
11 Correct 10 ms 16216 KB Output is correct
12 Correct 104 ms 48980 KB Output is correct
13 Correct 98 ms 48980 KB Output is correct
14 Correct 94 ms 48980 KB Output is correct
15 Correct 103 ms 48924 KB Output is correct
16 Correct 92 ms 48844 KB Output is correct
17 Correct 100 ms 48980 KB Output is correct
18 Correct 98 ms 48980 KB Output is correct
19 Correct 93 ms 48832 KB Output is correct
20 Correct 101 ms 48976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 36688 KB Output is correct
2 Correct 99 ms 48980 KB Output is correct
3 Correct 193 ms 44588 KB Output is correct
4 Correct 109 ms 39360 KB Output is correct
5 Correct 159 ms 46128 KB Output is correct
6 Correct 114 ms 39352 KB Output is correct
7 Correct 177 ms 45828 KB Output is correct
8 Correct 171 ms 39416 KB Output is correct
9 Correct 99 ms 51516 KB Output is correct
10 Correct 176 ms 44488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 73 ms 35708 KB Output is correct
6 Correct 45 ms 47428 KB Output is correct
7 Correct 93 ms 43344 KB Output is correct
8 Correct 55 ms 35664 KB Output is correct
9 Correct 81 ms 41036 KB Output is correct
10 Correct 49 ms 35648 KB Output is correct
11 Correct 4 ms 15708 KB Output is correct
12 Correct 4 ms 15964 KB Output is correct
13 Correct 4 ms 15964 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 4 ms 15708 KB Output is correct
16 Correct 4 ms 15964 KB Output is correct
17 Correct 4 ms 15708 KB Output is correct
18 Correct 4 ms 15964 KB Output is correct
19 Correct 4 ms 15708 KB Output is correct
20 Correct 4 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 73 ms 35708 KB Output is correct
6 Correct 45 ms 47428 KB Output is correct
7 Correct 93 ms 43344 KB Output is correct
8 Correct 55 ms 35664 KB Output is correct
9 Correct 81 ms 41036 KB Output is correct
10 Correct 49 ms 35648 KB Output is correct
11 Correct 3 ms 15708 KB Output is correct
12 Correct 4 ms 15708 KB Output is correct
13 Correct 3 ms 15964 KB Output is correct
14 Correct 106 ms 48912 KB Output is correct
15 Correct 109 ms 48792 KB Output is correct
16 Correct 96 ms 48876 KB Output is correct
17 Correct 103 ms 48884 KB Output is correct
18 Correct 135 ms 48976 KB Output is correct
19 Correct 89 ms 48976 KB Output is correct
20 Correct 98 ms 48940 KB Output is correct
21 Correct 10 ms 16216 KB Output is correct
22 Correct 104 ms 48980 KB Output is correct
23 Correct 98 ms 48980 KB Output is correct
24 Correct 94 ms 48980 KB Output is correct
25 Correct 103 ms 48924 KB Output is correct
26 Correct 92 ms 48844 KB Output is correct
27 Correct 100 ms 48980 KB Output is correct
28 Correct 98 ms 48980 KB Output is correct
29 Correct 93 ms 48832 KB Output is correct
30 Correct 101 ms 48976 KB Output is correct
31 Correct 189 ms 36688 KB Output is correct
32 Correct 99 ms 48980 KB Output is correct
33 Correct 193 ms 44588 KB Output is correct
34 Correct 109 ms 39360 KB Output is correct
35 Correct 159 ms 46128 KB Output is correct
36 Correct 114 ms 39352 KB Output is correct
37 Correct 177 ms 45828 KB Output is correct
38 Correct 171 ms 39416 KB Output is correct
39 Correct 99 ms 51516 KB Output is correct
40 Correct 176 ms 44488 KB Output is correct
41 Correct 4 ms 15708 KB Output is correct
42 Correct 4 ms 15964 KB Output is correct
43 Correct 4 ms 15964 KB Output is correct
44 Correct 4 ms 15704 KB Output is correct
45 Correct 4 ms 15708 KB Output is correct
46 Correct 4 ms 15964 KB Output is correct
47 Correct 4 ms 15708 KB Output is correct
48 Correct 4 ms 15964 KB Output is correct
49 Correct 4 ms 15708 KB Output is correct
50 Correct 4 ms 15964 KB Output is correct
51 Correct 184 ms 39932 KB Output is correct
52 Correct 104 ms 51536 KB Output is correct
53 Correct 168 ms 44640 KB Output is correct
54 Correct 105 ms 39424 KB Output is correct
55 Correct 221 ms 39472 KB Output is correct
56 Correct 102 ms 51764 KB Output is correct
57 Correct 147 ms 45648 KB Output is correct
58 Correct 117 ms 39376 KB Output is correct
59 Correct 150 ms 39508 KB Output is correct
60 Correct 98 ms 51556 KB Output is correct
61 Correct 173 ms 45820 KB Output is correct
62 Correct 135 ms 39516 KB Output is correct
63 Correct 168 ms 39356 KB Output is correct
64 Correct 103 ms 51540 KB Output is correct
65 Correct 175 ms 45824 KB Output is correct
66 Correct 126 ms 39664 KB Output is correct
67 Correct 170 ms 39500 KB Output is correct
68 Correct 103 ms 51584 KB Output is correct
69 Correct 177 ms 43824 KB Output is correct
70 Correct 111 ms 39688 KB Output is correct