Submission #995519

# Submission time Handle Problem Language Result Execution time Memory
995519 2024-06-09T08:51:08 Z abczz Construction of Highway (JOI18_construction) C++17
100 / 100
1036 ms 37432 KB
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#define ll long long

using namespace std;

vector <ll> adj[100000];
array <ll, 2> edge[100000];
vector <ll> dfn;
vector <array<ll, 2>> V;
array <ll, 2> R[100000];
ll n, a, b, x, l, r, d, t, f, C[100000], D[100000], T[100000], bl[100000][17];
struct SegTree{
  vector <ll> st{vector <ll>(400000, 0)};
  void update(ll id, ll l, ll r, ll q) {
    if (l == r) {
      st[id] = T[dfn[l]];
      return;
    }
    ll mid = (l+r)/2;
    if (q <= mid) update(id*2, l, mid, q);
    else update(id*2+1, mid+1, r, q);
    st[id] = max(st[id*2], st[id*2+1]);
  }
  ll query(ll id, ll l, ll r, ll ql, ll qr) {
    if (qr < l || r < ql) return -1e18;
    else if (ql <= l && r <= qr) return st[id];
    ll mid = (l+r)/2;
    return max(query(id*2, l, mid, ql, qr), query(id*2+1, mid+1, r, ql, qr));
  }
}ST;

