답안 #624602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624602 2022-08-08T14:24:04 Z MilosMilutinovic Construction of Highway (JOI18_construction) C++14
100 / 100
1004 ms 34712 KB
/**
 *    author:  wxhtzdy
 *    created: 08.08.2022 14:06:23
**/
#include <bits/stdc++.h>

using namespace std;

template <typename T>
class fenwick {
  public:
  vector<T> fenw;
  int n;
  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }
  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }
  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  auto xs = c;
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < n; i++) {
    c[i] = lower_bound(xs.begin(), xs.end(), c[i]) - xs.begin();
  }  
  vector<vector<int>> g(n);
  vector<pair<int, int>> edges;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;                
    g[u].push_back(v);
    g[v].push_back(u);
    edges.emplace_back(u, v);
  }
  vector<int> par(n);
  vector<int> dep(n);
  vector<int> tin(n);
  vector<int> tout(n);
  int T = 0;   
  function<void(int, int)> Dfs = [&](int v, int pr) {
    tin[v] = ++T;
    par[v] = pr;
    dep[v] = dep[pr] + 1;
    for (int u : g[v]) {
      if (u != pr) {
        Dfs(u, v);
      }
    }
    tout[v] = T;
  };
  Dfs(0, 0);
  const int L = 25;
  vector<vector<int>> jump(n, vector<int>(L));
  for (int i = 0; i < n; i++) {
    jump[i][0] = par[i];  
  }
  for (int j = 1; j < L; j++) {
    for (int i = 0; i < n; i++) {
      jump[i][j] = jump[jump[i][j - 1]][j - 1];
    }
  }     
  auto isPar = [&](int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
  };
  auto Find = [&](int u, int v) {
    for (int i = L - 1; i >= 0; i--) {
      if (!isPar(jump[v][i], u)) {
        v = jump[v][i];
      }
    }
    return v;
  };                   
  vector<int> f(n, -1);
  f[0] = 0;
  fenwick<int> fenw(n);
  for (auto& p : edges) {
    int a = p.first;
    int b = p.second;    
    int x = 0;
    vector<pair<int, int>> v;
    while (x != b && !isPar(f[x], b)) {
      int i = Find(f[x], b);
      int j = Find(b, f[x]);
      f[j] = f[x];
      v.emplace_back(dep[i] - dep[x], c[f[x]]);        
      x = i;
    }   
    if (x != b) {      
      v.emplace_back(dep[b] - dep[x], c[f[x]]);
    }
    f[0] = b;
    long long ans = 0;
    reverse(v.begin(), v.end());
    for (auto& p : v) {
      ans += fenw.get(p.second - 1) * 1LL * p.first;
      fenw.modify(p.second, p.first);
    }
    cout << ans << '\n';
    for (auto& p : v) {
      fenw.modify(p.second, -p.first);    
    }
  } 
  return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:100:9: warning: unused variable 'a' [-Wunused-variable]
  100 |     int a = p.first;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 2 ms 328 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 456 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 456 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 2 ms 328 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 456 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 456 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 428 KB Output is correct
