Submission #529997

# Submission time Handle Problem Language Result Execution time Memory
529997 2022-02-24T10:39:23 Z cig32 Election Campaign (JOI15_election_campaign) C++17
100 / 100
249 ms 71456 KB
#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
//#define int long long
 
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
vector<int> adj[MAXN];
int n, m;
vector<int> e;
int dep[MAXN];
int par[17][MAXN];
int pos[MAXN];
int sz[MAXN];
int nxt = 0;
void tour(int node, int prv) {
  e.push_back(node);
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  par[0][node] = prv;
  pos[node] = ++nxt;
  sz[node] = 1;
  for(int x: adj[node]) {
    if(x != prv) {
      tour(x, node);
      sz[node] += sz[x];
      e.push_back(node);
    }
  }
}
int l[MAXN], r[MAXN];
pair<int, int> sp[18][2*MAXN]; 
int lca_idx(int x, int y) {
  int m1 = min(l[x], l[y]);
  int m2 = max(r[x], r[y]);
  int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
  return min(sp[k][m1], sp[k][m2 - (1<<k) + 1]).second;
}
struct plan {
  int u, v, cost;
};
vector<plan> v[MAXN];
struct node {
  int upd = 0;
  int ans = 0;
} st[4*MAXN];
void u(int l, int r, int constl, int constr, int idx, int val) {
  if(l <= constl && constr <= r) {
    st[idx].upd += val;
    st[idx].ans += val;
    return;
  }
  if(st[idx].upd != 0) {
    st[2*idx+1].upd += st[idx].upd;
    st[2*idx+2].upd += st[idx].upd;
    st[2*idx+1].ans += st[idx].upd;
    st[2*idx+2].ans += st[idx].upd;
    st[idx].upd = 0;
    st[idx].ans = min(st[2*idx+1].ans, st[2*idx+2].ans);
  }
  int mid = (constl + constr) >> 1;
  if(mid < l || r < constl) u(l, r, mid+1, constr, 2*idx+2, val);
  else if(constr < l || r < mid + 1) u(l, r, constl, mid, 2*idx+1, val);
  else {
    u(l, r, constl, mid, 2*idx+1, val);
    u(l, r, mid+1, constr, 2*idx+2, val);
  }
  st[idx].ans = min(st[2*idx+1].ans, st[2*idx+2].ans);
}
int qu(int l, int r, int constl, int constr, int idx) {
  if(l <= constl && constr <= r) return st[idx].ans;
  if(st[idx].upd != 0) {
    st[2*idx+1].upd += st[idx].upd;
    st[2*idx+2].upd += st[idx].upd;
    st[2*idx+1].ans += st[idx].upd;
    st[2*idx+2].ans += st[idx].upd;
    st[idx].upd = 0;
    st[idx].ans = min(st[2*idx+1].ans, st[2*idx+2].ans);
  }
  int mid = (constl + constr) >> 1;
  if(mid < l || r < constl) return qu(l, r, mid+1, constr, 2*idx+2);
  else if(constr < l || r < mid+1) return qu(l, r, constl, mid, 2*idx+1);
  else return min(qu(l, r, constl, mid, 2*idx+1), qu(l, r, mid+1, constr, 2*idx+2));
}
void update(int l, int r, int v) {
  u(l, r, 0, MAXN-1, 0, v);
}
int query(int i) {
  return qu(pos[i], pos[i], 0, MAXN-1, 0);
}
int dpf(int node, int prv) {
  int sum = 0;
  unordered_map<int, int> contrib;
  for(int x: adj[node]) {
    if(x != prv) {
      int d = dpf(x, node);
      sum += d;
      contrib[x] = d;
    }
  }
  int ans = sum;
  if(v[node].size()) {
    for(plan f: v[node]) {
      if(f.u == node || f.v == node) {
        int dist = dep[(f.v == node ? f.u : f.v)] - dep[node] - 1;
        int cur = (f.u == node ? f.v : f.u);
        for(int k=16; k>=0; k--) {
          if(dist >= (1 << k)) {
            dist -= (1 << k);
            cur = par[k][cur];
          }
        }
        ans = max(ans, f.cost + sum - contrib[cur] + query(f.u ^ f.v ^ node));
      }
      else {
        int dist = dep[f.u] - dep[node] - 1;
        int cur = f.u;
        for(int k=16; k>=0; k--) {
          if(dist >= (1 << k)) {
            dist -= (1 << k);
            cur = par[k][cur];
          }
        }
        int dist2 = dep[f.v] - dep[node] - 1;
        int cur2 = f.v;
        for(int k=16; k>=0; k--) {
          if(dist2 >= (1 << k)) {
            dist2 -= (1 << k);
            cur2 = par[k][cur2];
          }
        }
        ans = max(ans, f.cost + sum - contrib[cur] - contrib[cur2] + query(f.u) + query(f.v));
      }
    }
  }
  for(int x: adj[node]) {
    if(x != prv) {
      update(pos[x], pos[x] + sz[x] - 1, sum - contrib[x]);
    }
  }
  update(pos[node], pos[node], sum);
  return ans;
}
void solve(int tc) {
  cin >> n;
  for(int i=1; i<n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  e.push_back(0);
  tour(1, -1);
  for(int i=1; i<e.size(); i++) {
    if(l[e[i]] == 0) l[e[i]] = i;
    r[e[i]] = i;
  }
  for(int i=1; i<e.size(); i++) sp[0][i] = {dep[e[i]], e[i]};
  for(int i=1; i<18; i++) {
    for(int j=1; j<e.size(); j++) {
      sp[i][j] = min(sp[i-1][j], sp[i-1][j + (1<< (i-1))]);
    }
  }
  for(int i=1; i<17; i++) {
    for(int j=1; j<=n; j++) {
      par[i][j] = par[i-1][par[i-1][j]];
    }
  }
  cin >> m;
  for(int i=1; i<=m; i++) {
    int q, w, z;
    cin >> q >> w >> z;
    int j = lca_idx(q, w);
    v[j].push_back({q, w, z});
  }
  cout << dpf(1, -1) << "\n";
}
int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1;// cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
/*
23
1 2
1 3
1 4
1 5
2 9
2 10
4 19
4 6
5 17
5 18
9 11
10 13
10 16
19 20
6 7
11 12
11 14
11 15
7 8
14 21
14 22
22 23
5
12 3 1
21 23 2
17 18 4
13 16 8
20 8 16
*/

Compilation message

election_campaign.cpp: In function 'void solve(int)':
election_campaign.cpp:156:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |   for(int i=1; i<e.size(); i++) {
      |                ~^~~~~~~~~
election_campaign.cpp:160:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |   for(int i=1; i<e.size(); i++) sp[0][i] = {dep[e[i]], e[i]};
      |                ~^~~~~~~~~
election_campaign.cpp:162:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |     for(int j=1; j<e.size(); j++) {
      |                  ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5156 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 140 ms 48356 KB Output is correct
6 Correct 111 ms 67740 KB Output is correct
7 Correct 151 ms 61304 KB Output is correct
8 Correct 136 ms 49880 KB Output is correct
9 Correct 124 ms 59092 KB Output is correct
10 Correct 107 ms 49740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 4 ms 5836 KB Output is correct
4 Correct 183 ms 68736 KB Output is correct
5 Correct 158 ms 68800 KB Output is correct
6 Correct 156 ms 68760 KB Output is correct
7 Correct 166 ms 68752 KB Output is correct
8 Correct 170 ms 68920 KB Output is correct
9 Correct 150 ms 68732 KB Output is correct
10 Correct 166 ms 68736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 4 ms 5836 KB Output is correct
4 Correct 183 ms 68736 KB Output is correct
5 Correct 158 ms 68800 KB Output is correct
6 Correct 156 ms 68760 KB Output is correct
7 Correct 166 ms 68752 KB Output is correct
8 Correct 170 ms 68920 KB Output is correct
9 Correct 150 ms 68732 KB Output is correct
10 Correct 166 ms 68736 KB Output is correct
11 Correct 19 ms 6624 KB Output is correct
12 Correct 166 ms 68788 KB Output is correct
13 Correct 173 ms 68788 KB Output is correct
14 Correct 169 ms 68792 KB Output is correct
15 Correct 175 ms 68776 KB Output is correct
16 Correct 168 ms 68820 KB Output is correct
17 Correct 164 ms 68812 KB Output is correct
18 Correct 165 ms 68812 KB Output is correct
19 Correct 225 ms 68824 KB Output is correct
20 Correct 166 ms 68788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 50232 KB Output is correct
2 Correct 182 ms 71040 KB Output is correct
3 Correct 220 ms 65900 KB Output is correct
4 Correct 223 ms 52572 KB Output is correct
5 Correct 209 ms 64452 KB Output is correct
6 Correct 190 ms 52784 KB Output is correct
7 Correct 243 ms 63964 KB Output is correct
8 Correct 188 ms 52196 KB Output is correct
9 Correct 201 ms 70948 KB Output is correct
10 Correct 227 ms 60860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5156 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 140 ms 48356 KB Output is correct
6 Correct 111 ms 67740 KB Output is correct
7 Correct 151 ms 61304 KB Output is correct
8 Correct 136 ms 49880 KB Output is correct
9 Correct 124 ms 59092 KB Output is correct
10 Correct 107 ms 49740 KB Output is correct
11 Correct 5 ms 5664 KB Output is correct
12 Correct 5 ms 5860 KB Output is correct
13 Correct 4 ms 5804 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 4 ms 5660 KB Output is correct
16 Correct 4 ms 5708 KB Output is correct
17 Correct 5 ms 5708 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 6 ms 5708 KB Output is correct
20 Correct 5 ms 5936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5156 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 140 ms 48356 KB Output is correct
6 Correct 111 ms 67740 KB Output is correct
7 Correct 151 ms 61304 KB Output is correct
8 Correct 136 ms 49880 KB Output is correct
9 Correct 124 ms 59092 KB Output is correct
10 Correct 107 ms 49740 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 3 ms 5280 KB Output is correct
13 Correct 4 ms 5836 KB Output is correct
14 Correct 183 ms 68736 KB Output is correct
15 Correct 158 ms 68800 KB Output is correct
16 Correct 156 ms 68760 KB Output is correct
17 Correct 166 ms 68752 KB Output is correct
18 Correct 170 ms 68920 KB Output is correct
19 Correct 150 ms 68732 KB Output is correct
20 Correct 166 ms 68736 KB Output is correct
21 Correct 19 ms 6624 KB Output is correct
22 Correct 166 ms 68788 KB Output is correct
23 Correct 173 ms 68788 KB Output is correct
24 Correct 169 ms 68792 KB Output is correct
25 Correct 175 ms 68776 KB Output is correct
26 Correct 168 ms 68820 KB Output is correct
27 Correct 164 ms 68812 KB Output is correct
28 Correct 165 ms 68812 KB Output is correct
29 Correct 225 ms 68824 KB Output is correct
30 Correct 166 ms 68788 KB Output is correct
31 Correct 220 ms 50232 KB Output is correct
32 Correct 182 ms 71040 KB Output is correct
33 Correct 220 ms 65900 KB Output is correct
34 Correct 223 ms 52572 KB Output is correct
35 Correct 209 ms 64452 KB Output is correct
36 Correct 190 ms 52784 KB Output is correct
37 Correct 243 ms 63964 KB Output is correct
38 Correct 188 ms 52196 KB Output is correct
39 Correct 201 ms 70948 KB Output is correct
40 Correct 227 ms 60860 KB Output is correct
41 Correct 5 ms 5664 KB Output is correct
42 Correct 5 ms 5860 KB Output is correct
43 Correct 4 ms 5804 KB Output is correct
44 Correct 4 ms 5708 KB Output is correct
45 Correct 4 ms 5660 KB Output is correct
46 Correct 4 ms 5708 KB Output is correct
47 Correct 5 ms 5708 KB Output is correct
48 Correct 4 ms 5836 KB Output is correct
49 Correct 6 ms 5708 KB Output is correct
50 Correct 5 ms 5936 KB Output is correct
51 Correct 197 ms 52404 KB Output is correct
52 Correct 168 ms 71264 KB Output is correct
53 Correct 249 ms 62096 KB Output is correct
54 Correct 173 ms 52736 KB Output is correct
55 Correct 196 ms 52336 KB Output is correct
56 Correct 170 ms 71208 KB Output is correct
57 Correct 201 ms 62220 KB Output is correct
58 Correct 183 ms 52852 KB Output is correct
59 Correct 219 ms 52400 KB Output is correct
60 Correct 163 ms 71268 KB Output is correct
61 Correct 237 ms 62536 KB Output is correct
62 Correct 179 ms 52592 KB Output is correct
63 Correct 215 ms 52600 KB Output is correct
64 Correct 180 ms 71276 KB Output is correct
65 Correct 222 ms 62308 KB Output is correct
66 Correct 165 ms 52660 KB Output is correct
67 Correct 211 ms 52624 KB Output is correct
68 Correct 206 ms 71456 KB Output is correct
69 Correct 248 ms 60024 KB Output is correct
70 Correct 176 ms 52744 KB Output is correct