답안 #619559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
619559 2022-08-02T12:56:45 Z nohaxjustsoflo Election Campaign (JOI15_election_campaign) C++17
100 / 100
347 ms 49936 KB
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN=3e5+5;
const int mod=998244353;
const int mxlogN=20;
const int mxK=26;
const int inf=2e9;
const int K=600;


vector<int> adj[mxN];
int up[mxlogN][mxN], down[mxN], in[mxN], out[mxN], timer_;
vector<int> who[mxN];
void dfs(int i=1, int p=1, int d=0)
{
    up[0][i]=p, down[i]=d, in[i]=timer_++;
    who[d].push_back(i);
    for(int j=1; j<mxlogN; j++) up[j][i]=up[j-1][up[j-1][i]];
    for(int j:adj[i]) if(j!=p) dfs(j,i,d+1);
    out[i]=timer_;
}
int goup(int i, int k)
{
    for(int j=mxlogN-1; j>=0; j--) if(k&1<<j) i=up[j][i];
    return i;
}
int lca(int i, int j)
{
    if(down[i]<down[j]) swap(i,j);
    i=goup(i, down[i]-down[j]);
    if(i==j) return i;
    for(int k=mxlogN-1; k>=0; k--) if(up[k][i]!=up[k][j]) i=up[k][i], j=up[k][j];
    return up[0][i];
}
int sz;
void build(int n)
{
    sz=1;
    while(sz<n) sz<<=1;
}
ll sum[mxN];
void add(int l, int r, ll v, int i=0, int Lx=0, int Rx=sz)
{
    if(Rx<=l || Lx>=r) return;
    if(Lx>=l && Rx<=r)
    {
        sum[i]+=v;
        return;
    }
    int m=(Rx+Lx)/2;
    add(l,r,v,i*2+1,Lx,m);
    add(l,r,v,i*2+2,m,Rx);
}
ll dp2(int pos, int i=0, int Lx=0, int Rx=sz)
{
    if(Rx-Lx==1) return sum[i];
    int m=(Rx+Lx)/2;
    if(pos<m) return sum[i]+dp2(pos,i*2+1,Lx,m);
    else return sum[i]+dp2(pos,i*2+2,m,Rx);
}

