Submission #645960

# Submission time Handle Problem Language Result Execution time Memory
645960 2022-09-28T10:58:52 Z dattranxxx Construction of Highway (JOI18_construction) C++11
100 / 100
361 ms 110524 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
vector<int> adj[N];
int a[N], c[N];
int n;
int siz[N], par[N], dep[N];
int size(int u) {
  siz[u] = 1;
  for (int& v : adj[u]) {
    adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
    par[v] = u, dep[v] = dep[u] + 1;
    siz[u] += size(v);
    if (siz[v] > siz[adj[u][0]])
      swap(v, adj[u][0]);
  }
  return siz[u];
}

int nxt[N];
void hld(int u) {
  for (int v : adj[u]) {
    nxt[v] = v == adj[u][0] ? nxt[u] : v;
    hld(v);
  }
}
int m;
int bit[N];
int cnt(int i) {
  int res = 0;
  for (; i; i -= i & -i)
    res += bit[i];
  return res;
}

void add(int i, int x) {
  for (; i <= m; i += i & -i)
    bit[i] += x;
}

#define Tp pair<int, int>
#define fi first
#define se second
deque<Tp> seq[N];

ll get(int u) {
  vector<Tp> cur;
  for (int v = u; ~v; v = par[nxt[v]]) {
    int len = dep[v] - dep[nxt[v]] + 1;
    vector<Tp> tmp;
    for (auto& p : seq[nxt[v]]) {
      if (len >= p.se)
        len -= p.se, tmp.emplace_back(p);
      else {
        tmp.emplace_back(p);
        tmp.back().se = len; break;
      }
    }
    reverse(tmp.begin(), tmp.end());
    for (auto& p : tmp) cur.emplace_back(p);
  }
  ll res = 0;
  for (auto& p : cur) 
    res += p.se * cnt(p.fi - 1), add(p.fi, p.se);
  for (auto& p : cur) add(p.fi, -p.se);
  return res;
}

