답안 #875870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875870 2023-11-20T16:22:43 Z awu Construction of Highway (JOI18_construction) C++14
100 / 100
1433 ms 30104 KB
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #define int long long
#define ll long long
// #define double long double
#define all(x) x.begin(), x.end()
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
#define f first
#define s second
// #define endl '\n'

using pii = pair<int, int>;

const int inf = 1 << 30;
// const ll inf = 1ll << 62;

struct maxtree {
  vector<pii> t;
  int n;
  maxtree(int s) {
    n = 1;
    while(n < s) n *= 2;
    t.resize(n * 2, {-1, -1});
  }
  void update(int i, pii v) {
    for(t[i += n] = v; i > 1; i /= 2) {
      t[i / 2] = max(t[i], t[i ^ 1]);
    }
  }
  pii query(int l, int r) {
    pii res = {-1, -1};
    for(l += n, r += n; l < r; l /= 2, r /= 2) {
      if(l & 1) res = max(res, t[l++]);
      if(r & 1) res = max(res, t[--r]);
    }
    return res;
  }
};

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n; cin >> n;
  vector<int> c(n);
  for(int i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<pii> el;
  vector<vector<int>> ch(n);
  for(int i = 0; i < n - 1; i++) {
    int a, b; cin >> a >> b; a--; b--;
    ch[a].push_back(b);
    el.push_back({a, b});
  }
  vector<int> d(n);
  vector<vector<int>> jmp(n, vector<int>(ceil(log2(n))));
  vector<int> tin(n), tout(n);
  int time = 0;
  function<void(int)> dfs = [&](int cur) {
    tin[cur] = time++;
    for(int i = 1; i < log2(n); i++) {
      jmp[cur][i] = jmp[jmp[cur][i - 1]][i - 1];
    }
    for(auto i : ch[cur]) {
      jmp[i][0] = cur;
      d[i] = d[cur] + 1;
      dfs(i);
    }
    tout[cur] = time;
  };
  dfs(0);
  maxtree st(n);
  st.update(0, {-1, c[0]});
  for(int i = 0; i < n - 1; i++) {
    vector<pii> v;
    int cur = el[i].f;
    while(true) {
      int od = d[cur];
      pii curv = st.query(tin[cur], tout[cur]);
      for(int i = ceil(log2(n)) - 1; i >= 0; i--) {
        int nxt = jmp[cur][i];
        if(st.query(tin[nxt], tout[nxt]).f == curv.f) {
          cur = nxt;
        }
      }
      v.push_back({curv.s, od - d[cur] + 1});
      if(cur == 0) break;
      cur = jmp[cur][0];
    }
    int ans = 0;
    function<vector<pii>(int, int)> dnc = [&](int l, int r) {
      if(l + 1 == r) return vector<pii>{v[l]};
      int m = (l + r) / 2;
      auto lv = dnc(l, m), rv = dnc(m, r);
      vector<pii> res;
      int i = 0, j = 0, x = 0;
      while(j < rv.size()) {
        if(i < lv.size() && lv[i].f < rv[j].f) {
          res.push_back(lv[i]);
          x += lv[i].s;
          i++;
        } else {
          res.push_back(rv[j]);
          ans += x * rv[j].s;
          j++;
        }
      }
      while(i < lv.size()) {
        res.push_back(lv[i]);
        i++;
      }
      return res;
    };
    dnc(0, v.size());
    cout << ans << '\n';
    st.update(tin[el[i].s], {i, c[el[i].s]});
  }
}

Compilation message

