Submission #964217

# Submission time Handle Problem Language Result Execution time Memory
964217 2024-04-16T12:28:07 Z Whisper Election Campaign (JOI15_election_campaign) C++17
100 / 100
220 ms 45896 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 4 ms 8796 KB Output is correct
3 Correct 4 ms 8540 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 101 ms 33484 KB Output is correct
6 Correct 44 ms 40536 KB Output is correct
7 Correct 91 ms 38228 KB Output is correct
8 Correct 58 ms 33664 KB Output is correct
9 Correct 92 ms 36772 KB Output is correct
10 Correct 59 ms 33648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 95 ms 45444 KB Output is correct
5 Correct 89 ms 45428 KB Output is correct
6 Correct 80 ms 45468 KB Output is correct
7 Correct 85 ms 45376 KB Output is correct
8 Correct 117 ms 45596 KB Output is correct
9 Correct 79 ms 45552 KB Output is correct
10 Correct 95 ms 45392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 95 ms 45444 KB Output is correct
5 Correct 89 ms 45428 KB Output is correct
6 Correct 80 ms 45468 KB Output is correct
7 Correct 85 ms 45376 KB Output is correct
8 Correct 117 ms 45596 KB Output is correct
9 Correct 79 ms 45552 KB Output is correct
10 Correct 95 ms 45392 KB Output is correct
11 Correct 12 ms 10072 KB Output is correct
12 Correct 98 ms 45652 KB Output is correct
13 Correct 92 ms 45648 KB Output is correct
14 Correct 81 ms 45832 KB Output is correct
15 Correct 89 ms 45648 KB Output is correct
16 Correct 84 ms 45888 KB Output is correct
17 Correct 98 ms 45832 KB Output is correct
18 Correct 91 ms 45652 KB Output is correct
19 Correct 82 ms 45796 KB Output is correct
20 Correct 108 ms 45896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 37932 KB Output is correct
2 Correct 76 ms 45396 KB Output is correct
3 Correct 183 ms 42636 KB Output is correct
4 Correct 116 ms 38168 KB Output is correct
5 Correct 150 ms 42124 KB Output is correct
6 Correct 86 ms 38132 KB Output is correct
7 Correct 171 ms 41864 KB Output is correct
8 Correct 143 ms 37904 KB Output is correct
9 Correct 80 ms 45392 KB Output is correct
10 Correct 197 ms 40832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 4 ms 8796 KB Output is correct
3 Correct 4 ms 8540 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 101 ms 33484 KB Output is correct
6 Correct 44 ms 40536 KB Output is correct
7 Correct 91 ms 38228 KB Output is correct
8 Correct 58 ms 33664 KB Output is correct
9 Correct 92 ms 36772 KB Output is correct
10 Correct 59 ms 33648 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 3 ms 9048 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 3 ms 8796 KB Output is correct
15 Correct 3 ms 8940 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 4 ms 8796 KB Output is correct
3 Correct 4 ms 8540 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 101 ms 33484 KB Output is correct
6 Correct 44 ms 40536 KB Output is correct
7 Correct 91 ms 38228 KB Output is correct
8 Correct 58 ms 33664 KB Output is correct
9 Correct 92 ms 36772 KB Output is correct
10 Correct 59 ms 33648 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 95 ms 45444 KB Output is correct
15 Correct 89 ms 45428 KB Output is correct
16 Correct 80 ms 45468 KB Output is correct
17 Correct 85 ms 45376 KB Output is correct
18 Correct 117 ms 45596 KB Output is correct
19 Correct 79 ms 45552 KB Output is correct
20 Correct 95 ms 45392 KB Output is correct
21 Correct 12 ms 10072 KB Output is correct
22 Correct 98 ms 45652 KB Output is correct
23 Correct 92 ms 45648 KB Output is correct
24 Correct 81 ms 45832 KB Output is correct
25 Correct 89 ms 45648 KB Output is correct
26 Correct 84 ms 45888 KB Output is correct
27 Correct 98 ms 45832 KB Output is correct
28 Correct 91 ms 45652 KB Output is correct
29 Correct 82 ms 45796 KB Output is correct
30 Correct 108 ms 45896 KB Output is correct
31 Correct 159 ms 37932 KB Output is correct
32 Correct 76 ms 45396 KB Output is correct
33 Correct 183 ms 42636 KB Output is correct
34 Correct 116 ms 38168 KB Output is correct
35 Correct 150 ms 42124 KB Output is correct
36 Correct 86 ms 38132 KB Output is correct
37 Correct 171 ms 41864 KB Output is correct
38 Correct 143 ms 37904 KB Output is correct
39 Correct 80 ms 45392 KB Output is correct
40 Correct 197 ms 40832 KB Output is correct
41 Correct 2 ms 8792 KB Output is correct
42 Correct 3 ms 9048 KB Output is correct
43 Correct 3 ms 8796 KB Output is correct
44 Correct 3 ms 8796 KB Output is correct
45 Correct 3 ms 8940 KB Output is correct
46 Correct 3 ms 8796 KB Output is correct
47 Correct 2 ms 8796 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 2 ms 8796 KB Output is correct
50 Correct 2 ms 9052 KB Output is correct
51 Correct 152 ms 38288 KB Output is correct
52 Correct 113 ms 45784 KB Output is correct
53 Correct 190 ms 41056 KB Output is correct
54 Correct 95 ms 38452 KB Output is correct
55 Correct 129 ms 37964 KB Output is correct
56 Correct 83 ms 45648 KB Output is correct
57 Correct 150 ms 41868 KB Output is correct
58 Correct 99 ms 38356 KB Output is correct
59 Correct 149 ms 38240 KB Output is correct
60 Correct 111 ms 45648 KB Output is correct
61 Correct 150 ms 41960 KB Output is correct
62 Correct 110 ms 37920 KB Output is correct
63 Correct 137 ms 37912 KB Output is correct
64 Correct 85 ms 45784 KB Output is correct
65 Correct 220 ms 41832 KB Output is correct
66 Correct 98 ms 38244 KB Output is correct
67 Correct 143 ms 37980 KB Output is correct
68 Correct 89 ms 45704 KB Output is correct
69 Correct 204 ms 40860 KB Output is correct
70 Correct 92 ms 38148 KB Output is correct