void update(int u) {
  for (int v = u; ~v; v = par[nxt[v]]) {
    int len = dep[v] - dep[nxt[v]] + 1;
    while (!seq[nxt[v]].empty()) {
      if (len >= seq[nxt[v]].front().se)
        len -= seq[nxt[v]].front().se, seq[nxt[v]].pop_front();
      else {
        seq[nxt[v]].front().se -= len; break;
      }
    }
    seq[nxt[v]].emplace_front(a[u], dep[v] - dep[nxt[v]] + 1);
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0); cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i], c[i] = a[i];
  sort(c, c + n); m = unique(c, c + n) - c;
  for (int i = 0; i < n; ++i)
    a[i] = lower_bound(c, c + m, a[i]) - c + 1;
  vector<pair<int, int>> edges;
  for (int i = 0, u, v; i < n-1; ++i)
    cin >> u >> v, u--, v--,
    adj[u].emplace_back(v),
    adj[v].emplace_back(u),
    edges.emplace_back(u, v);
  par[0] = -1, size(0), hld(0);
  update(0);
  for (int i = 0, u, v; i < n-1; ++i) {
    tie(u, v) = edges[i];
    cout << get(u) << '\n';
    update(v);
  }
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 39 ms 69912 KB Output is correct
2 Correct 40 ms 69992 KB Output is correct
3 Correct 45 ms 69976 KB Output is correct
4 Correct 44 ms 70092 KB Output is correct
5 Correct 44 ms 70092 KB Output is correct
6 Correct 44 ms 70120 KB Output is correct
7 Correct 44 ms 70092 KB Output is correct
8 Correct 40 ms 70104 KB Output is correct
9 Correct 44 ms 69992 KB Output is correct
10 Correct 40 ms 69992 KB Output is correct
11 Correct 45 ms 70104 KB Output is correct
12 Correct 45 ms 70220 KB Output is correct
13 Correct 47 ms 70212 KB Output is correct
14 Correct 45 ms 70164 KB Output is correct
15 Correct 48 ms 70188 KB Output is correct
16 Correct 45 ms 70084 KB Output is correct
17 Correct 45 ms 70092 KB Output is correct
18 Correct 47 ms 70128 KB Output is correct
19 Correct 43 ms 70092 KB Output is correct
20 Correct 45 ms 70100 KB Output is correct
21 Correct 44 ms 70092 KB Output is correct
22 Correct 50 ms 70196 KB Output is correct
23 Correct 45 ms 70128 KB Output is correct
24 Correct 45 ms 70088 KB Output is correct
25 Correct 46 ms 70132 KB Output is correct
26 Correct 42 ms 70020 KB Output is correct
27 Correct 45 ms 70140 KB Output is correct
28 Correct 45 ms 70140 KB Output is correct
29 Correct 47 ms 70240 KB Output is correct
30 Correct 45 ms 70092 KB Output is correct
31 Correct 45 ms 70092 KB Output is correct
32 Correct 45 ms 70224 KB Output is correct
33 Correct 47 ms 70124 KB Output is correct
34 Correct 45 ms 70124 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 45 ms 70196 KB Output is correct
37 Correct 46 ms 70188 KB Output is correct
38 Correct 44 ms 70120 KB Output is correct
39 Correct 45 ms 70100 KB Output is correct
40 Correct 45 ms 70084 KB Output is correct
41 Correct 45 ms 70188 KB Output is correct
42 Correct 46 ms 70152 KB Output is correct
43 Correct 45 ms 70168 KB Output is correct
44 Correct 48 ms 70120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 69912 KB Output is correct
2 Correct 40 ms 69992 KB Output is correct
3 Correct 45 ms 69976 KB Output is correct
4 Correct 44 ms 70092 KB Output is correct
5 Correct 44 ms 70092 KB Output is correct
6 Correct 44 ms 70120 KB Output is correct
7 Correct 44 ms 70092 KB Output is correct
8 Correct 40 ms 70104 KB Output is correct
9 Correct 44 ms 69992 KB Output is correct
10 Correct 40 ms 69992 KB Output is correct
11 Correct 45 ms 70104 KB Output is correct
12 Correct 45 ms 70220 KB Output is correct
13 Correct 47 ms 70212 KB Output is correct
14 Correct 45 ms 70164 KB Output is correct
15 Correct 48 ms 70188 KB Output is correct
16 Correct 45 ms 70084 KB Output is correct
17 Correct 45 ms 70092 KB Output is correct
18 Correct 47 ms 70128 KB Output is correct
19 Correct 43 ms 70092 KB Output is correct
20 Correct 45 ms 70100 KB Output is correct
21 Correct 44 ms 70092 KB Output is correct
22 Correct 50 ms 70196 KB Output is correct
23 Correct 45 ms 70128 KB Output is correct
24 Correct 45 ms 70088 KB Output is correct
25 Correct 46 ms 70132 KB Output is correct
26 Correct 42 ms 70020 KB Output is correct
27 Correct 45 ms 70140 KB Output is correct
28 Correct 45 ms 70140 KB Output is correct
29 Correct 47 ms 70240 KB Output is correct
30 Correct 45 ms 70092 KB Output is correct
31 Correct 45 ms 70092 KB Output is correct
32 Correct 45 ms 70224 KB Output is correct
33 Correct 47 ms 70124 KB Output is correct
34 Correct 45 ms 70124 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 45 ms 70196 KB Output is correct
37 Correct 46 ms 70188 KB Output is correct
38 Correct 44 ms 70120 KB Output is correct
39 Correct 45 ms 70100 KB Output is correct
40 Correct 45 ms 70084 KB Output is correct
41 Correct 45 ms 70188 KB Output is correct
42 Correct 46 ms 70152 KB Output is correct
43 Correct 45 ms 70168 KB Output is correct
44 Correct 48 ms 70120 KB Output is correct
45 Correct 46 ms 70252 KB Output is correct
46 Correct 52 ms 71272 KB Output is correct
47 Correct 53 ms 71476 KB Output is correct
48 Correct 51 ms 71500 KB Output is correct
49 Correct 44 ms 70668 KB Output is correct
50 Correct 44 ms 70604 KB Output is correct
51 Correct 44 ms 70672 KB Output is correct
52 Correct 50 ms 71696 KB Output is correct
53 Correct 49 ms 71500 KB Output is correct
54 Correct 49 ms 71600 KB Output is correct
55 Correct 50 ms 71516 KB Output is correct
56 Correct 51 ms 71488 KB Output is correct
57 Correct 52 ms 71264 KB Output is correct
58 Correct 53 ms 71356 KB Output is correct
59 Correct 56 ms 71440 KB Output is correct
60 Correct 53 ms 71284 KB Output is correct
61 Correct 49 ms 71544 KB Output is correct
62 Correct 49 ms 71472 KB Output is correct
63 Correct 51 ms 71560 KB Output is correct
64 Correct 51 ms 71252 KB Output is correct
65 Correct 52 ms 71280 KB Output is correct
66 Correct 50 ms 71280 KB Output is correct
67 Correct 52 ms 71272 KB Output is correct
68 Correct 44 ms 70564 KB Output is correct
69 Correct 51 ms 71604 KB Output is correct
70 Correct 49 ms 71564 KB Output is correct
71 Correct 49 ms 71408 KB Output is correct
72 Correct 52 ms 71244 KB Output is correct
73 Correct 50 ms 71292 KB Output is correct
74 Correct 48 ms 71404 KB Output is correct
75 Correct 52 ms 71352 KB Output is correct
76 Correct 61 ms 71360 KB Output is correct
77 Correct 50 ms 71392 KB Output is correct
78 Correct 50 ms 71424 KB Output is correct
79 Correct 49 ms 71404 KB Output is correct
80 Correct 55 ms 71436 KB Output is correct
81 Correct 49 ms 71380 KB Output is correct
82 Correct 50 ms 71372 KB Output is correct
83 Correct 50 ms 71380 KB Output is correct
84 Correct 50 ms 71400 KB Output is correct
85 Correct 50 ms 71408 KB Output is correct
86 Correct 49 ms 71368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 69912 KB Output is correct
2 Correct 40 ms 69992 KB Output is correct
3 Correct 45 ms 69976 KB Output is correct
4 Correct 44 ms 70092 KB Output is correct
5 Correct 44 ms 70092 KB Output is correct
6 Correct 44 ms 70120 KB Output is correct
7 Correct 44 ms 70092 KB Output is correct
8 Correct 40 ms 70104 KB Output is correct
9 Correct 44 ms 69992 KB Output is correct
10 Correct 40 ms 69992 KB Output is correct
11 Correct 45 ms 70104 KB Output is correct
12 Correct 45 ms 70220 KB Output is correct
13 Correct 47 ms 70212 KB Output is correct
14 Correct 45 ms 70164 KB Output is correct
15 Correct 48 ms 70188 KB Output is correct
16 Correct 45 ms 70084 KB Output is correct
17 Correct 45 ms 70092 KB Output is correct
18 Correct 47 ms 70128 KB Output is correct
19 Correct 43 ms 70092 KB Output is correct
20 Correct 45 ms 70100 KB Output is correct
21 Correct 44 ms 70092 KB Output is correct
22 Correct 50 ms 70196 KB Output is correct
23 Correct 45 ms 70128 KB Output is correct
24 Correct 45 ms 70088 KB Output is correct
25 Correct 46 ms 70132 KB Output is correct
26 Correct 42 ms 70020 KB Output is correct
27 Correct 45 ms 70140 KB Output is correct
28 Correct 45 ms 70140 KB Output is correct
29 Correct 47 ms 70240 KB Output is correct
30 Correct 45 ms 70092 KB Output is correct
31 Correct 45 ms 70092 KB Output is correct
32 Correct 45 ms 70224 KB Output is correct
33 Correct 47 ms 70124 KB Output is correct
34 Correct 45 ms 70124 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 45 ms 70196 KB Output is correct
37 Correct 46 ms 70188 KB Output is correct
38 Correct 44 ms 70120 KB Output is correct
39 Correct 45 ms 70100 KB Output is correct
40 Correct 45 ms 70084 KB Output is correct
41 Correct 45 ms 70188 KB Output is correct
42 Correct 46 ms 70152 KB Output is correct
43 Correct 45 ms 70168 KB Output is correct
44 Correct 48 ms 70120 KB Output is correct
45 Correct 46 ms 70252 KB Output is correct
46 Correct 52 ms 71272 KB Output is correct
47 Correct 53 ms 71476 KB Output is correct
48 Correct 51 ms 71500 KB Output is correct
49 Correct 44 ms 70668 KB Output is correct
50 Correct 44 ms 70604 KB Output is correct
51 Correct 44 ms 70672 KB Output is correct
52 Correct 50 ms 71696 KB Output is correct
53 Correct 49 ms 71500 KB Output is correct
54 Correct 49 ms 71600 KB Output is correct
55 Correct 50 ms 71516 KB Output is correct
56 Correct 51 ms 71488 KB Output is correct
57 Correct 52 ms 71264 KB Output is correct
58 Correct 53 ms 71356 KB Output is correct
59 Correct 56 ms 71440 KB Output is correct
60 Correct 53 ms 71284 KB Output is correct
61 Correct 49 ms 71544 KB Output is correct
62 Correct 49 ms 71472 KB Output is correct
63 Correct 51 ms 71560 KB Output is correct
64 Correct 51 ms 71252 KB Output is correct
65 Correct 52 ms 71280 KB Output is correct
66 Correct 50 ms 71280 KB Output is correct
67 Correct 52 ms 71272 KB Output is correct
68 Correct 44 ms 70564 KB Output is correct
69 Correct 51 ms 71604 KB Output is correct
70 Correct 49 ms 71564 KB Output is correct
71 Correct 49 ms 71408 KB Output is correct
72 Correct 52 ms 71244 KB Output is correct
73 Correct 50 ms 71292 KB Output is correct
74 Correct 48 ms 71404 KB Output is correct
75 Correct 52 ms 71352 KB Output is correct
76 Correct 61 ms 71360 KB Output is correct
77 Correct 50 ms 71392 KB Output is correct
78 Correct 50 ms 71424 KB Output is correct
79 Correct 49 ms 71404 KB Output is correct
80 Correct 55 ms 71436 KB Output is correct
81 Correct 49 ms 71380 KB Output is correct
82 Correct 50 ms 71372 KB Output is correct
83 Correct 50 ms 71380 KB Output is correct
84 Correct 50 ms 71400 KB Output is correct
85 Correct 50 ms 71408 KB Output is correct
86 Correct 49 ms 71368 KB Output is correct
87 Correct 64 ms 73512 KB Output is correct
88 Correct 104 ms 80420 KB Output is correct
89 Correct 297 ms 105100 KB Output is correct
90 Correct 305 ms 104904 KB Output is correct
91 Correct 298 ms 104932 KB Output is correct
92 Correct 129 ms 86752 KB Output is correct
93 Correct 128 ms 86724 KB Output is correct
94 Correct 127 ms 86760 KB Output is correct
95 Correct 158 ms 109928 KB Output is correct
96 Correct 179 ms 110360 KB Output is correct
97 Correct 169 ms 110524 KB Output is correct
98 Correct 164 ms 110284 KB Output is correct
99 Correct 167 ms 108784 KB Output is correct
100 Correct 342 ms 96540 KB Output is correct
101 Correct 361 ms 96920 KB Output is correct
102 Correct 352 ms 96984 KB Output is correct
103 Correct 361 ms 96800 KB Output is correct
104 Correct 180 ms 108772 KB Output is correct
105 Correct 177 ms 108736 KB Output is correct
106 Correct 181 ms 108864 KB Output is correct
107 Correct 241 ms 103748 KB Output is correct
108 Correct 264 ms 104004 KB Output is correct
109 Correct 282 ms 104220 KB Output is correct
110 Correct 121 ms 85808 KB Output is correct
111 Correct 160 ms 109916 KB Output is correct
112 Correct 154 ms 109376 KB Output is correct
113 Correct 153 ms 107780 KB Output is correct
114 Correct 331 ms 96464 KB Output is correct
115 Correct 298 ms 95864 KB Output is correct
116 Correct 165 ms 107824 KB Output is correct
117 Correct 171 ms 106536 KB Output is correct
118 Correct 179 ms 105788 KB Output is correct
119 Correct 178 ms 105552 KB Output is correct
120 Correct 155 ms 105660 KB Output is correct
121 Correct 158 ms 105160 KB Output is correct
122 Correct 156 ms 104804 KB Output is correct
123 Correct 185 ms 106584 KB Output is correct
124 Correct 192 ms 106044 KB Output is correct
125 Correct 182 ms 105576 KB Output is correct
126 Correct 165 ms 105792 KB Output is correct
127 Correct 169 ms 105152 KB Output is correct
128 Correct 175 ms 104840 KB Output is correct