Submission #964218

# Submission time Handle Problem Language Result Execution time Memory
964218 2024-04-16T12:29:10 Z vjudge1 Election Campaign (JOI15_election_campaign) C++17
100 / 100
219 ms 44916 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)

constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void setupIO(){
    #define name "Whisper"
    //Phu Trong from Nguyen Tat Thanh High School for gifted student
    srand(time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    cout << fixed << setprecision(10);
}

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
const int MAX = 1e5 + 5;
int numNode, numQuery;

struct Edge{
    int u, v;
    Edge(){}
    Edge(int _u, int _v): u(_u), v(_v){}

    int other(int x){ return (x ^ u ^ v); }
} edge[MAX];

vector<int> G[MAX];


int tin[MAX], tout[MAX], cnt = 0;
int up[MAX][21], depth[MAX], par[MAX];

void pre_dfs(int u, int p = -1){
    tin[u] = ++cnt;
    for (int &i : G[u]){
        int v = edge[i].other(u);
        if(v ^ p){
            depth[v] = depth[u] + 1;
            par[v] = u;
            up[v][0] = u;
            for (int i = 1; i <= 20; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
            pre_dfs(v, u);

        }
    }
    tout[u] = cnt;
}

int lca(int u, int v){
    if (depth[u] < depth[v]) swap(u, v);
    int dis = depth[u] - depth[v];
    for (int i = 0; MASK(i) <= dis; ++i) if(BIT(dis, i))
        u = up[u][i];
    if (u == v) return u;
    int k = 31 - __builtin_clz(depth[v]);
    for (int i = k; i >= 0; --i) if(up[u][i] != up[v][i]){
        u = up[u][i], v = up[v][i];
    }
    return up[u][0];
}

vector<tuple<int, int, int>> q[MAX];
int ndp[MAX], dp[MAX];


struct FenwickTree{
    int n;
    vector<int> f;
    FenwickTree(int _n = 0){
        this -> n = _n;
        f.resize(n + 5);
    }
    void modify(int p, int val){
        for (; p <= n; p += p & (-p)) f[p] += val;
    }
    int query(int p){
        int res = 0;
        for (; p > 0; p -= p & (-p)) res += f[p];
        return res;
    }
    void modify(int l, int r, int val){
        modify(l, val); modify(r + 1, -val);
    }
} fen;

void dfs(int u, int p = -1){
    for (int &i : G[u]){
        int v = edge[i].other(u);
        if(v ^ p){
            dfs(v, u);
            ndp[u] += dp[v];
        }
    }
    dp[u] = ndp[u]; // not choose any segment

    for (tuple<int, int, int>&x : q[u]){
        int nodeu, nodev, c; tie(nodeu, nodev, c) = x;
        maximize(dp[u], fen.query(tin[nodeu]) + fen.query(tin[nodev]) + c + ndp[u]);
    }

    fen.modify(tin[u], tout[u], ndp[u] - dp[u]);
}
void Whisper(){
    cin >> numNode;

    for (int i = 1; i < numNode; ++i){
        cin >> edge[i].u >> edge[i].v;
        G[edge[i].u].emplace_back(i);
        G[edge[i].v].emplace_back(i);
    }
    //dp on tree

    pre_dfs(1);
    cin >> numQuery;

    for (int i = 1; i <= numQuery; ++i){
        int u, v, c; cin >> u >> v >> c;
        int p = lca(u, v);
        q[p].emplace_back(u, v, c);
    }

    fen = FenwickTree (numNode);
    dfs(1);
    cout << dp[1];
}


signed main(){
    setupIO();
    int Test = 1;
//    cin >> Test;
    for ( int i = 1 ; i <= Test ; i++ ){
        Whisper();
        if (i < Test) cout << '\n';
    }
}


# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8936 KB Output is correct
5 Correct 105 ms 33140 KB Output is correct
6 Correct 46 ms 40168 KB Output is correct
7 Correct 105 ms 38040 KB Output is correct
8 Correct 60 ms 33308 KB Output is correct
9 Correct 91 ms 36292 KB Output is correct
10 Correct 62 ms 33352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8632 KB Output is correct
2 Correct 3 ms 8536 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 90 ms 44616 KB Output is correct
5 Correct 98 ms 44752 KB Output is correct
6 Correct 78 ms 44628 KB Output is correct
7 Correct 87 ms 44560 KB Output is correct
8 Correct 98 ms 44588 KB Output is correct
9 Correct 81 ms 44512 KB Output is correct
10 Correct 104 ms 44916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8632 KB Output is correct
2 Correct 3 ms 8536 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 90 ms 44616 KB Output is correct
5 Correct 98 ms 44752 KB Output is correct
6 Correct 78 ms 44628 KB Output is correct
7 Correct 87 ms 44560 KB Output is correct
8 Correct 98 ms 44588 KB Output is correct
9 Correct 81 ms 44512 KB Output is correct
10 Correct 104 ms 44916 KB Output is correct
11 Correct 9 ms 10272 KB Output is correct
12 Correct 108 ms 44556 KB Output is correct
13 Correct 92 ms 44476 KB Output is correct
14 Correct 89 ms 44600 KB Output is correct
15 Correct 121 ms 44572 KB Output is correct
16 Correct 99 ms 44572 KB Output is correct
17 Correct 99 ms 44564 KB Output is correct
18 Correct 92 ms 44600 KB Output is correct
19 Correct 99 ms 44792 KB Output is correct
20 Correct 107 ms 44464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 36868 KB Output is correct
2 Correct 81 ms 44624 KB Output is correct
3 Correct 173 ms 41804 KB Output is correct
4 Correct 85 ms 37380 KB Output is correct
5 Correct 177 ms 41108 KB Output is correct
6 Correct 96 ms 37196 KB Output is correct
7 Correct 160 ms 40988 KB Output is correct
8 Correct 154 ms 37000 KB Output is correct
9 Correct 90 ms 44524 KB Output is correct
10 Correct 173 ms 39952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8936 KB Output is correct
5 Correct 105 ms 33140 KB Output is correct
6 Correct 46 ms 40168 KB Output is correct
7 Correct 105 ms 38040 KB Output is correct
8 Correct 60 ms 33308 KB Output is correct
9 Correct 91 ms 36292 KB Output is correct
10 Correct 62 ms 33352 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8948 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 3 ms 8796 KB Output is correct
15 Correct 2 ms 8824 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 2 ms 8948 KB Output is correct
19 Correct 4 ms 8952 KB Output is correct
20 Correct 2 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8936 KB Output is correct
5 Correct 105 ms 33140 KB Output is correct
6 Correct 46 ms 40168 KB Output is correct
7 Correct 105 ms 38040 KB Output is correct
8 Correct 60 ms 33308 KB Output is correct
9 Correct 91 ms 36292 KB Output is correct
10 Correct 62 ms 33352 KB Output is correct
11 Correct 2 ms 8632 KB Output is correct
12 Correct 3 ms 8536 KB Output is correct
13 Correct 2 ms 8792 KB Output is correct
14 Correct 90 ms 44616 KB Output is correct
15 Correct 98 ms 44752 KB Output is correct
16 Correct 78 ms 44628 KB Output is correct
17 Correct 87 ms 44560 KB Output is correct
18 Correct 98 ms 44588 KB Output is correct
19 Correct 81 ms 44512 KB Output is correct
20 Correct 104 ms 44916 KB Output is correct
21 Correct 9 ms 10272 KB Output is correct
22 Correct 108 ms 44556 KB Output is correct
23 Correct 92 ms 44476 KB Output is correct
24 Correct 89 ms 44600 KB Output is correct
25 Correct 121 ms 44572 KB Output is correct
26 Correct 99 ms 44572 KB Output is correct
27 Correct 99 ms 44564 KB Output is correct
28 Correct 92 ms 44600 KB Output is correct
29 Correct 99 ms 44792 KB Output is correct
30 Correct 107 ms 44464 KB Output is correct
31 Correct 158 ms 36868 KB Output is correct
32 Correct 81 ms 44624 KB Output is correct
33 Correct 173 ms 41804 KB Output is correct
34 Correct 85 ms 37380 KB Output is correct
35 Correct 177 ms 41108 KB Output is correct
36 Correct 96 ms 37196 KB Output is correct
37 Correct 160 ms 40988 KB Output is correct
38 Correct 154 ms 37000 KB Output is correct
39 Correct 90 ms 44524 KB Output is correct
40 Correct 173 ms 39952 KB Output is correct
41 Correct 3 ms 8796 KB Output is correct
42 Correct 3 ms 8948 KB Output is correct
43 Correct 2 ms 8796 KB Output is correct
44 Correct 3 ms 8796 KB Output is correct
45 Correct 2 ms 8824 KB Output is correct
46 Correct 2 ms 8796 KB Output is correct
47 Correct 3 ms 8796 KB Output is correct
48 Correct 2 ms 8948 KB Output is correct
49 Correct 4 ms 8952 KB Output is correct
50 Correct 2 ms 9048 KB Output is correct
51 Correct 134 ms 36996 KB Output is correct
52 Correct 94 ms 44628 KB Output is correct
53 Correct 218 ms 40096 KB Output is correct
54 Correct 103 ms 37068 KB Output is correct
55 Correct 131 ms 36872 KB Output is correct
56 Correct 97 ms 44428 KB Output is correct
57 Correct 158 ms 40628 KB Output is correct
58 Correct 105 ms 37128 KB Output is correct
59 Correct 146 ms 36980 KB Output is correct
60 Correct 109 ms 44604 KB Output is correct
61 Correct 157 ms 40916 KB Output is correct
62 Correct 107 ms 36680 KB Output is correct
63 Correct 143 ms 36488 KB Output is correct
64 Correct 112 ms 44732 KB Output is correct
65 Correct 219 ms 40708 KB Output is correct
66 Correct 93 ms 37276 KB Output is correct
67 Correct 136 ms 36900 KB Output is correct
68 Correct 89 ms 44568 KB Output is correct
69 Correct 162 ms 39476 KB Output is correct
70 Correct 87 ms 37112 KB Output is correct