Submission #484390

# Submission time Handle Problem Language Result Execution time Memory
484390 2021-11-03T08:15:48 Z Mahdi Election Campaign (JOI15_election_campaign) C++17
100 / 100
198 ms 36848 KB
#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
typedef long long ll;
const int N=1e5+5;
int n, q, lg, dp[N], pd[N][18], s[N], f[N], dis[N], h[N];
vector<int>g[N], kd[N], nd;
vector<pair<int, pii>>w[N];

struct fenwick{
    vector<ll>bit=vector<ll>(n, 0);
    void add(int v, int x){
        for(int i=v;i<n;i|=i+1)
            bit[i]+=x;
    }
    void add(int l, int r, int x){
        add(l, x);
        add(r, -x);
    }
    int num(int v){
        ll ans=0;
        for(int i=v;i>=0;i=(i&(i+1))-1)
            ans+=bit[i];
        return ans;
    }
};

void dfs(const int &v, const int &p, int &t){
    s[v]=t++;
    nd.push_back(v);
    for(auto u: g[v]){
        if(u!=p){
            dis[u]=dis[v]+1;
            kd[v].push_back(u);
            pd[u][0]=v;
            for(int i=1;i<=lg;++i)
                pd[u][i]=pd[pd[u][i-1]][i-1];
            dfs(u, v, t);
        }
    }
    f[v]=t;
}

int par(int v, int p){
    int pp;
    while(p>0){
        pp=p&(-p);
        v=pd[v][31-__builtin_clz(pp)];
        p-=pp;
    }
    return v;
}

