Submission #853267

# Submission time Handle Problem Language Result Execution time Memory
853267 2023-09-23T19:25:33 Z epicci23 Election Campaign (JOI15_election_campaign) C++17
100 / 100
173 ms 45092 KB
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n" 
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()


const int N = 1e5 + 5;
const int LOG = 20;
vector<int> v[N];
int fw[N][2],tin[N],tout[N];
int lift[N][LOG];
int timer=0;
int dp[N],pre[N];
vector<array<int,3>> go[N];

void upd(int c,int t,int u){
  for(;c<N;c+=c&-c) fw[c][t]+=u;
}

int query(int c,int t,int res=0){
  for(;c;c-=c&-c) res+=fw[c][t];
  return res;
}

bool check(int a,int b){
 return tin[a]<=tin[b] && tin[b]<=tout[a];
}

int lca(int a,int b){
  if(check(a,b)) return a;
  for(int i=LOG-1;i>=0;i--) if(!check(lift[a][i],b)) a=lift[a][i];
  return lift[a][0];
}

void dfs(int c,int p){
  lift[c][0]=p;
  for(int i=1;i<LOG;i++) lift[c][i]=lift[lift[c][i-1]][i-1];
  tin[c]=++timer;
  tout[c]=tin[c];
  for(int x:v[c]){
  	if(x==p) continue;
  	dfs(x,c);
  	tout[c]=max(tout[c],tout[x]);
  }
}

void dfs2(int c,int p){
  for(int x:v[c]){
  	if(x==p) continue;
  	dfs2(x,c);
  	pre[c]+=dp[x];
  }
  dp[c]=max(dp[c],pre[c]);
  upd(tin[c],1,pre[c]),upd(tout[c]+1,1,-pre[c]);
  for(auto x:go[c]){
  	int cur=x[2];
  	cur+=query(tin[x[0]],1);
  	cur-=query(tin[x[0]],0);
  	cur+=query(tin[x[1]],1);
  	cur-=query(tin[x[1]],0);
  	cur-=pre[c];
  	dp[c]=max(dp[c],cur);
  }
  upd(tin[c],0,dp[c]),upd(tout[c]+1,0,-dp[c]);
}

void solve(){
  int n; cin >> n;
  for(int i=1;i<n;i++){
  	int a,b;
  	cin >> a >> b;
  	v[a].pb(b);
  	v[b].pb(a);
  }
  
  dfs(1,1);
  int q; cin >> q;

  array<int,3> ar[q+1];
  for(int i=1;i<=q;i++){
  	int a,b,c;
  	cin >> a >> b >> c;
    ar[i]={a,b,c};
    go[lca(a,b)].pb({a,b,c});
  }

  dfs2(1,1);

  cout << dp[1] << endl;
}

