Submission #104791

# Submission time Handle Problem Language Result Execution time Memory
104791 2019-04-09T09:22:33 Z IOrtroiii Designated Cities (JOI19_designated_cities) C++14
100 / 100
735 ms 57172 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200200;

vector< tuple<int, int, int> > g[N];
long long md[N];
long long sm[N];
long long up[N];

void dfs_down(int u,int p) {
  for (auto e : g[u]) {
    int v, wf, wb;
    tie(v, wf, wb) = e;
    if (v == p) continue;
    dfs_down(v, u);
    md[u] = max(md[u], md[v] + wf);
    sm[u] += wf + sm[v];
  }
}

void dfs_up(int u,int p) {
  md[u] = max(md[u], up[u]);
  pair<long long, long long> mx(up[u], 0);
  for (auto e : g[u]) {
    int v, wf, wb;
    tie(v, wf, wb) = e;
    if (v == p) continue;
    long long nw = md[v] + wf;
    if (nw > mx.second) {
      mx.second = nw;
    }
    if (mx.first < mx.second) {
      swap(mx.first, mx.second);
    }
  }
  for (auto e : g[u]){
    int v, wf, wb;
    tie(v, wf, wb) = e;
    if (v == p) continue;
    long long nw = md[v] + wf;
    up[v] = wb + (nw == mx.first ? mx.second : mx.first);
    sm[v] = sm[u] + wb - wf;
    dfs_up(v, u);
  }
}

int ver[N], tin[N], tout[N], tt;
int par[N];
int parw[N];
long long d[N];

void dfs(int u,int p) {
  ver[tin[u] = ++tt] = u;
  for (auto e : g[u]) {
    int v, wf, wb;
    tie(v, wf, wb) = e;
    if (v == p) continue;
    par[v] = u;
    parw[v] = wf;
    d[v] = d[u] + wf;
    dfs(v, u);
  }
  tout[u] = tt;
}

pair<long long, int> t[N << 2];
long long lz[N << 2];

void build(int v,int l,int r) {
  if (l == r) {
    t[v] = {d[ver[l]], ver[l]};
    return;
  }
  int md = (l + r) >> 1;
  build(v << 1, l, md);
  build(v << 1 | 1, md + 1, r);
  t[v] = max(t[v << 1], t[v << 1 | 1]);
}

void push(int v,int l,int r) {
  if (lz[v]) {
    t[v].first += lz[v];
    if (l < r) {
      lz[v << 1] += lz[v];
      lz[v << 1 | 1] += lz[v];
    }
    lz[v] = 0;
  }
}

void add(int v,int l,int r,int L,int R,int val) {
  push(v, l, r);
  if (L > r || R < l) return;
  if (L <= l && r <= R) {
    lz[v] += val;
    push(v, l, r);
    return;
  }
  int md = (l + r) >> 1;
  add(v << 1, l, md, L, R, val);
  add(v << 1 | 1, md + 1, r, L, R, val);
  t[v] = max(t[v << 1], t[v << 1 | 1]);
}

bool visit[N];
long long ans[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v, x, y;
    scanf("%d %d %d %d", &u, &v, &x, &y);
    g[u].push_back(make_tuple(v, x, y));
    g[v].push_back(make_tuple(u, y, x));
  }
  dfs_down(1, -1);
  dfs_up(1, -1);
  ans[1] = *min_element(sm + 1, sm + 1 + n);
  int rt = 1;
  for (int i = 2; i <= n; ++i) {
    if (sm[i] - md[i] < sm[rt] - md[rt]) rt = i;
  }
  dfs(rt, -1);
  build(1, 1, n);
  visit[rt] = true;
  long long now = sm[rt];
  for (int i = 2; i <= n; ++i) {
    now -= t[1].first;
    ans[i] = now;
    int v = t[1].second;
    while (!visit[v]) {
      visit[v] = true;
      add(1, 1, n, tin[v], tout[v], -parw[v]);
      v = par[v];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int v;
    scanf("%d", &v);
    printf("%lld\n", ans[v]);
  }
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
designated_cities.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d %d", &u, &v, &x, &y);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &q);
   ~~~~~^~~~~~~~~~
designated_cities.cpp:144:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &v);
     ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5152 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5040 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5092 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 16 ms 5124 KB Output is correct
