#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
int n;
int c[N], pr[N], head[N], tin[N], sz[N], s[N], bot[N];
set<array<int, 2>> st[N];
vector<int> g[N];
void upd(int i, int x) {
for (; i <= n; i += i & -i) {
s[i] += x;
}
}
int qry(int i) {
int res = 0;
for (; i; i -= i & -i) {
res += s[i];
}
return res;
}
void dfs(int u) {
for (int &v : g[u]) {
if (v == pr[u]) {
swap(v, g[u].back());
g[u].pop_back();
break;
}
}
sz[u] = 1;
for (int v : g[u]) {
pr[v] = u;
dfs(v);
sz[u] += sz[v];
}
}
int timer;
void hld(int u, int h) {
tin[u] = ++timer;
head[u] = h;
bot[h] = timer;
if (g[u].size()) {
int x = *max_element(g[u].begin(), g[u].end(), [&](int i, int j) {
return sz[i] < sz[j];
});
hld(x, h);
for (int v : g[u]) {
if (v != x) {
hld(v, v);
}
}
}
}
long long qry(int u, int x) {
vector<array<int, 2>> cands;
long long res = 0;
for (; u; u = pr[head[u]]) {
int h = head[u];
auto it = st[h].lower_bound({tin[u] + 1, 0});
assert(it != st[h].end());
bool exist = (*it)[0] == tin[u] + 1;
--it;
if (!exist) {
st[h].insert({tin[u] + 1, (*it)[1]});
}
vector<array<int, 2>> nw;
while (st[h].size() && (*(it = st[h].begin()))[0] <= tin[u]) {
nw.push_back({(*next(it))[0] - (*it)[0], (*it)[1]});
st[h].erase(it);
}
reverse(nw.begin(), nw.end());
cands.insert(cands.end(), nw.begin(), nw.end());
nw.clear();
st[h].insert({tin[h], x});
}
for (auto [l, x] : cands) {
res += (long long) qry(x - 1) * l;
upd(x, l);
}
for (auto [l, x] : cands) {
upd(x, -l);
}
cands.clear();
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
vector<int> comp;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
comp.push_back(c[i]);
}
vector<array<int, 2>> edges;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
edges.push_back({u, v});
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
hld(1, 1);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 1; i <= n; ++i) {
int h = head[i];
c[i] = lower_bound(comp.begin(), comp.end(), c[i]) - comp.begin() + 1;
st[h].insert({tin[i], c[i]});
if (bot[i]) {
st[h].insert({bot[i] + 1, -1});
}
}
for (auto [u, v] : edges) {
cout << qry(u, c[v]) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7376 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
4 ms |
7524 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
4 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7528 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7512 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
4 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7524 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
7516 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
3 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
3 ms |
7516 KB |
Output is correct |
28 |
Correct |
4 ms |
7640 KB |
Output is correct |
29 |
Correct |
3 ms |
7516 KB |
Output is correct |
30 |
Correct |
4 ms |
7512 KB |
Output is correct |
31 |
Correct |
5 ms |
7620 KB |
Output is correct |
32 |
Correct |
3 ms |
7640 KB |
Output is correct |
33 |
Correct |
4 ms |
7580 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
7516 KB |
Output is correct |
36 |
Correct |
3 ms |
7512 KB |
Output is correct |
37 |
Correct |
4 ms |
7516 KB |
Output is correct |
38 |
Correct |
3 ms |
7516 KB |
Output is correct |
39 |
Correct |
5 ms |
7516 KB |
Output is correct |
40 |
Correct |
4 ms |
7512 KB |
Output is correct |
41 |
Correct |
4 ms |
7512 KB |
Output is correct |
42 |
Correct |
4 ms |
7560 KB |
Output is correct |
43 |
Correct |
3 ms |
7516 KB |
Output is correct |
44 |
Correct |
4 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7376 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
4 ms |
7524 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
4 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7528 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7512 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
4 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7524 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
7516 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
3 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
3 ms |
7516 KB |
Output is correct |
28 |
Correct |
4 ms |
7640 KB |
Output is correct |
29 |
Correct |
3 ms |
7516 KB |
Output is correct |
30 |
Correct |
4 ms |
7512 KB |
Output is correct |
31 |
Correct |
5 ms |
7620 KB |
Output is correct |
32 |
Correct |
3 ms |
7640 KB |
Output is correct |
33 |
Correct |
4 ms |
7580 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
7516 KB |
Output is correct |
36 |
Correct |
3 ms |
7512 KB |
Output is correct |
37 |
Correct |
4 ms |
7516 KB |
Output is correct |
38 |
Correct |
3 ms |
7516 KB |
Output is correct |
39 |
Correct |
5 ms |
7516 KB |
Output is correct |
40 |
Correct |
4 ms |
7512 KB |
Output is correct |
41 |
Correct |
4 ms |
7512 KB |
Output is correct |
42 |
Correct |
4 ms |
7560 KB |
Output is correct |
43 |
Correct |
3 ms |
7516 KB |
Output is correct |
44 |
Correct |
4 ms |
7516 KB |
Output is correct |
45 |
Correct |
4 ms |
7516 KB |
Output is correct |
46 |
Correct |
8 ms |
8200 KB |
Output is correct |
47 |
Correct |
8 ms |
8028 KB |
Output is correct |
48 |
Correct |
7 ms |
8028 KB |
Output is correct |
49 |
Correct |
6 ms |
8284 KB |
Output is correct |
50 |
Correct |
6 ms |
8284 KB |
Output is correct |
51 |
Correct |
5 ms |
8284 KB |
Output is correct |
52 |
Correct |
6 ms |
8284 KB |
Output is correct |
53 |
Correct |
9 ms |
8540 KB |
Output is correct |
54 |
Correct |
6 ms |
8336 KB |
Output is correct |
55 |
Correct |
9 ms |
8284 KB |
Output is correct |
56 |
Correct |
5 ms |
8296 KB |
Output is correct |
57 |
Correct |
9 ms |
8028 KB |
Output is correct |
58 |
Correct |
9 ms |
8032 KB |
Output is correct |
59 |
Correct |
10 ms |
8028 KB |
Output is correct |
60 |
Correct |
10 ms |
8036 KB |
Output is correct |
61 |
Correct |
7 ms |
8284 KB |
Output is correct |
62 |
Correct |
7 ms |
8300 KB |
Output is correct |
63 |
Correct |
7 ms |
8284 KB |
Output is correct |
64 |
Correct |
8 ms |
8064 KB |
Output is correct |
65 |
Correct |
11 ms |
8088 KB |
Output is correct |
66 |
Correct |
7 ms |
8000 KB |
Output is correct |
67 |
Correct |
8 ms |
8028 KB |
Output is correct |
68 |
Correct |
7 ms |
8284 KB |
Output is correct |
69 |
Correct |
9 ms |
8372 KB |
Output is correct |
70 |
Correct |
9 ms |
8284 KB |
Output is correct |
71 |
Correct |
9 ms |
8240 KB |
Output is correct |
72 |
Correct |
9 ms |
8028 KB |
Output is correct |
73 |
Correct |
14 ms |
8052 KB |
Output is correct |
74 |
Correct |
6 ms |
8280 KB |
Output is correct |
75 |
Correct |
10 ms |
8128 KB |
Output is correct |
76 |
Correct |
8 ms |
8024 KB |
Output is correct |
77 |
Correct |
7 ms |
8024 KB |
Output is correct |
78 |
Correct |
5 ms |
8028 KB |
Output is correct |
79 |
Correct |
6 ms |
8028 KB |
Output is correct |
80 |
Correct |
9 ms |
8024 KB |
Output is correct |
81 |
Correct |
9 ms |
8028 KB |
Output is correct |
82 |
Correct |
7 ms |
8028 KB |
Output is correct |
83 |
Correct |
6 ms |
8028 KB |
Output is correct |
84 |
Correct |
6 ms |
8028 KB |
Output is correct |
85 |
Correct |
6 ms |
8028 KB |
Output is correct |
86 |
Correct |
6 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7376 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
4 ms |
7524 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
4 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7528 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7512 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
4 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7524 KB |
Output is correct |
19 |
Correct |
3 ms |
7516 KB |
Output is correct |
20 |
Correct |
3 ms |
7516 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
3 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
3 ms |
7516 KB |
Output is correct |
28 |
Correct |
4 ms |
7640 KB |
Output is correct |
29 |
Correct |
3 ms |
7516 KB |
Output is correct |
30 |
Correct |
4 ms |
7512 KB |
Output is correct |
31 |
Correct |
5 ms |
7620 KB |
Output is correct |
32 |
Correct |
3 ms |
7640 KB |
Output is correct |
33 |
Correct |
4 ms |
7580 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
7516 KB |
Output is correct |
36 |
Correct |
3 ms |
7512 KB |
Output is correct |
37 |
Correct |
4 ms |
7516 KB |
Output is correct |
38 |
Correct |
3 ms |
7516 KB |
Output is correct |
39 |
Correct |
5 ms |
7516 KB |
Output is correct |
40 |
Correct |
4 ms |
7512 KB |
Output is correct |
41 |
Correct |
4 ms |
7512 KB |
Output is correct |
42 |
Correct |
4 ms |
7560 KB |
Output is correct |
43 |
Correct |
3 ms |
7516 KB |
Output is correct |
44 |
Correct |
4 ms |
7516 KB |
Output is correct |
45 |
Correct |
4 ms |
7516 KB |
Output is correct |
46 |
Correct |
8 ms |
8200 KB |
Output is correct |
47 |
Correct |
8 ms |
8028 KB |
Output is correct |
48 |
Correct |
7 ms |
8028 KB |
Output is correct |
49 |
Correct |
6 ms |
8284 KB |
Output is correct |
50 |
Correct |
6 ms |
8284 KB |
Output is correct |
51 |
Correct |
5 ms |
8284 KB |
Output is correct |
52 |
Correct |
6 ms |
8284 KB |
Output is correct |
53 |
Correct |
9 ms |
8540 KB |
Output is correct |
54 |
Correct |
6 ms |
8336 KB |
Output is correct |
55 |
Correct |
9 ms |
8284 KB |
Output is correct |
56 |
Correct |
5 ms |
8296 KB |
Output is correct |
57 |
Correct |
9 ms |
8028 KB |
Output is correct |
58 |
Correct |
9 ms |
8032 KB |
Output is correct |
59 |
Correct |
10 ms |
8028 KB |
Output is correct |
60 |
Correct |
10 ms |
8036 KB |
Output is correct |
61 |
Correct |
7 ms |
8284 KB |
Output is correct |
62 |
Correct |
7 ms |
8300 KB |
Output is correct |
63 |
Correct |
7 ms |
8284 KB |
Output is correct |
64 |
Correct |
8 ms |
8064 KB |
Output is correct |
65 |
Correct |
11 ms |
8088 KB |
Output is correct |
66 |
Correct |
7 ms |
8000 KB |
Output is correct |
67 |
Correct |
8 ms |
8028 KB |
Output is correct |
68 |
Correct |
7 ms |
8284 KB |
Output is correct |
69 |
Correct |
9 ms |
8372 KB |
Output is correct |
70 |
Correct |
9 ms |
8284 KB |
Output is correct |
71 |
Correct |
9 ms |
8240 KB |
Output is correct |
72 |
Correct |
9 ms |
8028 KB |
Output is correct |
73 |
Correct |
14 ms |
8052 KB |
Output is correct |
74 |
Correct |
6 ms |
8280 KB |
Output is correct |
75 |
Correct |
10 ms |
8128 KB |
Output is correct |
76 |
Correct |
8 ms |
8024 KB |
Output is correct |
77 |
Correct |
7 ms |
8024 KB |
Output is correct |
78 |
Correct |
5 ms |
8028 KB |
Output is correct |
79 |
Correct |
6 ms |
8028 KB |
Output is correct |
80 |
Correct |
9 ms |
8024 KB |
Output is correct |
81 |
Correct |
9 ms |
8028 KB |
Output is correct |
82 |
Correct |
7 ms |
8028 KB |
Output is correct |
83 |
Correct |
6 ms |
8028 KB |
Output is correct |
84 |
Correct |
6 ms |
8028 KB |
Output is correct |
85 |
Correct |
6 ms |
8028 KB |
Output is correct |
86 |
Correct |
6 ms |
8028 KB |
Output is correct |
87 |
Correct |
16 ms |
9052 KB |
Output is correct |
88 |
Correct |
50 ms |
12476 KB |
Output is correct |
89 |
Correct |
267 ms |
24128 KB |
Output is correct |
90 |
Correct |
264 ms |
23904 KB |
Output is correct |
91 |
Correct |
230 ms |
24032 KB |
Output is correct |
92 |
Correct |
125 ms |
28812 KB |
Output is correct |
93 |
Correct |
126 ms |
28848 KB |
Output is correct |
94 |
Correct |
124 ms |
28856 KB |
Output is correct |
95 |
Correct |
111 ms |
28304 KB |
Output is correct |
96 |
Correct |
139 ms |
28584 KB |
Output is correct |
97 |
Correct |
130 ms |
28664 KB |
Output is correct |
98 |
Correct |
130 ms |
28640 KB |
Output is correct |
99 |
Correct |
115 ms |
28008 KB |
Output is correct |
100 |
Correct |
337 ms |
22740 KB |
Output is correct |
101 |
Correct |
400 ms |
23052 KB |
Output is correct |
102 |
Correct |
429 ms |
23252 KB |
Output is correct |
103 |
Correct |
352 ms |
23244 KB |
Output is correct |
104 |
Correct |
150 ms |
27884 KB |
Output is correct |
105 |
Correct |
133 ms |
27852 KB |
Output is correct |
106 |
Correct |
114 ms |
27876 KB |
Output is correct |
107 |
Correct |
198 ms |
22732 KB |
Output is correct |
108 |
Correct |
179 ms |
22876 KB |
Output is correct |
109 |
Correct |
218 ms |
23300 KB |
Output is correct |
110 |
Correct |
92 ms |
27968 KB |
Output is correct |
111 |
Correct |
115 ms |
28320 KB |
Output is correct |
112 |
Correct |
98 ms |
27600 KB |
Output is correct |
113 |
Correct |
89 ms |
26828 KB |
Output is correct |
114 |
Correct |
307 ms |
22804 KB |
Output is correct |
115 |
Correct |
302 ms |
22164 KB |
Output is correct |
116 |
Correct |
96 ms |
26900 KB |
Output is correct |
117 |
Correct |
123 ms |
25548 KB |
Output is correct |
118 |
Correct |
145 ms |
24780 KB |
Output is correct |
119 |
Correct |
108 ms |
24524 KB |
Output is correct |
120 |
Correct |
95 ms |
24780 KB |
Output is correct |
121 |
Correct |
90 ms |
24192 KB |
Output is correct |
122 |
Correct |
94 ms |
23772 KB |
Output is correct |
123 |
Correct |
116 ms |
25632 KB |
Output is correct |
124 |
Correct |
142 ms |
25012 KB |
Output is correct |
125 |
Correct |
129 ms |
24624 KB |
Output is correct |
126 |
Correct |
109 ms |
24852 KB |
Output is correct |
127 |
Correct |
114 ms |
24208 KB |
Output is correct |
128 |
Correct |
118 ms |
23824 KB |
Output is correct |