construction.cpp: In lambda function:
construction.cpp:99:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |       while(j < rv.size()) {
      |             ~~^~~~~~~~~~~
construction.cpp:100:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |         if(i < lv.size() && lv[i].f < rv[j].f) {
      |            ~~^~~~~~~~~~~
construction.cpp:110:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |       while(i < lv.size()) {
      |             ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 500 KB Output is correct
38 Correct 1 ms 532 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 500 KB Output is correct
38 Correct 1 ms 532 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 3 ms 604 KB Output is correct
46 Correct 12 ms 1116 KB Output is correct
47 Correct 13 ms 1116 KB Output is correct
48 Correct 12 ms 1052 KB Output is correct
49 Correct 5 ms 1372 KB Output is correct
50 Correct 5 ms 1372 KB Output is correct
51 Correct 5 ms 1372 KB Output is correct
52 Correct 6 ms 1372 KB Output is correct
53 Correct 6 ms 1380 KB Output is correct
54 Correct 6 ms 1244 KB Output is correct
55 Correct 6 ms 1372 KB Output is correct
56 Correct 9 ms 1112 KB Output is correct
57 Correct 24 ms 1140 KB Output is correct
58 Correct 25 ms 1116 KB Output is correct
59 Correct 24 ms 1116 KB Output is correct
60 Correct 24 ms 1124 KB Output is correct
61 Correct 7 ms 1116 KB Output is correct
62 Correct 7 ms 1112 KB Output is correct
63 Correct 7 ms 1116 KB Output is correct
64 Correct 12 ms 1116 KB Output is correct
65 Correct 14 ms 1116 KB Output is correct
66 Correct 12 ms 1116 KB Output is correct
67 Correct 12 ms 1112 KB Output is correct
68 Correct 6 ms 1464 KB Output is correct
69 Correct 8 ms 1372 KB Output is correct
70 Correct 8 ms 1348 KB Output is correct
71 Correct 6 ms 1100 KB Output is correct
72 Correct 24 ms 1116 KB Output is correct
73 Correct 25 ms 1112 KB Output is correct
74 Correct 7 ms 1116 KB Output is correct
75 Correct 6 ms 1116 KB Output is correct
76 Correct 6 ms 1184 KB Output is correct
77 Correct 6 ms 1112 KB Output is correct
78 Correct 9 ms 1116 KB Output is correct
79 Correct 7 ms 1116 KB Output is correct
80 Correct 7 ms 1116 KB Output is correct
81 Correct 8 ms 1112 KB Output is correct
82 Correct 8 ms 1116 KB Output is correct
83 Correct 8 ms 1076 KB Output is correct
84 Correct 7 ms 1144 KB Output is correct
85 Correct 8 ms 1112 KB Output is correct
86 Correct 7 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 552 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 500 KB Output is correct
38 Correct 1 ms 532 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 3 ms 604 KB Output is correct
46 Correct 12 ms 1116 KB Output is correct
47 Correct 13 ms 1116 KB Output is correct
48 Correct 12 ms 1052 KB Output is correct
49 Correct 5 ms 1372 KB Output is correct
50 Correct 5 ms 1372 KB Output is correct
51 Correct 5 ms 1372 KB Output is correct
52 Correct 6 ms 1372 KB Output is correct
53 Correct 6 ms 1380 KB Output is correct
54 Correct 6 ms 1244 KB Output is correct
55 Correct 6 ms 1372 KB Output is correct
56 Correct 9 ms 1112 KB Output is correct
57 Correct 24 ms 1140 KB Output is correct
58 Correct 25 ms 1116 KB Output is correct
59 Correct 24 ms 1116 KB Output is correct
60 Correct 24 ms 1124 KB Output is correct
61 Correct 7 ms 1116 KB Output is correct
62 Correct 7 ms 1112 KB Output is correct
63 Correct 7 ms 1116 KB Output is correct
64 Correct 12 ms 1116 KB Output is correct
65 Correct 14 ms 1116 KB Output is correct
66 Correct 12 ms 1116 KB Output is correct
67 Correct 12 ms 1112 KB Output is correct
68 Correct 6 ms 1464 KB Output is correct
69 Correct 8 ms 1372 KB Output is correct
70 Correct 8 ms 1348 KB Output is correct
71 Correct 6 ms 1100 KB Output is correct
72 Correct 24 ms 1116 KB Output is correct
73 Correct 25 ms 1112 KB Output is correct
74 Correct 7 ms 1116 KB Output is correct
75 Correct 6 ms 1116 KB Output is correct
76 Correct 6 ms 1184 KB Output is correct
77 Correct 6 ms 1112 KB Output is correct
78 Correct 9 ms 1116 KB Output is correct
79 Correct 7 ms 1116 KB Output is correct
80 Correct 7 ms 1116 KB Output is correct
81 Correct 8 ms 1112 KB Output is correct
82 Correct 8 ms 1116 KB Output is correct
83 Correct 8 ms 1076 KB Output is correct
84 Correct 7 ms 1144 KB Output is correct
85 Correct 8 ms 1112 KB Output is correct
86 Correct 7 ms 1116 KB Output is correct
87 Correct 38 ms 2236 KB Output is correct
88 Correct 159 ms 5844 KB Output is correct
89 Correct 625 ms 19400 KB Output is correct
90 Correct 600 ms 19220 KB Output is correct
91 Correct 625 ms 19408 KB Output is correct
92 Correct 198 ms 29944 KB Output is correct
93 Correct 186 ms 29896 KB Output is correct
94 Correct 191 ms 29892 KB Output is correct
95 Correct 265 ms 25812 KB Output is correct
96 Correct 264 ms 25576 KB Output is correct
97 Correct 261 ms 25544 KB Output is correct
98 Correct 277 ms 25540 KB Output is correct
99 Correct 281 ms 23856 KB Output is correct
100 Correct 1365 ms 20000 KB Output is correct
101 Correct 1386 ms 19792 KB Output is correct
102 Correct 1433 ms 20028 KB Output is correct
103 Correct 1392 ms 19684 KB Output is correct
104 Correct 338 ms 24192 KB Output is correct
105 Correct 328 ms 23752 KB Output is correct
106 Correct 348 ms 24520 KB Output is correct
107 Correct 591 ms 19232 KB Output is correct
108 Correct 611 ms 19260 KB Output is correct
109 Correct 642 ms 19144 KB Output is correct
110 Correct 184 ms 30104 KB Output is correct
111 Correct 264 ms 25540 KB Output is correct
112 Correct 257 ms 25436 KB Output is correct
113 Correct 254 ms 23748 KB Output is correct
114 Correct 1367 ms 19944 KB Output is correct
115 Correct 1353 ms 19844 KB Output is correct
116 Correct 332 ms 23748 KB Output is correct
117 Correct 236 ms 21432 KB Output is correct
118 Correct 253 ms 21268 KB Output is correct
119 Correct 237 ms 20168 KB Output is correct
120 Correct 245 ms 21592 KB Output is correct
121 Correct 239 ms 20800 KB Output is correct
122 Correct 229 ms 20388 KB Output is correct
123 Correct 366 ms 21800 KB Output is correct
124 Correct 353 ms 20800 KB Output is correct
125 Correct 376 ms 20644 KB Output is correct
126 Correct 351 ms 21640 KB Output is correct
127 Correct 363 ms 20712 KB Output is correct
128 Correct 359 ms 20280 KB Output is correct