Submission #754996

# Submission time Handle Problem Language Result Execution time Memory
754996 2023-06-09T08:53:06 Z ooscode Election Campaign (JOI15_election_campaign) C++17
100 / 100
415 ms 66336 KB
// IN THE NAME OF ALLAH
 
#include<bits/stdc++.h>
 
using namespace std;
 
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define wall cerr << "------------------------------------" << endl
#define pb push_back
#define pob pop_back
#define F first
#define S second
#define all(x) x.begin() , x.end()
#define scan scanf
#define print printf
#define outs(x) print("%lld " , x)
#define out(x) print("%lld\n" , x)
#define in(x) scan("%lld" , &x)
#define int ll

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
#pragma GCC optimize("Ofast")
 
typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef pair<pii,int> piii;
typedef pair<pll , ll> plll;
 
const int N = 4e5+10;
const int K = 640+10;
const ll mod = 1e9+7;
const ll INF = 1e9+10;
const int P = 31;
const int lg = 25;

const int delta = 10289;

int par[N][lg];
int p[N];
int h[N];
int dp[N];
int pd[N];
int st[N];
int fn[N];
int oo = 0;

int seg[4*N];
int la[4*N];

vector<int> g[N];

int n , m;

vector<pair<int , pii>> qu[N];

void shift(int v , int tl , int tr) {
    seg[v] += la[v];

    if(tl != tr) {
        la[2*v] += la[v];
        la[2*v+1] += la[v];
    }

    la[v] = 0;
}

void upd(int v , int tl , int tr , int l , int r , int x) {
    shift(v , tl , tr);

    if(l > r)
        return;

    if(tl == l && tr == r) {
        la[v] += x;
        shift(v , tl , tr);
        
        return;
    }

    int tm = (tl + tr)/2;

    upd(2*v , tl , tm , l , min(tm , r) , x);
    upd(2*v+1 , tm+1 , tr , max(l , tm+1) , r , x);

    seg[v] = seg[2*v] + seg[2*v+1];
}

int ask(int v , int tl , int tr , int pos) {
    shift(v , tl , tr);

    if(tl == tr)
        return seg[v];

    int tm = (tl + tr)/2;

    if(tm >= pos)
        return ask(2*v , tl , tm , pos);
    return ask(2*v+1 , tm+1 , tr , pos);
}

void init() {
    for(int i = 1 ; i <= n ; i++)
        par[i][0] = p[i];

    for(int i = 1 ; i < lg ; i++)
        for(int j = 1 ; j <= n ; j++)
            par[j][i] = par[par[j][i-1]][i-1];
}

void dfs(int v , int pr) {
    st[v] = ++oo;
    for(auto u : g[v]) {
        if(u == pr)
            continue;

        p[u] = v;
        h[u] = h[v] + 1;

        dfs(u , v);
    }

    fn[v] = oo;
}

int get_par(int v , int h) {
    for(int i = 0 ; i < lg ; i++)
        if(h & (1ll << i))
            v = par[v][i];

    return v;
}

int lca(int v , int u) {
    if(h[u] > h[v])
        swap(u , v);

    int he = h[v] - h[u];

    if(he) 
        v = get_par(v , he);

    if(v == u)
        return v;

    for(int i = lg-1 ; i >= 0 ; i--)
        if(par[v][i] != par[u][i]) {
            u = par[u][i];
            v = par[v][i];
        }

    return par[v][0];
}

void get(int v , int pr) {
    for(auto u : g[v]) {
        if(u == pr)
            continue;
        
        get(u , v);
        pd[v] += dp[u];
    }

    dp[v] = pd[v];

    for(auto px : qu[v]) {
        int y = px.S.F;
        int u = px.S.S;
        int w = px.F;

        int x = pd[v] + w;

        int f = 0 , e = 0;

        if(u != v)
            f = ask(1 , 1 , n , st[u]);
        if(y != v)
            e = ask(1 , 1 , n , st[y]);

        dp[v] = max(dp[v] , x + f + e);
    }

    upd(1 , 1 , n , st[v] , fn[v] , pd[v] - dp[v]);
}

