답안 #109680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109680 2019-05-07T12:14:41 Z popovicirobert Election Campaign (JOI15_election_campaign) C++14
100 / 100
402 ms 35408 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

struct Fenwick {
    vector <ll> aib;
    int n;

    inline void init(int _n) {
        n = _n;
        aib.resize(n + 1);
    }

    inline void update(int pos, int val) {
        for(int i = pos; i <= n; i += lsb(i)) {
            aib[i] += val;
        }
    }

    inline ll query(int pos) {
        ll ans = 0;
        for(int i = pos; i > 0; i -= lsb(i)) {
            ans += aib[i];
        }
        return ans;
    }

    inline ll sum(int l, int r) {
        return query(r) - query(l);
    }
};

const int MAXN = (int) 1e5;
const int LOG = 17;

vector <int> g[MAXN + 1];
int anc[MAXN + 1][LOG + 1], lvl[MAXN + 1];
int first[MAXN + 1], last[MAXN + 1], sz;


void dfs(int nod, int par) {

    first[nod] = ++sz;
    anc[nod][0] = par;
    for(int bit = 1; bit <= LOG; bit++) {
        anc[nod][bit] = anc[anc[nod][bit - 1]][bit - 1];
    }

    lvl[nod] = lvl[par] + 1;
    for(auto it : g[nod]) {
        if(it != par) {
            dfs(it, nod);
        }
    }

    last[nod] = ++sz;
}

inline int get_lca(int x, int y) {
    if(lvl[x] > lvl[y]) {
        swap(x, y);
    }

    for(int bit = 0; bit <= LOG; bit++) {
        if((lvl[y] - lvl[x]) & (1 << bit)) {
            y = anc[y][bit];
        }
    }

    if(x == y) {
        return x;
    }

    for(int bit = LOG; bit >= 0; bit--) {
        if(anc[x][bit] != anc[y][bit]) {
            x = anc[x][bit];
            y = anc[y][bit];
        }
    }

    return anc[x][0];
}

inline int get_anc(int nod, int len) {
    for(int bit = 0; bit <= LOG; bit++) {
        if(len & (1 << bit)) {
            nod = anc[nod][bit];
        }
    }

    return nod;
}

inline ll get_sum(int x, int y, Fenwick &fen) {
    x = first[x], y = first[y];

    if(x > y) {
        swap(x, y);
    }

    return fen.sum(x, y);

}

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

vector <Query> qry[MAXN + 1];

Fenwick fen_dp, fen_sum;
ll dp[MAXN + 1], sum[MAXN + 1];