struct qry
{
    int a,b,c;
};
vector<qry> qs[mxN];
int dp[mxN];
void solve(int i)
{
    int p=up[0][i];
    int ans=0;
    for(int j:adj[i])
    {
        if(j==p) continue;
        ans+=dp[j];
    }
    int ans2=ans;
    for(auto q:qs[i])
    {
        int a=q.a,b=q.b,c=q.c;
        int newans=dp2(in[a])+dp2(in[b])+c+ans;
        if(a!=i) newans-=dp[goup(a,down[a]-down[i]-1)];
        if(b!=i) newans-=dp[goup(b,down[b]-down[i]-1)];
        ans2=max(ans2,newans);
    }
    dp[i]=ans2;
    for(int j:adj[i])
    {
        if(j==p) continue;
        ans-=dp[j];
        add(in[j],out[j],ans);
        ans+=dp[j];
    }
    add(in[i],in[i]+1,ans);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    build(n);
    for(int i=1; i<n; i++)
    {
        int u,v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs();
    int m; cin >> m;
    for(int i=0; i<m; i++)
    {
        int a,b,c; cin >> a >> b >> c;
        qs[lca(a,b)].push_back({a,b,c});
    }
    for(int d=n-1; d>=0; d--) for(int i:who[d]) solve(i);
    cout << dp[1] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21588 KB Output is correct
2 Correct 11 ms 21512 KB Output is correct
3 Correct 11 ms 21516 KB Output is correct
4 Correct 12 ms 21756 KB Output is correct
5 Correct 136 ms 37548 KB Output is correct
6 Correct 87 ms 46260 KB Output is correct
7 Correct 146 ms 42964 KB Output is correct
8 Correct 96 ms 37252 KB Output is correct
9 Correct 139 ms 41164 KB Output is correct
10 Correct 110 ms 37296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21588 KB Output is correct
2 Correct 11 ms 21624 KB Output is correct
3 Correct 12 ms 21844 KB Output is correct
4 Correct 146 ms 49588 KB Output is correct
5 Correct 145 ms 49524 KB Output is correct
6 Correct 136 ms 49512 KB Output is correct
7 Correct 146 ms 49492 KB Output is correct
8 Correct 193 ms 49576 KB Output is correct
9 Correct 139 ms 49504 KB Output is correct
10 Correct 149 ms 49564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21588 KB Output is correct
2 Correct 11 ms 21624 KB Output is correct
3 Correct 12 ms 21844 KB Output is correct
4 Correct 146 ms 49588 KB Output is correct
5 Correct 145 ms 49524 KB Output is correct
6 Correct 136 ms 49512 KB Output is correct
7 Correct 146 ms 49492 KB Output is correct
8 Correct 193 ms 49576 KB Output is correct
9 Correct 139 ms 49504 KB Output is correct
10 Correct 149 ms 49564 KB Output is correct
11 Correct 24 ms 22624 KB Output is correct
12 Correct 189 ms 49852 KB Output is correct
13 Correct 158 ms 49776 KB Output is correct
14 Correct 139 ms 49864 KB Output is correct
15 Correct 156 ms 49936 KB Output is correct
16 Correct 141 ms 49764 KB Output is correct
17 Correct 189 ms 49748 KB Output is correct
18 Correct 165 ms 49868 KB Output is correct
19 Correct 182 ms 49836 KB Output is correct
20 Correct 172 ms 49864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 40380 KB Output is correct
2 Correct 145 ms 49488 KB Output is correct
3 Correct 347 ms 45824 KB Output is correct
4 Correct 159 ms 40252 KB Output is correct
5 Correct 266 ms 45252 KB Output is correct
6 Correct 184 ms 40012 KB Output is correct
7 Correct 307 ms 45020 KB Output is correct
8 Correct 198 ms 40512 KB Output is correct
9 Correct 142 ms 49480 KB Output is correct
10 Correct 276 ms 43852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21588 KB Output is correct
2 Correct 11 ms 21512 KB Output is correct
3 Correct 11 ms 21516 KB Output is correct
4 Correct 12 ms 21756 KB Output is correct
5 Correct 136 ms 37548 KB Output is correct
6 Correct 87 ms 46260 KB Output is correct
7 Correct 146 ms 42964 KB Output is correct
8 Correct 96 ms 37252 KB Output is correct
9 Correct 139 ms 41164 KB Output is correct
10 Correct 110 ms 37296 KB Output is correct
11 Correct 13 ms 21752 KB Output is correct
12 Correct 12 ms 21844 KB Output is correct
13 Correct 11 ms 21844 KB Output is correct
14 Correct 12 ms 21748 KB Output is correct
15 Correct 12 ms 21716 KB Output is correct
16 Correct 12 ms 21716 KB Output is correct
17 Correct 15 ms 21716 KB Output is correct
18 Correct 13 ms 21748 KB Output is correct
19 Correct 15 ms 21752 KB Output is correct
20 Correct 13 ms 21852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21588 KB Output is correct
2 Correct 11 ms 21512 KB Output is correct
3 Correct 11 ms 21516 KB Output is correct
4 Correct 12 ms 21756 KB Output is correct
5 Correct 136 ms 37548 KB Output is correct
6 Correct 87 ms 46260 KB Output is correct
7 Correct 146 ms 42964 KB Output is correct
8 Correct 96 ms 37252 KB Output is correct
9 Correct 139 ms 41164 KB Output is correct
10 Correct 110 ms 37296 KB Output is correct
11 Correct 11 ms 21588 KB Output is correct
12 Correct 11 ms 21624 KB Output is correct
13 Correct 12 ms 21844 KB Output is correct
14 Correct 146 ms 49588 KB Output is correct
15 Correct 145 ms 49524 KB Output is correct
16 Correct 136 ms 49512 KB Output is correct
17 Correct 146 ms 49492 KB Output is correct
18 Correct 193 ms 49576 KB Output is correct
19 Correct 139 ms 49504 KB Output is correct
20 Correct 149 ms 49564 KB Output is correct
21 Correct 24 ms 22624 KB Output is correct
22 Correct 189 ms 49852 KB Output is correct
23 Correct 158 ms 49776 KB Output is correct
24 Correct 139 ms 49864 KB Output is correct
25 Correct 156 ms 49936 KB Output is correct
26 Correct 141 ms 49764 KB Output is correct
27 Correct 189 ms 49748 KB Output is correct
28 Correct 165 ms 49868 KB Output is correct
29 Correct 182 ms 49836 KB Output is correct
30 Correct 172 ms 49864 KB Output is correct
31 Correct 209 ms 40380 KB Output is correct
32 Correct 145 ms 49488 KB Output is correct
33 Correct 347 ms 45824 KB Output is correct
34 Correct 159 ms 40252 KB Output is correct
35 Correct 266 ms 45252 KB Output is correct
36 Correct 184 ms 40012 KB Output is correct
37 Correct 307 ms 45020 KB Output is correct
38 Correct 198 ms 40512 KB Output is correct
39 Correct 142 ms 49480 KB Output is correct
40 Correct 276 ms 43852 KB Output is correct
41 Correct 13 ms 21752 KB Output is correct
42 Correct 12 ms 21844 KB Output is correct
43 Correct 11 ms 21844 KB Output is correct
44 Correct 12 ms 21748 KB Output is correct
45 Correct 12 ms 21716 KB Output is correct
46 Correct 12 ms 21716 KB Output is correct
47 Correct 15 ms 21716 KB Output is correct
48 Correct 13 ms 21748 KB Output is correct
49 Correct 15 ms 21752 KB Output is correct
50 Correct 13 ms 21852 KB Output is correct
51 Correct 199 ms 40860 KB Output is correct
52 Correct 168 ms 49756 KB Output is correct
53 Correct 277 ms 44212 KB Output is correct
54 Correct 179 ms 40492 KB Output is correct
55 Correct 209 ms 40600 KB Output is correct
56 Correct 150 ms 49872 KB Output is correct
57 Correct 238 ms 45004 KB Output is correct
58 Correct 180 ms 40240 KB Output is correct
59 Correct 198 ms 40856 KB Output is correct
60 Correct 200 ms 49772 KB Output is correct
61 Correct 250 ms 45132 KB Output is correct
62 Correct 210 ms 39984 KB Output is correct
63 Correct 219 ms 40500 KB Output is correct
64 Correct 158 ms 49900 KB Output is correct
65 Correct 320 ms 44948 KB Output is correct
66 Correct 161 ms 40364 KB Output is correct
67 Correct 205 ms 40556 KB Output is correct
68 Correct 168 ms 49748 KB Output is correct
69 Correct 268 ms 43620 KB Output is correct
70 Correct 179 ms 40412 KB Output is correct