Submission #897387

# Submission time Handle Problem Language Result Execution time Memory
897387 2024-01-03T03:17:13 Z NeroZein Designated Cities (JOI19_designated_cities) C++17
100 / 100
306 ms 52304 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 2e5 + 5;

int n;
int cnt;
int euler[N];
bool active[N];
long long f[N];
long long ans[N];
int in[N], out[N];
long long dist[N];
pair<int, int> pr[N];
long long seg[N * 2];
long long lazy[N * 2];
vector<pair<int, int>> g[N];
pair<long long, long long> dp[N];

void dfs1(int v, int p) {
  long long mx = 0, mx2 = 0;
  for (auto [u, w] : g[v]) {
    if (u == p) continue;
    dist[1] += w;
    dfs1(u, v); 
    mx2 = max(mx2, dp[u].first + w); 
    if (mx2 > mx) swap(mx, mx2);
  }
  dp[v] = make_pair(mx, mx2);
}

void dfs2(int v, int p, long long up) {
  auto [mx, mx2] = dp[v];
  for (auto [u, w] : g[v]) {
    if (u == p) {
      dist[v] += w;
      up += w; 
    }
  }
  f[v] = max(mx, up);
  for (auto [u, w] : g[v]) {
    if (u == p) continue;
    dist[u] = dist[v] - w; 
    long long nup = (mx == dp[u].first + w ? mx2 : mx);
    nup = max(up, nup);
    dfs2(u, v, nup);
  }
}

void push(int nd, int l, int r) {
  if (!lazy[nd]) return;
  seg[nd] += lazy[nd];
  if (l != r) {
    int mid = (l + r) >> 1;
    int rs = nd + ((mid - l + 1) << 1);
    lazy[nd + 1] += lazy[nd];
    lazy[rs] += lazy[nd];    
  }
  lazy[nd] = 0; 
}

