Submission #554738

# Submission time Handle Problem Language Result Execution time Memory
554738 2022-04-29T10:29:19 Z Jarif_Rahman Election Campaign (JOI15_election_campaign) C++17
100 / 100
196 ms 32328 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int K = 19;

struct BIT{
    int n;
    vector<ll> sm;
    BIT(){}
    BIT(int _n){
        n = _n;
        sm.resize(n);
    }
    void add(int in, ll x){
        in++;
        while(in <= n) sm[in-1]+=x, in+=in&-in;
    }
    void add(int l, int r, ll x){
        add(l, x);
        if(r+1 < n) add(r+1, -x);
    }
    ll get(int in){
        in++;
        ll s = 0;
        while(in >= 1) s+=sm[in-1], in-=in&-in;
        return s;
    }
};

int n, m;
vector<vector<int>> v;
vector<int> anc[K];
vector<int> depth;
vector<int> pos;
vector<int> sz;
int pos_cnt = 0;

int get_anc(int nd, int h){
    for(int i = 0; i < K; i++) { if(h%2 == 1) nd = anc[i][nd]; h/=2; } return nd;
}
int lca_jump(int a, int b){
    if(a == b) return a;
    for(int i = K-1; i >= 0; i--) if(anc[i][a] != anc[i][b])
        return lca_jump(anc[i][a], anc[i][b]);
    return anc[0][a];
}
int lca(int a, int b){
    if(depth[a] > depth[b]) swap(a, b);
    b = get_anc(b, depth[b]-depth[a]);
    return lca_jump(a, b);
}

void pre_dfs(int nd, int ss, int d){
    pos[nd] = pos_cnt++;
    for(int x: v[nd]) if(x != ss) pre_dfs(x, nd, d+1);
    for(int x: v[nd]) if(x != ss) sz[nd]+=sz[x];
    if(ss != -1) anc[0][nd] = ss;
    depth[nd] = d;
}

vector<vector<tuple<int, int, ll>>> plans;
vector<ll> dp, childsum;
BIT bit;

void dfs(int nd, int ss){
    for(int x: v[nd]) if(x != ss) dfs(x, nd);
    for(int x: v[nd]) if(x != ss) childsum[nd]+=dp[x];
    dp[nd] = childsum[nd];

    for(auto [a, b, c]: plans[nd]){
        ll cur = c;
        cur+=bit.get(pos[a]);
        cur+=bit.get(pos[b]);
        cur-=2LL*bit.get(pos[nd]);
        cur+=childsum[nd];
        dp[nd] = max(dp[nd], cur);
    }

    bit.add(pos[nd], pos[nd]+sz[nd]-1, childsum[nd]-dp[nd]);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    v.assign(n, {});
    fill(anc, anc+K, vector<int>(n, 0));
    depth.assign(n, -1);
    pos.assign(n, -1);
    sz.assign(n, 1);
    plans.assign(n, {});
    dp.assign(n, 0);
    childsum.assign(n, 0);
    bit = BIT(n);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);
    }

    pre_dfs(0, -1, 0);
    for(int p = 1; p < K; p++) for(int i = 0; i < n; i++)
        anc[p][i] = anc[p-1][anc[p-1][i]];

    cin >> m;

    for(int i = 0; i < m; i++){
        int a, b; ll c; cin >> a >> b >> c; a--, b--;
        plans[lca(a, b)].pb({a, b, c});
    }

    dfs(0, -1);

    cout << dp[0] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 71 ms 19408 KB Output is correct
