Submission #1086532

# Submission time Handle Problem Language Result Execution time Memory
1086532 2024-09-11T01:44:31 Z steveonalex Election Campaign (JOI15_election_campaign) C++17
100 / 100
110 ms 31060 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 1e5 + 69, LOG_N = 17;


struct FenwickTree{
    int n;
    vector<ll> a;

    FenwickTree(int _n){
        n = _n;
        a.resize(n+1);
    }

    void update(int i, ll v){
        while(i <= n){
            a[i] += v;
            i += LASTBIT(i);
        }
    }

    void update(int l, int r, ll v){
        update(l, v); update(r + 1, -v);
    }

    ll get(int i){
        ll ans = 0;
        while(i > 0){
            ans += a[i];
            i -= LASTBIT(i);
        }
        return ans;
    }
};

int n, q; 
vector<int> graph[N];
int parent[N][LOG_N], h[N];
int in[N], out[N], dfs_cnt = 0;

void dfs(int u, int p){
    in[u] = out[u] = ++dfs_cnt;
    h[u] = h[p] + 1;
    for(int j = 1; MASK(j) < h[u]; ++j)
        parent[u][j] = parent[parent[u][j-1]][j-1];
    for(int v: graph[u]) if (v != p){
        parent[v][0] = u;
        dfs(v, u);
        out[u] = out[v];
    }
}

int jump(int u, int d){
    for(int j = 0; j < LOG_N; ++j) if (GETBIT(d, j)) u = parent[u][j];
    return u;
}

int LCA(int u, int v){
    if (h[u] < h[v]) swap(u, v);
    u = jump(u, h[u] - h[v]);
    if (u == v) return u;
    for(int j = LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){
        u = parent[u][j];
        v = parent[v][j];
    }
    return parent[u][0];
}

vector<array<int, 3>> queries[N];
ll dp[N], sum[N];
FenwickTree bit(N);


void go(int u, int p){
    for(int v: graph[u]) if (v != p){
        go(v, u);
        sum[u] += dp[v];
        dp[u] += dp[v];
    }

    for(array<int, 3> i: queries[u]){
        int x = i[0], y = i[1], w = i[2];
        ll ans = sum[u] + w;
        ans += bit.get(in[x]) + bit.get(in[y]) - 2 * bit.get(in[u]);
        maximize(dp[u], ans);
    }

    bit.update(in[u], out[u], sum[u] - dp[u]);
}
 