int lca(int v, int u){
    v=par(v, dis[v]-dis[u]);
    u=par(u, dis[u]-dis[v]);
    if(u==v)
        return v;
    for(int i=lg;i>=0;--i){
        if(pd[v][i]!=pd[u][i]){
            v=pd[v][i];
            u=pd[u][i];
        }
    }
    return pd[v][0];
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    lg=31-__builtin_clz(n);
    int v, u;
    for(int i=0;i<n-1;++i){
        cin>>u>>v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    int t=0;
    dfs(0, 0, t);
    cin>>q;
    int x, y;
    while(q--){
        cin>>v>>u>>x;
        y=lca(--v, --u);
        w[y].push_back({x, {v, u}});
    }
    fenwick A;
    for(int i=n-1;i>=0;--i){
        v=nd[i];
        dp[v]=h[v];
        for(auto u: w[v]){
            x=A.num(s[u.S.F])+A.num(s[u.S.S])-2*A.num(s[v])+h[v]+u.F;
            dp[v]=max(dp[v], x);
        }
        h[pd[v][0]]+=dp[v];
        A.add(s[v], f[v], -dp[v]);
        A.add(s[pd[v][0]], f[pd[v][0]], dp[v]);
    }
    cout<<dp[0];
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7372 KB Output is correct
2 Correct 3 ms 7372 KB Output is correct
3 Correct 3 ms 7372 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 84 ms 23352 KB Output is correct
6 Correct 48 ms 34112 KB Output is correct
7 Correct 92 ms 30160 KB Output is correct
8 Correct 59 ms 22328 KB Output is correct
9 Correct 104 ms 28484 KB Output is correct
10 Correct 66 ms 22548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7628 KB Output is correct
4 Correct 91 ms 35104 KB Output is correct
5 Correct 92 ms 35100 KB Output is correct
6 Correct 88 ms 35188 KB Output is correct
7 Correct 111 ms 35388 KB Output is correct
8 Correct 96 ms 35132 KB Output is correct
9 Correct 85 ms 35116 KB Output is correct
10 Correct 90 ms 35108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7628 KB Output is correct
4 Correct 91 ms 35104 KB Output is correct
5 Correct 92 ms 35100 KB Output is correct
6 Correct 88 ms 35188 KB Output is correct
7 Correct 111 ms 35388 KB Output is correct
8 Correct 96 ms 35132 KB Output is correct
9 Correct 85 ms 35116 KB Output is correct
10 Correct 90 ms 35108 KB Output is correct
11 Correct 10 ms 8092 KB Output is correct
12 Correct 95 ms 35080 KB Output is correct
13 Correct 103 ms 35324 KB Output is correct
14 Correct 88 ms 35160 KB Output is correct
15 Correct 94 ms 35212 KB Output is correct
16 Correct 88 ms 35132 KB Output is correct
17 Correct 100 ms 35180 KB Output is correct
18 Correct 95 ms 35116 KB Output is correct
19 Correct 93 ms 35204 KB Output is correct
20 Correct 93 ms 35164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 23716 KB Output is correct
2 Correct 88 ms 36780 KB Output is correct
3 Correct 182 ms 32444 KB Output is correct
4 Correct 88 ms 24460 KB Output is correct
5 Correct 152 ms 31756 KB Output is correct
6 Correct 96 ms 24504 KB Output is correct
7 Correct 174 ms 31536 KB Output is correct
8 Correct 123 ms 25732 KB Output is correct
9 Correct 92 ms 36772 KB Output is correct
10 Correct 161 ms 30268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7372 KB Output is correct
2 Correct 3 ms 7372 KB Output is correct
3 Correct 3 ms 7372 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 84 ms 23352 KB Output is correct
6 Correct 48 ms 34112 KB Output is correct
7 Correct 92 ms 30160 KB Output is correct
8 Correct 59 ms 22328 KB Output is correct
9 Correct 104 ms 28484 KB Output is correct
10 Correct 66 ms 22548 KB Output is correct
11 Correct 4 ms 7500 KB Output is correct
12 Correct 4 ms 7628 KB Output is correct
13 Correct 4 ms 7632 KB Output is correct
14 Correct 5 ms 7584 KB Output is correct
15 Correct 4 ms 7500 KB Output is correct
16 Correct 6 ms 7500 KB Output is correct
17 Correct 4 ms 7500 KB Output is correct
18 Correct 5 ms 7628 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7372 KB Output is correct
2 Correct 3 ms 7372 KB Output is correct
3 Correct 3 ms 7372 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 84 ms 23352 KB Output is correct
6 Correct 48 ms 34112 KB Output is correct
7 Correct 92 ms 30160 KB Output is correct
8 Correct 59 ms 22328 KB Output is correct
9 Correct 104 ms 28484 KB Output is correct
10 Correct 66 ms 22548 KB Output is correct
11 Correct 3 ms 7372 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7628 KB Output is correct
14 Correct 91 ms 35104 KB Output is correct
15 Correct 92 ms 35100 KB Output is correct
16 Correct 88 ms 35188 KB Output is correct
17 Correct 111 ms 35388 KB Output is correct
18 Correct 96 ms 35132 KB Output is correct
19 Correct 85 ms 35116 KB Output is correct
20 Correct 90 ms 35108 KB Output is correct
21 Correct 10 ms 8092 KB Output is correct
22 Correct 95 ms 35080 KB Output is correct
23 Correct 103 ms 35324 KB Output is correct
24 Correct 88 ms 35160 KB Output is correct
25 Correct 94 ms 35212 KB Output is correct
26 Correct 88 ms 35132 KB Output is correct
27 Correct 100 ms 35180 KB Output is correct
28 Correct 95 ms 35116 KB Output is correct
29 Correct 93 ms 35204 KB Output is correct
30 Correct 93 ms 35164 KB Output is correct
31 Correct 116 ms 23716 KB Output is correct
32 Correct 88 ms 36780 KB Output is correct
33 Correct 182 ms 32444 KB Output is correct
34 Correct 88 ms 24460 KB Output is correct
35 Correct 152 ms 31756 KB Output is correct
36 Correct 96 ms 24504 KB Output is correct
37 Correct 174 ms 31536 KB Output is correct
38 Correct 123 ms 25732 KB Output is correct
39 Correct 92 ms 36772 KB Output is correct
40 Correct 161 ms 30268 KB Output is correct
41 Correct 4 ms 7500 KB Output is correct
42 Correct 4 ms 7628 KB Output is correct
43 Correct 4 ms 7632 KB Output is correct
44 Correct 5 ms 7584 KB Output is correct
45 Correct 4 ms 7500 KB Output is correct
46 Correct 6 ms 7500 KB Output is correct
47 Correct 4 ms 7500 KB Output is correct
48 Correct 5 ms 7628 KB Output is correct
49 Correct 4 ms 7508 KB Output is correct
50 Correct 4 ms 7640 KB Output is correct
51 Correct 154 ms 25848 KB Output is correct
52 Correct 103 ms 36848 KB Output is correct
53 Correct 198 ms 30432 KB Output is correct
54 Correct 98 ms 24604 KB Output is correct
55 Correct 145 ms 25408 KB Output is correct
56 Correct 98 ms 36796 KB Output is correct
57 Correct 179 ms 31396 KB Output is correct
58 Correct 94 ms 24444 KB Output is correct
59 Correct 134 ms 25744 KB Output is correct
60 Correct 97 ms 36792 KB Output is correct
61 Correct 167 ms 31296 KB Output is correct
62 Correct 99 ms 24408 KB Output is correct
63 Correct 125 ms 25316 KB Output is correct
64 Correct 98 ms 36780 KB Output is correct
65 Correct 188 ms 31116 KB Output is correct
66 Correct 111 ms 24596 KB Output is correct
67 Correct 148 ms 25508 KB Output is correct
68 Correct 100 ms 36756 KB Output is correct
69 Correct 162 ms 29764 KB Output is correct
70 Correct 104 ms 24764 KB Output is correct