6 Correct 40 ms 27020 KB Output is correct
7 Correct 64 ms 24260 KB Output is correct
8 Correct 53 ms 19652 KB Output is correct
9 Correct 63 ms 22720 KB Output is correct
10 Correct 54 ms 19788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 584 KB Output is correct
4 Correct 91 ms 29632 KB Output is correct
5 Correct 86 ms 31888 KB Output is correct
6 Correct 83 ms 31940 KB Output is correct
7 Correct 94 ms 32036 KB Output is correct
8 Correct 87 ms 31944 KB Output is correct
9 Correct 86 ms 31904 KB Output is correct
10 Correct 89 ms 32004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 584 KB Output is correct
4 Correct 91 ms 29632 KB Output is correct
5 Correct 86 ms 31888 KB Output is correct
6 Correct 83 ms 31940 KB Output is correct
7 Correct 94 ms 32036 KB Output is correct
8 Correct 87 ms 31944 KB Output is correct
9 Correct 86 ms 31904 KB Output is correct
10 Correct 89 ms 32004 KB Output is correct
11 Correct 9 ms 1480 KB Output is correct
12 Correct 97 ms 32244 KB Output is correct
13 Correct 112 ms 32312 KB Output is correct
14 Correct 92 ms 32272 KB Output is correct
15 Correct 93 ms 32256 KB Output is correct
16 Correct 91 ms 32308 KB Output is correct
17 Correct 95 ms 32252 KB Output is correct
18 Correct 97 ms 32148 KB Output is correct
19 Correct 91 ms 32312 KB Output is correct
20 Correct 127 ms 32168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 21312 KB Output is correct
2 Correct 97 ms 29560 KB Output is correct
3 Correct 165 ms 28972 KB Output is correct
4 Correct 98 ms 24220 KB Output is correct
5 Correct 135 ms 28376 KB Output is correct
6 Correct 95 ms 24532 KB Output is correct
7 Correct 169 ms 28088 KB Output is correct
8 Correct 136 ms 23912 KB Output is correct
9 Correct 82 ms 31924 KB Output is correct
10 Correct 179 ms 26944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 71 ms 19408 KB Output is correct
6 Correct 40 ms 27020 KB Output is correct
7 Correct 64 ms 24260 KB Output is correct
8 Correct 53 ms 19652 KB Output is correct
9 Correct 63 ms 22720 KB Output is correct
10 Correct 54 ms 19788 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 548 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 71 ms 19408 KB Output is correct
6 Correct 40 ms 27020 KB Output is correct
7 Correct 64 ms 24260 KB Output is correct
8 Correct 53 ms 19652 KB Output is correct
9 Correct 63 ms 22720 KB Output is correct
10 Correct 54 ms 19788 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 584 KB Output is correct
14 Correct 91 ms 29632 KB Output is correct
15 Correct 86 ms 31888 KB Output is correct
16 Correct 83 ms 31940 KB Output is correct
17 Correct 94 ms 32036 KB Output is correct
18 Correct 87 ms 31944 KB Output is correct
19 Correct 86 ms 31904 KB Output is correct
20 Correct 89 ms 32004 KB Output is correct
21 Correct 9 ms 1480 KB Output is correct
22 Correct 97 ms 32244 KB Output is correct
23 Correct 112 ms 32312 KB Output is correct
24 Correct 92 ms 32272 KB Output is correct
25 Correct 93 ms 32256 KB Output is correct
26 Correct 91 ms 32308 KB Output is correct
27 Correct 95 ms 32252 KB Output is correct
28 Correct 97 ms 32148 KB Output is correct
29 Correct 91 ms 32312 KB Output is correct
30 Correct 127 ms 32168 KB Output is correct
31 Correct 158 ms 21312 KB Output is correct
32 Correct 97 ms 29560 KB Output is correct
33 Correct 165 ms 28972 KB Output is correct
34 Correct 98 ms 24220 KB Output is correct
35 Correct 135 ms 28376 KB Output is correct
36 Correct 95 ms 24532 KB Output is correct
37 Correct 169 ms 28088 KB Output is correct
38 Correct 136 ms 23912 KB Output is correct
39 Correct 82 ms 31924 KB Output is correct
40 Correct 179 ms 26944 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 596 KB Output is correct
43 Correct 1 ms 588 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 548 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 596 KB Output is correct
49 Correct 1 ms 460 KB Output is correct
50 Correct 1 ms 596 KB Output is correct
51 Correct 146 ms 24240 KB Output is correct
52 Correct 92 ms 32196 KB Output is correct
53 Correct 182 ms 27368 KB Output is correct
54 Correct 94 ms 24448 KB Output is correct
55 Correct 148 ms 23888 KB Output is correct
56 Correct 98 ms 32196 KB Output is correct
57 Correct 147 ms 28176 KB Output is correct
58 Correct 109 ms 24308 KB Output is correct
59 Correct 144 ms 24388 KB Output is correct
60 Correct 95 ms 32168 KB Output is correct
61 Correct 141 ms 28260 KB Output is correct
62 Correct 98 ms 24492 KB Output is correct
63 Correct 149 ms 23976 KB Output is correct
64 Correct 99 ms 32328 KB Output is correct
65 Correct 196 ms 28444 KB Output is correct
66 Correct 94 ms 24516 KB Output is correct
67 Correct 146 ms 24564 KB Output is correct
68 Correct 88 ms 32260 KB Output is correct
69 Correct 145 ms 26968 KB Output is correct
70 Correct 98 ms 24680 KB Output is correct