void solve(int nod, int par) {

    for(auto it : g[nod]) {
        if(it != par) {
            solve(it, nod);
            sum[nod] += dp[it];
        }
    }

    fen_sum.update(first[nod], sum[nod]);
    fen_sum.update(last[nod], -sum[nod]);

    dp[nod] = sum[nod];

    for(auto it : qry[nod]) {
        int a = it.a, b = it.b, c = it.c;
        ll cur = get_sum(a, nod, fen_sum) + get_sum(b, nod, fen_sum) - get_sum(a, nod, fen_dp) - get_sum(b, nod, fen_dp);
        dp[nod] = max(dp[nod], cur + sum[nod] + c);
    }

    fen_dp.update(first[nod], dp[nod]);
    fen_dp.update(last[nod], -dp[nod]);

}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    for(i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    dfs(1, 0);

    cin >> m;

    for(i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        qry[get_lca(a, b)].push_back({a, b, c});
    }

    fen_dp.init(2 * n), fen_sum.init(2 * n);
    solve(1, 0);

    cout << dp[1];

    //cin.close();
    //cout.close();
    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 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 172 ms 22392 KB Output is correct
6 Correct 87 ms 31864 KB Output is correct
7 Correct 163 ms 28344 KB Output is correct
8 Correct 185 ms 22008 KB Output is correct
9 Correct 163 ms 26488 KB Output is correct
10 Correct 126 ms 22008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 158 ms 34920 KB Output is correct
5 Correct 194 ms 34984 KB Output is correct
6 Correct 165 ms 34936 KB Output is correct
7 Correct 219 ms 34956 KB Output is correct
8 Correct 215 ms 35020 KB Output is correct
9 Correct 175 ms 34936 KB Output is correct
10 Correct 205 ms 35044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 5 ms 4992 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 158 ms 34920 KB Output is correct
5 Correct 194 ms 34984 KB Output is correct
6 Correct 165 ms 34936 KB Output is correct
7 Correct 219 ms 34956 KB Output is correct
8 Correct 215 ms 35020 KB Output is correct
9 Correct 175 ms 34936 KB Output is correct
10 Correct 205 ms 35044 KB Output is correct
11 Correct 21 ms 6136 KB Output is correct
12 Correct 213 ms 35208 KB Output is correct
13 Correct 230 ms 35056 KB Output is correct
14 Correct 151 ms 35184 KB Output is correct
15 Correct 171 ms 35292 KB Output is correct
16 Correct 175 ms 35216 KB Output is correct
17 Correct 164 ms 35192 KB Output is correct
18 Correct 266 ms 35408 KB Output is correct
19 Correct 205 ms 35192 KB Output is correct
20 Correct 318 ms 35276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 284 ms 25128 KB Output is correct
2 Correct 232 ms 35024 KB Output is correct
3 Correct 361 ms 31508 KB Output is correct
4 Correct 182 ms 24880 KB Output is correct
5 Correct 293 ms 30764 KB Output is correct
6 Correct 180 ms 24812 KB Output is correct
7 Correct 324 ms 30384 KB Output is correct
8 Correct 318 ms 25620 KB Output is correct
9 Correct 191 ms 35036 KB Output is correct
10 Correct 390 ms 29296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 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 172 ms 22392 KB Output is correct
6 Correct 87 ms 31864 KB Output is correct
7 Correct 163 ms 28344 KB Output is correct
8 Correct 185 ms 22008 KB Output is correct
9 Correct 163 ms 26488 KB Output is correct
10 Correct 126 ms 22008 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5376 KB Output is correct
13 Correct 10 ms 5280 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 10 ms 5248 KB Output is correct
17 Correct 7 ms 5308 KB Output is correct
18 Correct 8 ms 5376 KB Output is correct
19 Correct 9 ms 5248 KB Output is correct
20 Correct 8 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 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 172 ms 22392 KB Output is correct
6 Correct 87 ms 31864 KB Output is correct
7 Correct 163 ms 28344 KB Output is correct
8 Correct 185 ms 22008 KB Output is correct
9 Correct 163 ms 26488 KB Output is correct
10 Correct 126 ms 22008 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 5 ms 4992 KB Output is correct
13 Correct 6 ms 5376 KB Output is correct
14 Correct 158 ms 34920 KB Output is correct
15 Correct 194 ms 34984 KB Output is correct
16 Correct 165 ms 34936 KB Output is correct
17 Correct 219 ms 34956 KB Output is correct
18 Correct 215 ms 35020 KB Output is correct
19 Correct 175 ms 34936 KB Output is correct
20 Correct 205 ms 35044 KB Output is correct
21 Correct 21 ms 6136 KB Output is correct
22 Correct 213 ms 35208 KB Output is correct
23 Correct 230 ms 35056 KB Output is correct
24 Correct 151 ms 35184 KB Output is correct
25 Correct 171 ms 35292 KB Output is correct
26 Correct 175 ms 35216 KB Output is correct
27 Correct 164 ms 35192 KB Output is correct
28 Correct 266 ms 35408 KB Output is correct
29 Correct 205 ms 35192 KB Output is correct
30 Correct 318 ms 35276 KB Output is correct
31 Correct 284 ms 25128 KB Output is correct
32 Correct 232 ms 35024 KB Output is correct
33 Correct 361 ms 31508 KB Output is correct
34 Correct 182 ms 24880 KB Output is correct
35 Correct 293 ms 30764 KB Output is correct
36 Correct 180 ms 24812 KB Output is correct
37 Correct 324 ms 30384 KB Output is correct
38 Correct 318 ms 25620 KB Output is correct
39 Correct 191 ms 35036 KB Output is correct
40 Correct 390 ms 29296 KB Output is correct
41 Correct 8 ms 5248 KB Output is correct
42 Correct 8 ms 5376 KB Output is correct
43 Correct 10 ms 5280 KB Output is correct
44 Correct 8 ms 5248 KB Output is correct
45 Correct 8 ms 5248 KB Output is correct
46 Correct 10 ms 5248 KB Output is correct
47 Correct 7 ms 5308 KB Output is correct
48 Correct 8 ms 5376 KB Output is correct
49 Correct 9 ms 5248 KB Output is correct
50 Correct 8 ms 5376 KB Output is correct
51 Correct 288 ms 25736 KB Output is correct
52 Correct 225 ms 35316 KB Output is correct
53 Correct 357 ms 29716 KB Output is correct
54 Correct 176 ms 25396 KB Output is correct
55 Correct 250 ms 25744 KB Output is correct
56 Correct 268 ms 35192 KB Output is correct
57 Correct 296 ms 30532 KB Output is correct
58 Correct 182 ms 25136 KB Output is correct
59 Correct 275 ms 25848 KB Output is correct
60 Correct 179 ms 35164 KB Output is correct
61 Correct 339 ms 30624 KB Output is correct
62 Correct 269 ms 25176 KB Output is correct
63 Correct 265 ms 25380 KB Output is correct
64 Correct 178 ms 35320 KB Output is correct
65 Correct 402 ms 30384 KB Output is correct
66 Correct 228 ms 25396 KB Output is correct
67 Correct 301 ms 25516 KB Output is correct
68 Correct 218 ms 35236 KB Output is correct
69 Correct 324 ms 29048 KB Output is correct
70 Correct 223 ms 25388 KB Output is correct