void upd(int nd, int l, int r, int s, int e, int v) {
  push(nd, l, r);
  if (l >= s && r <= e) {
    lazy[nd] = v; 
    push(nd, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (mid >= e) {
    push(rs, mid + 1, r);
    upd(nd + 1, l, mid, s, e, v); 
  } else {
    if (mid < s) {
      push(nd + 1, l, mid);
      upd(rs, mid + 1, r, s, e, v); 
    } else {
      upd(nd + 1, l, mid, s, e, v);
      upd(rs, mid + 1, r, s, e, v);       
    }
  }
  seg[nd] = max(seg[nd + 1], seg[rs]); 
}

int get(int nd, int l, int r) {
  push(nd, l, r); 
  if (l == r) {
    return euler[l]; 
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  push(nd + 1, l, mid);
  push(rs, mid + 1, r); 
  if (seg[nd + 1] > seg[rs]) {
    return get(nd + 1, l, mid);
  }
  return get(rs, mid + 1, r); 
}

int find(int v, int p, long long tar, long long d = 0) {
  if (d == tar) {
    return v;
  }
  for (auto [u, w] : g[v]) {
    if (u == p) continue;
    int ret = find(u, v, tar, d + w);
    if (ret != -1) return ret; 
  }
  return -1; 
}

bool dfs3(int v, int p, int o) {
  euler[cnt] = v; 
  in[v] = cnt++;
  bool ret = o == v;
  for (auto [u, w] : g[v]) {
    if (u == p) continue;
    pr[u] = make_pair(v, w);
    bool ru = dfs3(u, v, o);
    if (!ru) {
      upd(0, 0, n - 1, in[u], out[u], w);
    } else {
      ret = true;
    }
  }
  out[v] = cnt - 1;
  active[v] = ret;
  return ret; 
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v, c, d;
    cin >> u >> v >> c >> d;
    g[u].emplace_back(v, c);
    g[v].emplace_back(u, d); 
  }
  dfs1(1, 0);
  dfs2(1, 0, 0);
  ans[1] = *min_element(dist + 1, dist + 1 + n);
  ans[2] = LLONG_MAX;
  dist[0] = LLONG_MAX;
  int r = 0; 
  for (int i = 1; i <= n; ++i) {
    if (dist[i] - f[i] < dist[r] - f[r]) {
      r = i;
    }
  }
  int o = find(r, r, f[r]);
  ans[2] = dist[r] - f[r];
  dfs3(r, r, o);
  for (int i = 3; i <= n; ++i) {
    if (!seg[0]) break;
    ans[i] = ans[i - 1] - seg[0];
    int v = get(0, 0, n - 1);
    while (!active[v]) {
      active[v] = true;
      auto [pv, pw] = pr[v];
      upd(0, 0, n - 1, in[v], out[v], -pw);
      v = pv; 
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int e;
    cin >> e;
    cout << ans[e] << '\n'; 
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 18920 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 16824 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 276 ms 30528 KB Output is correct
3 Correct 221 ms 44144 KB Output is correct
4 Correct 249 ms 30656 KB Output is correct
5 Correct 269 ms 30468 KB Output is correct
6 Correct 272 ms 32900 KB Output is correct
7 Correct 285 ms 30664 KB Output is correct
8 Correct 223 ms 47408 KB Output is correct
9 Correct 231 ms 30928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 266 ms 30668 KB Output is correct
3 Correct 232 ms 44180 KB Output is correct
4 Correct 270 ms 30568 KB Output is correct
5 Correct 263 ms 30412 KB Output is correct
6 Correct 253 ms 33420 KB Output is correct
7 Correct 225 ms 30908 KB Output is correct
8 Correct 251 ms 42324 KB Output is correct
9 Correct 226 ms 31416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 18920 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 16824 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 ms 16728 KB Output is correct
13 Correct 5 ms 19032 KB Output is correct
14 Correct 4 ms 16988 KB Output is correct
15 Correct 4 ms 19092 KB Output is correct
16 Correct 4 ms 19036 KB Output is correct
17 Correct 5 ms 19248 KB Output is correct
18 Correct 4 ms 19036 KB Output is correct
19 Correct 5 ms 19036 KB Output is correct
20 Correct 4 ms 18916 KB Output is correct
21 Correct 4 ms 19032 KB Output is correct
22 Correct 5 ms 19036 KB Output is correct
23 Correct 5 ms 19036 KB Output is correct
24 Correct 4 ms 19036 KB Output is correct
25 Correct 4 ms 19260 KB Output is correct
26 Correct 4 ms 19032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 276 ms 30528 KB Output is correct
3 Correct 221 ms 44144 KB Output is correct
4 Correct 249 ms 30656 KB Output is correct
5 Correct 269 ms 30468 KB Output is correct
6 Correct 272 ms 32900 KB Output is correct
7 Correct 285 ms 30664 KB Output is correct
8 Correct 223 ms 47408 KB Output is correct
9 Correct 231 ms 30928 KB Output is correct
10 Correct 2 ms 16732 KB Output is correct
11 Correct 266 ms 30668 KB Output is correct
12 Correct 232 ms 44180 KB Output is correct
13 Correct 270 ms 30568 KB Output is correct
14 Correct 263 ms 30412 KB Output is correct
15 Correct 253 ms 33420 KB Output is correct
16 Correct 225 ms 30908 KB Output is correct
17 Correct 251 ms 42324 KB Output is correct
18 Correct 226 ms 31416 KB Output is correct
19 Correct 2 ms 16732 KB Output is correct
20 Correct 284 ms 30656 KB Output is correct
21 Correct 189 ms 44372 KB Output is correct
22 Correct 241 ms 30660 KB Output is correct
23 Correct 285 ms 37096 KB Output is correct
24 Correct 256 ms 35916 KB Output is correct
25 Correct 263 ms 37108 KB Output is correct
26 Correct 263 ms 35920 KB Output is correct
27 Correct 250 ms 36928 KB Output is correct
28 Correct 279 ms 39764 KB Output is correct
29 Correct 263 ms 36948 KB Output is correct
30 Correct 274 ms 36068 KB Output is correct
31 Correct 230 ms 37060 KB Output is correct
32 Correct 263 ms 48976 KB Output is correct
33 Correct 228 ms 37604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 3 ms 18920 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 3 ms 18780 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 16824 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 276 ms 30528 KB Output is correct
14 Correct 221 ms 44144 KB Output is correct
15 Correct 249 ms 30656 KB Output is correct
16 Correct 269 ms 30468 KB Output is correct
17 Correct 272 ms 32900 KB Output is correct
18 Correct 285 ms 30664 KB Output is correct
19 Correct 223 ms 47408 KB Output is correct
20 Correct 231 ms 30928 KB Output is correct
21 Correct 2 ms 16732 KB Output is correct
22 Correct 266 ms 30668 KB Output is correct
23 Correct 232 ms 44180 KB Output is correct
24 Correct 270 ms 30568 KB Output is correct
25 Correct 263 ms 30412 KB Output is correct
26 Correct 253 ms 33420 KB Output is correct
27 Correct 225 ms 30908 KB Output is correct
28 Correct 251 ms 42324 KB Output is correct
29 Correct 226 ms 31416 KB Output is correct
30 Correct 3 ms 16728 KB Output is correct
31 Correct 5 ms 19032 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 19092 KB Output is correct
34 Correct 4 ms 19036 KB Output is correct
35 Correct 5 ms 19248 KB Output is correct
36 Correct 4 ms 19036 KB Output is correct
37 Correct 5 ms 19036 KB Output is correct
38 Correct 4 ms 18916 KB Output is correct
39 Correct 4 ms 19032 KB Output is correct
40 Correct 5 ms 19036 KB Output is correct
41 Correct 5 ms 19036 KB Output is correct
42 Correct 4 ms 19036 KB Output is correct
43 Correct 4 ms 19260 KB Output is correct
44 Correct 4 ms 19032 KB Output is correct
45 Correct 2 ms 16732 KB Output is correct
46 Correct 284 ms 30656 KB Output is correct
47 Correct 189 ms 44372 KB Output is correct
48 Correct 241 ms 30660 KB Output is correct
49 Correct 285 ms 37096 KB Output is correct
50 Correct 256 ms 35916 KB Output is correct
51 Correct 263 ms 37108 KB Output is correct
52 Correct 263 ms 35920 KB Output is correct
53 Correct 250 ms 36928 KB Output is correct
54 Correct 279 ms 39764 KB Output is correct
55 Correct 263 ms 36948 KB Output is correct
56 Correct 274 ms 36068 KB Output is correct
57 Correct 230 ms 37060 KB Output is correct
58 Correct 263 ms 48976 KB Output is correct
59 Correct 228 ms 37604 KB Output is correct
60 Correct 2 ms 16732 KB Output is correct
61 Correct 303 ms 39676 KB Output is correct
62 Correct 222 ms 52304 KB Output is correct
63 Correct 273 ms 38300 KB Output is correct
64 Correct 306 ms 39528 KB Output is correct
65 Correct 276 ms 38340 KB Output is correct
66 Correct 285 ms 39540 KB Output is correct
67 Correct 292 ms 38216 KB Output is correct
68 Correct 284 ms 39656 KB Output is correct
69 Correct 288 ms 42076 KB Output is correct
70 Correct 286 ms 39452 KB Output is correct
71 Correct 284 ms 38856 KB Output is correct
72 Correct 279 ms 40008 KB Output is correct
73 Correct 296 ms 51280 KB Output is correct
74 Correct 250 ms 41684 KB Output is correct