void merge_sort(ll l, ll r) {
  if (l == r) return;
  ll mid = (l+r)/2;
  merge_sort(l, mid);
  merge_sort(mid+1, r);
  vector <array<ll, 2>> tmp;
  ll i = l, j = mid+1, s = 0;
  while (i <= mid && j <= r) {
    if (V[i][0] <= V[j][0]) {
      f += V[i][1] * s;
      tmp.push_back(V[i++]);
    }
    else {
      s += V[j][1];
      tmp.push_back(V[j++]);
    }
  }
  while (i <= mid) {
    f += V[i][1] * s;
    tmp.push_back(V[i++]);
  }
  while (j <= r) tmp.push_back(V[j++]);
  for (int i=l; i<=r; ++i) V[i] = tmp[i-l];
}
void dfs(ll u) {
  R[u][0] = dfn.size();
  dfn.push_back(u);
  for (auto v : adj[u]) {
    D[v] = D[u] + 1;
    dfs(v);
  }
  R[u][1] = dfn.size()-1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i=0; i<n; ++i) {
    cin >> C[i];
  }
  for (int i=1; i<n; ++i) {
    cin >> edge[i][0] >> edge[i][1];
    --edge[i][0], --edge[i][1];
    T[edge[i][1]] = i;
    bl[edge[i][1]][0] = edge[i][0];
    adj[edge[i][0]].push_back(edge[i][1]);
  }
  dfs(0);
  for (int j=1; j<17; ++j) {
    for (int i=0; i<n; ++i) {
      bl[i][j] = bl[bl[i][j-1]][j-1];
    }
  }
  for (int i=1; i<n; ++i) {
    V.clear();
    a = edge[i][0];
    while (true) {
      t = ST.query(1, 0, dfn.size()-1, R[a][0], R[a][1]);
      b = a;
      for (int j=16; j>=0; --j) {
        x = ST.query(1, 0, dfn.size()-1, R[bl[b][j]][0], R[bl[b][j]][1]);
        if (x == t) b = bl[b][j];
      }
      V.push_back({C[edge[t][1]], D[a]-D[b]+1});
      if (!b) break;
      else a = bl[b][0];
    }
    f = 0;
    reverse(V.begin(), V.end());
    merge_sort(0, V.size()-1);
    cout << f << '\n';
    ST.update(1, 0, dfn.size()-1, R[edge[i][1]][0]);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5976 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 3 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6232 KB Output is correct
14 Correct 3 ms 6236 KB Output is correct
15 Correct 3 ms 6236 KB Output is correct
16 Correct 5 ms 6236 KB Output is correct
17 Correct 3 ms 6236 KB Output is correct
18 Correct 3 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6136 KB Output is correct
21 Correct 3 ms 6236 KB Output is correct
22 Correct 3 ms 6236 KB Output is correct
23 Correct 3 ms 6236 KB Output is correct
24 Correct 3 ms 6236 KB Output is correct
25 Correct 3 ms 6236 KB Output is correct
26 Correct 3 ms 6136 KB Output is correct
27 Correct 3 ms 6236 KB Output is correct
28 Correct 2 ms 6280 KB Output is correct
29 Correct 4 ms 6232 KB Output is correct
30 Correct 3 ms 6232 KB Output is correct
31 Correct 3 ms 6132 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 3 ms 6232 KB Output is correct
34 Correct 3 ms 6240 KB Output is correct
35 Correct 3 ms 6128 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 2 ms 6068 KB Output is correct
39 Correct 2 ms 6236 KB Output is correct
40 Correct 2 ms 6236 KB Output is correct
41 Correct 3 ms 6236 KB Output is correct
42 Correct 2 ms 6236 KB Output is correct
43 Correct 3 ms 6236 KB Output is correct
44 Correct 3 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5976 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 3 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6232 KB Output is correct
14 Correct 3 ms 6236 KB Output is correct
15 Correct 3 ms 6236 KB Output is correct
16 Correct 5 ms 6236 KB Output is correct
17 Correct 3 ms 6236 KB Output is correct
18 Correct 3 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6136 KB Output is correct
21 Correct 3 ms 6236 KB Output is correct
22 Correct 3 ms 6236 KB Output is correct
23 Correct 3 ms 6236 KB Output is correct
24 Correct 3 ms 6236 KB Output is correct
25 Correct 3 ms 6236 KB Output is correct
26 Correct 3 ms 6136 KB Output is correct
27 Correct 3 ms 6236 KB Output is correct
28 Correct 2 ms 6280 KB Output is correct
29 Correct 4 ms 6232 KB Output is correct
30 Correct 3 ms 6232 KB Output is correct
31 Correct 3 ms 6132 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 3 ms 6232 KB Output is correct
34 Correct 3 ms 6240 KB Output is correct
35 Correct 3 ms 6128 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 2 ms 6068 KB Output is correct
39 Correct 2 ms 6236 KB Output is correct
40 Correct 2 ms 6236 KB Output is correct
41 Correct 3 ms 6236 KB Output is correct
42 Correct 2 ms 6236 KB Output is correct
43 Correct 3 ms 6236 KB Output is correct
44 Correct 3 ms 6236 KB Output is correct
45 Correct 4 ms 6232 KB Output is correct
46 Correct 10 ms 7000 KB Output is correct
47 Correct 11 ms 7124 KB Output is correct
48 Correct 9 ms 7004 KB Output is correct
49 Correct 4 ms 7260 KB Output is correct
50 Correct 5 ms 7260 KB Output is correct
51 Correct 6 ms 7260 KB Output is correct
52 Correct 7 ms 7260 KB Output is correct
53 Correct 7 ms 7260 KB Output is correct
54 Correct 7 ms 7236 KB Output is correct
55 Correct 7 ms 7260 KB Output is correct
56 Correct 7 ms 7236 KB Output is correct
57 Correct 18 ms 7088 KB Output is correct
58 Correct 19 ms 7004 KB Output is correct
59 Correct 19 ms 7000 KB Output is correct
60 Correct 18 ms 7000 KB Output is correct
61 Correct 8 ms 7000 KB Output is correct
62 Correct 8 ms 7004 KB Output is correct
63 Correct 10 ms 7048 KB Output is correct
64 Correct 10 ms 7004 KB Output is correct
65 Correct 10 ms 6948 KB Output is correct
66 Correct 11 ms 7104 KB Output is correct
67 Correct 12 ms 7004 KB Output is correct
68 Correct 4 ms 7160 KB Output is correct
69 Correct 6 ms 7260 KB Output is correct
70 Correct 6 ms 7260 KB Output is correct
71 Correct 6 ms 7156 KB Output is correct
72 Correct 18 ms 7004 KB Output is correct
73 Correct 28 ms 7068 KB Output is correct
74 Correct 8 ms 7000 KB Output is correct
75 Correct 7 ms 7004 KB Output is correct
76 Correct 6 ms 7004 KB Output is correct
77 Correct 7 ms 7004 KB Output is correct
78 Correct 6 ms 7068 KB Output is correct
79 Correct 6 ms 7004 KB Output is correct
80 Correct 6 ms 7004 KB Output is correct
81 Correct 8 ms 7004 KB Output is correct
82 Correct 9 ms 7004 KB Output is correct
83 Correct 8 ms 7004 KB Output is correct
84 Correct 8 ms 7004 KB Output is correct
85 Correct 8 ms 7004 KB Output is correct
86 Correct 8 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 5976 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 3 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6232 KB Output is correct
14 Correct 3 ms 6236 KB Output is correct
15 Correct 3 ms 6236 KB Output is correct
16 Correct 5 ms 6236 KB Output is correct
17 Correct 3 ms 6236 KB Output is correct
18 Correct 3 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6136 KB Output is correct
21 Correct 3 ms 6236 KB Output is correct
22 Correct 3 ms 6236 KB Output is correct
23 Correct 3 ms 6236 KB Output is correct
24 Correct 3 ms 6236 KB Output is correct
25 Correct 3 ms 6236 KB Output is correct
26 Correct 3 ms 6136 KB Output is correct
27 Correct 3 ms 6236 KB Output is correct
28 Correct 2 ms 6280 KB Output is correct
29 Correct 4 ms 6232 KB Output is correct
30 Correct 3 ms 6232 KB Output is correct
31 Correct 3 ms 6132 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 3 ms 6232 KB Output is correct
34 Correct 3 ms 6240 KB Output is correct
35 Correct 3 ms 6128 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 2 ms 6068 KB Output is correct
39 Correct 2 ms 6236 KB Output is correct
40 Correct 2 ms 6236 KB Output is correct
41 Correct 3 ms 6236 KB Output is correct
42 Correct 2 ms 6236 KB Output is correct
43 Correct 3 ms 6236 KB Output is correct
44 Correct 3 ms 6236 KB Output is correct
45 Correct 4 ms 6232 KB Output is correct
46 Correct 10 ms 7000 KB Output is correct
47 Correct 11 ms 7124 KB Output is correct
48 Correct 9 ms 7004 KB Output is correct
49 Correct 4 ms 7260 KB Output is correct
50 Correct 5 ms 7260 KB Output is correct
51 Correct 6 ms 7260 KB Output is correct
52 Correct 7 ms 7260 KB Output is correct
53 Correct 7 ms 7260 KB Output is correct
54 Correct 7 ms 7236 KB Output is correct
55 Correct 7 ms 7260 KB Output is correct
56 Correct 7 ms 7236 KB Output is correct
57 Correct 18 ms 7088 KB Output is correct
58 Correct 19 ms 7004 KB Output is correct
59 Correct 19 ms 7000 KB Output is correct
60 Correct 18 ms 7000 KB Output is correct
61 Correct 8 ms 7000 KB Output is correct
62 Correct 8 ms 7004 KB Output is correct
63 Correct 10 ms 7048 KB Output is correct
64 Correct 10 ms 7004 KB Output is correct
65 Correct 10 ms 6948 KB Output is correct
66 Correct 11 ms 7104 KB Output is correct
67 Correct 12 ms 7004 KB Output is correct
68 Correct 4 ms 7160 KB Output is correct
69 Correct 6 ms 7260 KB Output is correct
70 Correct 6 ms 7260 KB Output is correct
71 Correct 6 ms 7156 KB Output is correct
72 Correct 18 ms 7004 KB Output is correct
73 Correct 28 ms 7068 KB Output is correct
74 Correct 8 ms 7000 KB Output is correct
75 Correct 7 ms 7004 KB Output is correct
76 Correct 6 ms 7004 KB Output is correct
77 Correct 7 ms 7004 KB Output is correct
78 Correct 6 ms 7068 KB Output is correct
79 Correct 6 ms 7004 KB Output is correct
80 Correct 6 ms 7004 KB Output is correct
81 Correct 8 ms 7004 KB Output is correct
82 Correct 9 ms 7004 KB Output is correct
83 Correct 8 ms 7004 KB Output is correct
84 Correct 8 ms 7004 KB Output is correct
85 Correct 8 ms 7004 KB Output is correct
86 Correct 8 ms 7004 KB Output is correct
87 Correct 27 ms 8540 KB Output is correct
88 Correct 94 ms 13276 KB Output is correct
89 Correct 451 ms 29892 KB Output is correct
90 Correct 435 ms 29668 KB Output is correct
91 Correct 442 ms 29764 KB Output is correct
92 Correct 93 ms 37112 KB Output is correct
93 Correct 102 ms 37432 KB Output is correct
94 Correct 98 ms 37320 KB Output is correct
95 Correct 205 ms 34356 KB Output is correct
96 Correct 208 ms 34672 KB Output is correct
97 Correct 237 ms 34612 KB Output is correct
98 Correct 214 ms 34600 KB Output is correct
99 Correct 223 ms 32772 KB Output is correct
100 Correct 994 ms 29640 KB Output is correct
101 Correct 1036 ms 30104 KB Output is correct
102 Correct 994 ms 29924 KB Output is correct
103 Correct 968 ms 29896 KB Output is correct
104 Correct 322 ms 32612 KB Output is correct
105 Correct 297 ms 32712 KB Output is correct
106 Correct 304 ms 32716 KB Output is correct
107 Correct 439 ms 28888 KB Output is correct
108 Correct 449 ms 29132 KB Output is correct
109 Correct 481 ms 29392 KB Output is correct
110 Correct 88 ms 36548 KB Output is correct
111 Correct 215 ms 34344 KB Output is correct
112 Correct 223 ms 34104 KB Output is correct
113 Correct 201 ms 32084 KB Output is correct
114 Correct 988 ms 29700 KB Output is correct
115 Correct 942 ms 29468 KB Output is correct
116 Correct 308 ms 31948 KB Output is correct
117 Correct 191 ms 30852 KB Output is correct
118 Correct 185 ms 30348 KB Output is correct
119 Correct 199 ms 30088 KB Output is correct
120 Correct 187 ms 30400 KB Output is correct
121 Correct 208 ms 30020 KB Output is correct
122 Correct 180 ms 29536 KB Output is correct
123 Correct 305 ms 30924 KB Output is correct
124 Correct 321 ms 30328 KB Output is correct
125 Correct 311 ms 30080 KB Output is correct
126 Correct 306 ms 30524 KB Output is correct
127 Correct 307 ms 30048 KB Output is correct
128 Correct 323 ms 29612 KB Output is correct