signed main()
{   
    cin >> n;

    for(int i = 2 ; i <= n ; i++) {
        int v , u;
        cin >> v >> u;

        g[v].pb(u);
        g[u].pb(v);
    }

    dfs(1 , -1);

    init();

    cin >> m;

    for(int i = 1 ; i <= m ; i++) {
        int v , u , w;
        cin >> v >> u >> w;

        int lc = lca(v , u);

        qu[lc].pb({w , {v , u}});
    }

    get(1 , -1);

    cout << dp[1] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 13 ms 19132 KB Output is correct
3 Correct 11 ms 19148 KB Output is correct
4 Correct 11 ms 19412 KB Output is correct
5 Correct 164 ms 52636 KB Output is correct
6 Correct 125 ms 61240 KB Output is correct
7 Correct 161 ms 58316 KB Output is correct
8 Correct 138 ms 51996 KB Output is correct
9 Correct 167 ms 56448 KB Output is correct
10 Correct 174 ms 51924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19164 KB Output is correct
2 Correct 12 ms 19088 KB Output is correct
3 Correct 11 ms 19532 KB Output is correct
4 Correct 251 ms 63592 KB Output is correct
5 Correct 252 ms 63592 KB Output is correct
6 Correct 229 ms 63516 KB Output is correct
7 Correct 273 ms 63512 KB Output is correct
8 Correct 256 ms 63544 KB Output is correct
9 Correct 233 ms 63644 KB Output is correct
10 Correct 245 ms 63548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19164 KB Output is correct
2 Correct 12 ms 19088 KB Output is correct
3 Correct 11 ms 19532 KB Output is correct
4 Correct 251 ms 63592 KB Output is correct
5 Correct 252 ms 63592 KB Output is correct
6 Correct 229 ms 63516 KB Output is correct
7 Correct 273 ms 63512 KB Output is correct
8 Correct 256 ms 63544 KB Output is correct
9 Correct 233 ms 63644 KB Output is correct
10 Correct 245 ms 63548 KB Output is correct
11 Correct 31 ms 20564 KB Output is correct
12 Correct 281 ms 63664 KB Output is correct
13 Correct 255 ms 63476 KB Output is correct
14 Correct 234 ms 63516 KB Output is correct
15 Correct 309 ms 63604 KB Output is correct
16 Correct 261 ms 63616 KB Output is correct
17 Correct 288 ms 63460 KB Output is correct
18 Correct 248 ms 63480 KB Output is correct
19 Correct 238 ms 63528 KB Output is correct
20 Correct 259 ms 63504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 54296 KB Output is correct
2 Correct 234 ms 66024 KB Output is correct
3 Correct 349 ms 62820 KB Output is correct
4 Correct 261 ms 56584 KB Output is correct
5 Correct 318 ms 61972 KB Output is correct
6 Correct 292 ms 56364 KB Output is correct
7 Correct 379 ms 61708 KB Output is correct
8 Correct 285 ms 56860 KB Output is correct
9 Correct 246 ms 66064 KB Output is correct
10 Correct 415 ms 60456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 13 ms 19132 KB Output is correct
3 Correct 11 ms 19148 KB Output is correct
4 Correct 11 ms 19412 KB Output is correct
5 Correct 164 ms 52636 KB Output is correct
6 Correct 125 ms 61240 KB Output is correct
7 Correct 161 ms 58316 KB Output is correct
8 Correct 138 ms 51996 KB Output is correct
9 Correct 167 ms 56448 KB Output is correct
10 Correct 174 ms 51924 KB Output is correct
11 Correct 12 ms 19500 KB Output is correct
12 Correct 12 ms 19504 KB Output is correct
13 Correct 12 ms 19536 KB Output is correct
14 Correct 13 ms 19452 KB Output is correct
15 Correct 16 ms 19532 KB Output is correct
16 Correct 15 ms 19504 KB Output is correct
17 Correct 14 ms 19540 KB Output is correct
18 Correct 14 ms 19540 KB Output is correct
19 Correct 13 ms 19540 KB Output is correct
20 Correct 13 ms 19588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 13 ms 19132 KB Output is correct
3 Correct 11 ms 19148 KB Output is correct
4 Correct 11 ms 19412 KB Output is correct
5 Correct 164 ms 52636 KB Output is correct
6 Correct 125 ms 61240 KB Output is correct
7 Correct 161 ms 58316 KB Output is correct
8 Correct 138 ms 51996 KB Output is correct
9 Correct 167 ms 56448 KB Output is correct
10 Correct 174 ms 51924 KB Output is correct
11 Correct 11 ms 19164 KB Output is correct
12 Correct 12 ms 19088 KB Output is correct
13 Correct 11 ms 19532 KB Output is correct
14 Correct 251 ms 63592 KB Output is correct
15 Correct 252 ms 63592 KB Output is correct
16 Correct 229 ms 63516 KB Output is correct
17 Correct 273 ms 63512 KB Output is correct
18 Correct 256 ms 63544 KB Output is correct
19 Correct 233 ms 63644 KB Output is correct
20 Correct 245 ms 63548 KB Output is correct
21 Correct 31 ms 20564 KB Output is correct
22 Correct 281 ms 63664 KB Output is correct
23 Correct 255 ms 63476 KB Output is correct
24 Correct 234 ms 63516 KB Output is correct
25 Correct 309 ms 63604 KB Output is correct
26 Correct 261 ms 63616 KB Output is correct
27 Correct 288 ms 63460 KB Output is correct
28 Correct 248 ms 63480 KB Output is correct
29 Correct 238 ms 63528 KB Output is correct
30 Correct 259 ms 63504 KB Output is correct
31 Correct 311 ms 54296 KB Output is correct
32 Correct 234 ms 66024 KB Output is correct
33 Correct 349 ms 62820 KB Output is correct
34 Correct 261 ms 56584 KB Output is correct
35 Correct 318 ms 61972 KB Output is correct
36 Correct 292 ms 56364 KB Output is correct
37 Correct 379 ms 61708 KB Output is correct
38 Correct 285 ms 56860 KB Output is correct
39 Correct 246 ms 66064 KB Output is correct
40 Correct 415 ms 60456 KB Output is correct
41 Correct 12 ms 19500 KB Output is correct
42 Correct 12 ms 19504 KB Output is correct
43 Correct 12 ms 19536 KB Output is correct
44 Correct 13 ms 19452 KB Output is correct
45 Correct 16 ms 19532 KB Output is correct
46 Correct 15 ms 19504 KB Output is correct
47 Correct 14 ms 19540 KB Output is correct
48 Correct 14 ms 19540 KB Output is correct
49 Correct 13 ms 19540 KB Output is correct
50 Correct 13 ms 19588 KB Output is correct
51 Correct 305 ms 57124 KB Output is correct
52 Correct 328 ms 66252 KB Output is correct
53 Correct 384 ms 60824 KB Output is correct
54 Correct 265 ms 56680 KB Output is correct
55 Correct 296 ms 57032 KB Output is correct
56 Correct 265 ms 66336 KB Output is correct
57 Correct 313 ms 61644 KB Output is correct
58 Correct 355 ms 56480 KB Output is correct
59 Correct 311 ms 57260 KB Output is correct
60 Correct 299 ms 66220 KB Output is correct
61 Correct 323 ms 61768 KB Output is correct
62 Correct 293 ms 56132 KB Output is correct
63 Correct 332 ms 56808 KB Output is correct
64 Correct 267 ms 66216 KB Output is correct
65 Correct 358 ms 61612 KB Output is correct
66 Correct 269 ms 56344 KB Output is correct
67 Correct 285 ms 56884 KB Output is correct
68 Correct 281 ms 66288 KB Output is correct
69 Correct 311 ms 60352 KB Output is correct
70 Correct 267 ms 56672 KB Output is correct