int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    clock_t start = clock();
 
    cin >> n;
    for(int i = 1; i<n; ++i){
        int u,v; cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    dfs(1, 0);

    cin >> q;
    while(q--){
        int u, v, w; cin >> u >> v >> w;
        queries[LCA(u, v)].push_back({{u, v, w}});
    }

    go(1, 0);

    cout << dp[1] << "\n";
 
    cerr << "Time elapsed: " << clock() - start << " ms\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5896 KB Output is correct
5 Correct 47 ms 19784 KB Output is correct
6 Correct 35 ms 27220 KB Output is correct
7 Correct 52 ms 24720 KB Output is correct
8 Correct 39 ms 18604 KB Output is correct
9 Correct 48 ms 23008 KB Output is correct
10 Correct 32 ms 18520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5976 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 3 ms 5980 KB Output is correct
4 Correct 71 ms 30704 KB Output is correct
5 Correct 74 ms 30756 KB Output is correct
6 Correct 65 ms 30540 KB Output is correct
7 Correct 73 ms 30548 KB Output is correct
8 Correct 77 ms 30544 KB Output is correct
9 Correct 69 ms 30680 KB Output is correct
10 Correct 79 ms 30696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5976 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 3 ms 5980 KB Output is correct
4 Correct 71 ms 30704 KB Output is correct
5 Correct 74 ms 30756 KB Output is correct
6 Correct 65 ms 30540 KB Output is correct
7 Correct 73 ms 30548 KB Output is correct
8 Correct 77 ms 30544 KB Output is correct
9 Correct 69 ms 30680 KB Output is correct
10 Correct 79 ms 30696 KB Output is correct
11 Correct 9 ms 7004 KB Output is correct
12 Correct 76 ms 30920 KB Output is correct
13 Correct 78 ms 30800 KB Output is correct
14 Correct 68 ms 30804 KB Output is correct
15 Correct 76 ms 30804 KB Output is correct
16 Correct 73 ms 31056 KB Output is correct
17 Correct 77 ms 31004 KB Output is correct
18 Correct 76 ms 30800 KB Output is correct
19 Correct 67 ms 31060 KB Output is correct
20 Correct 73 ms 30808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 22540 KB Output is correct
2 Correct 66 ms 30544 KB Output is correct
3 Correct 103 ms 27688 KB Output is correct
4 Correct 59 ms 21416 KB Output is correct
5 Correct 89 ms 27060 KB Output is correct
6 Correct 56 ms 21448 KB Output is correct
7 Correct 110 ms 26888 KB Output is correct
8 Correct 76 ms 22780 KB Output is correct
9 Correct 68 ms 30784 KB Output is correct
10 Correct 100 ms 25864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5896 KB Output is correct
5 Correct 47 ms 19784 KB Output is correct
6 Correct 35 ms 27220 KB Output is correct
7 Correct 52 ms 24720 KB Output is correct
8 Correct 39 ms 18604 KB Output is correct
9 Correct 48 ms 23008 KB Output is correct
10 Correct 32 ms 18520 KB Output is correct
11 Correct 3 ms 5980 KB Output is correct
12 Correct 3 ms 5984 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5984 KB Output is correct
15 Correct 3 ms 6132 KB Output is correct
16 Correct 3 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5976 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 3 ms 6232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5896 KB Output is correct
5 Correct 47 ms 19784 KB Output is correct
6 Correct 35 ms 27220 KB Output is correct
7 Correct 52 ms 24720 KB Output is correct
8 Correct 39 ms 18604 KB Output is correct
9 Correct 48 ms 23008 KB Output is correct
10 Correct 32 ms 18520 KB Output is correct
11 Correct 3 ms 5976 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 71 ms 30704 KB Output is correct
15 Correct 74 ms 30756 KB Output is correct
16 Correct 65 ms 30540 KB Output is correct
17 Correct 73 ms 30548 KB Output is correct
18 Correct 77 ms 30544 KB Output is correct
19 Correct 69 ms 30680 KB Output is correct
20 Correct 79 ms 30696 KB Output is correct
21 Correct 9 ms 7004 KB Output is correct
22 Correct 76 ms 30920 KB Output is correct
23 Correct 78 ms 30800 KB Output is correct
24 Correct 68 ms 30804 KB Output is correct
25 Correct 76 ms 30804 KB Output is correct
26 Correct 73 ms 31056 KB Output is correct
27 Correct 77 ms 31004 KB Output is correct
28 Correct 76 ms 30800 KB Output is correct
29 Correct 67 ms 31060 KB Output is correct
30 Correct 73 ms 30808 KB Output is correct
31 Correct 74 ms 22540 KB Output is correct
32 Correct 66 ms 30544 KB Output is correct
33 Correct 103 ms 27688 KB Output is correct
34 Correct 59 ms 21416 KB Output is correct
35 Correct 89 ms 27060 KB Output is correct
36 Correct 56 ms 21448 KB Output is correct
37 Correct 110 ms 26888 KB Output is correct
38 Correct 76 ms 22780 KB Output is correct
39 Correct 68 ms 30784 KB Output is correct
40 Correct 100 ms 25864 KB Output is correct
41 Correct 3 ms 5980 KB Output is correct
42 Correct 3 ms 5984 KB Output is correct
43 Correct 3 ms 5980 KB Output is correct
44 Correct 3 ms 5984 KB Output is correct
45 Correct 3 ms 6132 KB Output is correct
46 Correct 3 ms 5980 KB Output is correct
47 Correct 3 ms 5980 KB Output is correct
48 Correct 3 ms 5976 KB Output is correct
49 Correct 3 ms 5980 KB Output is correct
50 Correct 3 ms 6232 KB Output is correct
51 Correct 76 ms 23124 KB Output is correct
52 Correct 74 ms 30848 KB Output is correct
53 Correct 98 ms 26124 KB Output is correct
54 Correct 55 ms 21768 KB Output is correct
55 Correct 75 ms 22900 KB Output is correct
56 Correct 73 ms 30800 KB Output is correct
57 Correct 87 ms 26820 KB Output is correct
58 Correct 57 ms 21512 KB Output is correct
59 Correct 78 ms 22868 KB Output is correct
60 Correct 73 ms 30964 KB Output is correct
61 Correct 85 ms 26964 KB Output is correct
62 Correct 56 ms 21676 KB Output is correct
63 Correct 72 ms 22456 KB Output is correct
64 Correct 73 ms 30804 KB Output is correct
65 Correct 104 ms 26888 KB Output is correct
66 Correct 52 ms 21648 KB Output is correct
67 Correct 71 ms 22596 KB Output is correct
68 Correct 76 ms 30804 KB Output is correct
69 Correct 82 ms 25628 KB Output is correct
70 Correct 55 ms 21724 KB Output is correct