45 Correct 3 ms 464 KB Output is correct
46 Correct 10 ms 1236 KB Output is correct
47 Correct 11 ms 1316 KB Output is correct
48 Correct 10 ms 1232 KB Output is correct
49 Correct 4 ms 1580 KB Output is correct
50 Correct 4 ms 1616 KB Output is correct
51 Correct 4 ms 1568 KB Output is correct
52 Correct 5 ms 1492 KB Output is correct
53 Correct 7 ms 1436 KB Output is correct
54 Correct 6 ms 1492 KB Output is correct
55 Correct 5 ms 1492 KB Output is correct
56 Correct 6 ms 1440 KB Output is correct
57 Correct 21 ms 1172 KB Output is correct
58 Correct 21 ms 1184 KB Output is correct
59 Correct 20 ms 1324 KB Output is correct
60 Correct 21 ms 1236 KB Output is correct
61 Correct 6 ms 1492 KB Output is correct
62 Correct 6 ms 1484 KB Output is correct
63 Correct 6 ms 1492 KB Output is correct
64 Correct 10 ms 1236 KB Output is correct
65 Correct 10 ms 1236 KB Output is correct
66 Correct 10 ms 1296 KB Output is correct
67 Correct 10 ms 1296 KB Output is correct
68 Correct 3 ms 1620 KB Output is correct
69 Correct 5 ms 1492 KB Output is correct
70 Correct 5 ms 1408 KB Output is correct
71 Correct 5 ms 1440 KB Output is correct
72 Correct 20 ms 1236 KB Output is correct
73 Correct 21 ms 1288 KB Output is correct
74 Correct 5 ms 1364 KB Output is correct
75 Correct 5 ms 1360 KB Output is correct
76 Correct 5 ms 1364 KB Output is correct
77 Correct 5 ms 1236 KB Output is correct
78 Correct 5 ms 1364 KB Output is correct
79 Correct 5 ms 1236 KB Output is correct
80 Correct 5 ms 1244 KB Output is correct
81 Correct 7 ms 1372 KB Output is correct
82 Correct 6 ms 1372 KB Output is correct
83 Correct 6 ms 1256 KB Output is correct
84 Correct 6 ms 1372 KB Output is correct
85 Correct 5 ms 1244 KB Output is correct
86 Correct 6 ms 1328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 2 ms 328 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 456 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 456 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 428 KB Output is correct
45 Correct 3 ms 464 KB Output is correct
46 Correct 10 ms 1236 KB Output is correct
47 Correct 11 ms 1316 KB Output is correct
48 Correct 10 ms 1232 KB Output is correct
49 Correct 4 ms 1580 KB Output is correct
50 Correct 4 ms 1616 KB Output is correct
51 Correct 4 ms 1568 KB Output is correct
52 Correct 5 ms 1492 KB Output is correct
53 Correct 7 ms 1436 KB Output is correct
54 Correct 6 ms 1492 KB Output is correct
55 Correct 5 ms 1492 KB Output is correct
56 Correct 6 ms 1440 KB Output is correct
57 Correct 21 ms 1172 KB Output is correct
58 Correct 21 ms 1184 KB Output is correct
59 Correct 20 ms 1324 KB Output is correct
60 Correct 21 ms 1236 KB Output is correct
61 Correct 6 ms 1492 KB Output is correct
62 Correct 6 ms 1484 KB Output is correct
63 Correct 6 ms 1492 KB Output is correct
64 Correct 10 ms 1236 KB Output is correct
65 Correct 10 ms 1236 KB Output is correct
66 Correct 10 ms 1296 KB Output is correct
67 Correct 10 ms 1296 KB Output is correct
68 Correct 3 ms 1620 KB Output is correct
69 Correct 5 ms 1492 KB Output is correct
70 Correct 5 ms 1408 KB Output is correct
71 Correct 5 ms 1440 KB Output is correct
72 Correct 20 ms 1236 KB Output is correct
73 Correct 21 ms 1288 KB Output is correct
74 Correct 5 ms 1364 KB Output is correct
75 Correct 5 ms 1360 KB Output is correct
76 Correct 5 ms 1364 KB Output is correct
77 Correct 5 ms 1236 KB Output is correct
78 Correct 5 ms 1364 KB Output is correct
79 Correct 5 ms 1236 KB Output is correct
80 Correct 5 ms 1244 KB Output is correct
81 Correct 7 ms 1372 KB Output is correct
82 Correct 6 ms 1372 KB Output is correct
83 Correct 6 ms 1256 KB Output is correct
84 Correct 6 ms 1372 KB Output is correct
85 Correct 5 ms 1244 KB Output is correct
86 Correct 6 ms 1328 KB Output is correct
87 Correct 29 ms 2824 KB Output is correct
88 Correct 101 ms 7972 KB Output is correct
89 Correct 472 ms 25648 KB Output is correct
90 Correct 474 ms 25672 KB Output is correct
91 Correct 471 ms 25580 KB Output is correct
92 Correct 128 ms 34712 KB Output is correct
93 Correct 138 ms 34608 KB Output is correct
94 Correct 134 ms 34680 KB Output is correct
95 Correct 188 ms 30448 KB Output is correct
96 Correct 190 ms 30912 KB Output is correct
97 Correct 186 ms 30904 KB Output is correct
98 Correct 187 ms 30836 KB Output is correct
99 Correct 197 ms 30212 KB Output is correct
100 Correct 949 ms 25256 KB Output is correct
101 Correct 990 ms 25536 KB Output is correct
102 Correct 999 ms 25372 KB Output is correct
103 Correct 1004 ms 25568 KB Output is correct
104 Correct 274 ms 30124 KB Output is correct
105 Correct 268 ms 30136 KB Output is correct
106 Correct 294 ms 30144 KB Output is correct
107 Correct 420 ms 24628 KB Output is correct
108 Correct 442 ms 24900 KB Output is correct
109 Correct 458 ms 25140 KB Output is correct
110 Correct 123 ms 34152 KB Output is correct
111 Correct 185 ms 30548 KB Output is correct
112 Correct 181 ms 30296 KB Output is correct
113 Correct 175 ms 29496 KB Output is correct
114 Correct 946 ms 25260 KB Output is correct
115 Correct 949 ms 24824 KB Output is correct
116 Correct 271 ms 29504 KB Output is correct
117 Correct 177 ms 27376 KB Output is correct
118 Correct 189 ms 26656 KB Output is correct
119 Correct 175 ms 26300 KB Output is correct
120 Correct 171 ms 27104 KB Output is correct
121 Correct 167 ms 26280 KB Output is correct
122 Correct 172 ms 25952 KB Output is correct
123 Correct 303 ms 27584 KB Output is correct
124 Correct 304 ms 26728 KB Output is correct
125 Correct 304 ms 26368 KB Output is correct
126 Correct 267 ms 27156 KB Output is correct
127 Correct 278 ms 26344 KB Output is correct
128 Correct 276 ms 25960 KB Output is correct