10 Correct 8 ms 5076 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 673 ms 37764 KB Output is correct
3 Correct 589 ms 48248 KB Output is correct
4 Correct 597 ms 37604 KB Output is correct
5 Correct 604 ms 37412 KB Output is correct
6 Correct 677 ms 39316 KB Output is correct
7 Correct 566 ms 37352 KB Output is correct
8 Correct 671 ms 49096 KB Output is correct
9 Correct 500 ms 38064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 602 ms 37656 KB Output is correct
3 Correct 626 ms 50608 KB Output is correct
4 Correct 585 ms 37500 KB Output is correct
5 Correct 727 ms 37600 KB Output is correct
6 Correct 657 ms 39624 KB Output is correct
7 Correct 581 ms 37884 KB Output is correct
8 Correct 735 ms 45328 KB Output is correct
9 Correct 507 ms 38032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5152 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5040 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5092 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 16 ms 5124 KB Output is correct
10 Correct 8 ms 5076 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 10 ms 5504 KB Output is correct
14 Correct 10 ms 5504 KB Output is correct
15 Correct 10 ms 5504 KB Output is correct
16 Correct 13 ms 5504 KB Output is correct
17 Correct 10 ms 5504 KB Output is correct
18 Correct 9 ms 5504 KB Output is correct
19 Correct 11 ms 5504 KB Output is correct
20 Correct 11 ms 5504 KB Output is correct
21 Correct 10 ms 5504 KB Output is correct
22 Correct 10 ms 5504 KB Output is correct
23 Correct 11 ms 5504 KB Output is correct
24 Correct 10 ms 5504 KB Output is correct
25 Correct 10 ms 5632 KB Output is correct
26 Correct 12 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 673 ms 37764 KB Output is correct
3 Correct 589 ms 48248 KB Output is correct
4 Correct 597 ms 37604 KB Output is correct
5 Correct 604 ms 37412 KB Output is correct
6 Correct 677 ms 39316 KB Output is correct
7 Correct 566 ms 37352 KB Output is correct
8 Correct 671 ms 49096 KB Output is correct
9 Correct 500 ms 38064 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 602 ms 37656 KB Output is correct
12 Correct 626 ms 50608 KB Output is correct
13 Correct 585 ms 37500 KB Output is correct
14 Correct 727 ms 37600 KB Output is correct
15 Correct 657 ms 39624 KB Output is correct
16 Correct 581 ms 37884 KB Output is correct
17 Correct 735 ms 45328 KB Output is correct
18 Correct 507 ms 38032 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 676 ms 37564 KB Output is correct
21 Correct 627 ms 57172 KB Output is correct
22 Correct 606 ms 42704 KB Output is correct
23 Correct 635 ms 44100 KB Output is correct
24 Correct 662 ms 42972 KB Output is correct
25 Correct 651 ms 44036 KB Output is correct
26 Correct 647 ms 42968 KB Output is correct
27 Correct 684 ms 43928 KB Output is correct
28 Correct 693 ms 45912 KB Output is correct
29 Correct 642 ms 43976 KB Output is correct
30 Correct 599 ms 43100 KB Output is correct
31 Correct 646 ms 43984 KB Output is correct
32 Correct 712 ms 52148 KB Output is correct
33 Correct 570 ms 44448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5152 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5040 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5092 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 16 ms 5124 KB Output is correct
10 Correct 8 ms 5076 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 5120 KB Output is correct
13 Correct 673 ms 37764 KB Output is correct
14 Correct 589 ms 48248 KB Output is correct
15 Correct 597 ms 37604 KB Output is correct
16 Correct 604 ms 37412 KB Output is correct
17 Correct 677 ms 39316 KB Output is correct
18 Correct 566 ms 37352 KB Output is correct
19 Correct 671 ms 49096 KB Output is correct
20 Correct 500 ms 38064 KB Output is correct
21 Correct 6 ms 5120 KB Output is correct
22 Correct 602 ms 37656 KB Output is correct
23 Correct 626 ms 50608 KB Output is correct
24 Correct 585 ms 37500 KB Output is correct
25 Correct 727 ms 37600 KB Output is correct
26 Correct 657 ms 39624 KB Output is correct
27 Correct 581 ms 37884 KB Output is correct
28 Correct 735 ms 45328 KB Output is correct
29 Correct 507 ms 38032 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 10 ms 5504 KB Output is correct
32 Correct 10 ms 5504 KB Output is correct
33 Correct 10 ms 5504 KB Output is correct
34 Correct 13 ms 5504 KB Output is correct
35 Correct 10 ms 5504 KB Output is correct
36 Correct 9 ms 5504 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 11 ms 5504 KB Output is correct
39 Correct 10 ms 5504 KB Output is correct
40 Correct 10 ms 5504 KB Output is correct
41 Correct 11 ms 5504 KB Output is correct
42 Correct 10 ms 5504 KB Output is correct
43 Correct 10 ms 5632 KB Output is correct
44 Correct 12 ms 5504 KB Output is correct
45 Correct 7 ms 5120 KB Output is correct
46 Correct 676 ms 37564 KB Output is correct
47 Correct 627 ms 57172 KB Output is correct
48 Correct 606 ms 42704 KB Output is correct
49 Correct 635 ms 44100 KB Output is correct
50 Correct 662 ms 42972 KB Output is correct
51 Correct 651 ms 44036 KB Output is correct
52 Correct 647 ms 42968 KB Output is correct
53 Correct 684 ms 43928 KB Output is correct
54 Correct 693 ms 45912 KB Output is correct
55 Correct 642 ms 43976 KB Output is correct
56 Correct 599 ms 43100 KB Output is correct
57 Correct 646 ms 43984 KB Output is correct
58 Correct 712 ms 52148 KB Output is correct
59 Correct 570 ms 44448 KB Output is correct
60 Correct 7 ms 5120 KB Output is correct
61 Correct 726 ms 46688 KB Output is correct
62 Correct 679 ms 56648 KB Output is correct
63 Correct 563 ms 45408 KB Output is correct
64 Correct 638 ms 46936 KB Output is correct
65 Correct 666 ms 45432 KB Output is correct
66 Correct 674 ms 46712 KB Output is correct
67 Correct 695 ms 45444 KB Output is correct
68 Correct 672 ms 46676 KB Output is correct
69 Correct 703 ms 48136 KB Output is correct
70 Correct 647 ms 46300 KB Output is correct
71 Correct 679 ms 45836 KB Output is correct
72 Correct 657 ms 47144 KB Output is correct
73 Correct 659 ms 53212 KB Output is correct
74 Correct 527 ms 48604 KB Output is correct