int32_t main(){

  cin.tie(0); ios::sync_with_stdio(0);
  
  int t=1;//cin >> t;
  while(t--) solve();

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 66 ms 30576 KB Output is correct
6 Correct 41 ms 37464 KB Output is correct
7 Correct 68 ms 35156 KB Output is correct
8 Correct 65 ms 30516 KB Output is correct
9 Correct 65 ms 33696 KB Output is correct
10 Correct 60 ms 30704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11108 KB Output is correct
4 Correct 93 ms 44672 KB Output is correct
5 Correct 110 ms 44820 KB Output is correct
6 Correct 92 ms 44828 KB Output is correct
7 Correct 103 ms 44556 KB Output is correct
8 Correct 95 ms 44560 KB Output is correct
9 Correct 89 ms 44628 KB Output is correct
10 Correct 93 ms 44628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11108 KB Output is correct
4 Correct 93 ms 44672 KB Output is correct
5 Correct 110 ms 44820 KB Output is correct
6 Correct 92 ms 44828 KB Output is correct
7 Correct 103 ms 44556 KB Output is correct
8 Correct 95 ms 44560 KB Output is correct
9 Correct 89 ms 44628 KB Output is correct
10 Correct 93 ms 44628 KB Output is correct
11 Correct 13 ms 13456 KB Output is correct
12 Correct 98 ms 44884 KB Output is correct
13 Correct 107 ms 44980 KB Output is correct
14 Correct 82 ms 44880 KB Output is correct
15 Correct 97 ms 45092 KB Output is correct
16 Correct 82 ms 44936 KB Output is correct
17 Correct 96 ms 44828 KB Output is correct
18 Correct 94 ms 44884 KB Output is correct
19 Correct 83 ms 44880 KB Output is correct
20 Correct 99 ms 44880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 37080 KB Output is correct
2 Correct 79 ms 44624 KB Output is correct
3 Correct 173 ms 41868 KB Output is correct
4 Correct 95 ms 37384 KB Output is correct
5 Correct 172 ms 41368 KB Output is correct
6 Correct 96 ms 37572 KB Output is correct
7 Correct 146 ms 40964 KB Output is correct
8 Correct 122 ms 37208 KB Output is correct
9 Correct 88 ms 44668 KB Output is correct
10 Correct 137 ms 39944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 66 ms 30576 KB Output is correct
6 Correct 41 ms 37464 KB Output is correct
7 Correct 68 ms 35156 KB Output is correct
8 Correct 65 ms 30516 KB Output is correct
9 Correct 65 ms 33696 KB Output is correct
10 Correct 60 ms 30704 KB Output is correct
11 Correct 3 ms 11096 KB Output is correct
12 Correct 3 ms 11096 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11096 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 3 ms 11244 KB Output is correct
17 Correct 3 ms 11096 KB Output is correct
18 Correct 2 ms 11100 KB Output is correct
19 Correct 3 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 66 ms 30576 KB Output is correct
6 Correct 41 ms 37464 KB Output is correct
7 Correct 68 ms 35156 KB Output is correct
8 Correct 65 ms 30516 KB Output is correct
9 Correct 65 ms 33696 KB Output is correct
10 Correct 60 ms 30704 KB Output is correct
11 Correct 2 ms 11096 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 2 ms 11108 KB Output is correct
14 Correct 93 ms 44672 KB Output is correct
15 Correct 110 ms 44820 KB Output is correct
16 Correct 92 ms 44828 KB Output is correct
17 Correct 103 ms 44556 KB Output is correct
18 Correct 95 ms 44560 KB Output is correct
19 Correct 89 ms 44628 KB Output is correct
20 Correct 93 ms 44628 KB Output is correct
21 Correct 13 ms 13456 KB Output is correct
22 Correct 98 ms 44884 KB Output is correct
23 Correct 107 ms 44980 KB Output is correct
24 Correct 82 ms 44880 KB Output is correct
25 Correct 97 ms 45092 KB Output is correct
26 Correct 82 ms 44936 KB Output is correct
27 Correct 96 ms 44828 KB Output is correct
28 Correct 94 ms 44884 KB Output is correct
29 Correct 83 ms 44880 KB Output is correct
30 Correct 99 ms 44880 KB Output is correct
31 Correct 114 ms 37080 KB Output is correct
32 Correct 79 ms 44624 KB Output is correct
33 Correct 173 ms 41868 KB Output is correct
34 Correct 95 ms 37384 KB Output is correct
35 Correct 172 ms 41368 KB Output is correct
36 Correct 96 ms 37572 KB Output is correct
37 Correct 146 ms 40964 KB Output is correct
38 Correct 122 ms 37208 KB Output is correct
39 Correct 88 ms 44668 KB Output is correct
40 Correct 137 ms 39944 KB Output is correct
41 Correct 3 ms 11096 KB Output is correct
42 Correct 3 ms 11096 KB Output is correct
43 Correct 3 ms 11100 KB Output is correct
44 Correct 3 ms 11096 KB Output is correct
45 Correct 3 ms 11100 KB Output is correct
46 Correct 3 ms 11244 KB Output is correct
47 Correct 3 ms 11096 KB Output is correct
48 Correct 2 ms 11100 KB Output is correct
49 Correct 3 ms 11100 KB Output is correct
50 Correct 2 ms 11100 KB Output is correct
51 Correct 139 ms 37520 KB Output is correct
52 Correct 100 ms 45060 KB Output is correct
53 Correct 148 ms 40472 KB Output is correct
54 Correct 96 ms 37448 KB Output is correct
55 Correct 121 ms 37348 KB Output is correct
56 Correct 106 ms 44888 KB Output is correct
57 Correct 141 ms 41240 KB Output is correct
58 Correct 102 ms 37636 KB Output is correct
59 Correct 139 ms 37344 KB Output is correct
60 Correct 95 ms 45040 KB Output is correct
61 Correct 150 ms 41316 KB Output is correct
62 Correct 109 ms 37720 KB Output is correct
63 Correct 113 ms 37228 KB Output is correct
64 Correct 106 ms 44836 KB Output is correct
65 Correct 157 ms 40976 KB Output is correct
66 Correct 120 ms 37508 KB Output is correct
67 Correct 117 ms 37876 KB Output is correct
68 Correct 93 ms 44880 KB Output is correct
69 Correct 133 ms 40080 KB Output is correct
70 Correct 94 ms 37756 KB Output is correct