#include "bits/stdc++.h"
using namespace std;
typedef pair <int, int> pii;
int l[100010], r[100010];
int c[100010];
int n;
vector <int> g[100010];
int sub[100010];
int big[100010];
int head[100010];
int pos[100010];
void dfs(int x) {
big[x] = -1;
sub[x] = 1;
for(auto i : g[x]) {
dfs(i);
sub[x] += sub[i];
if(big[x] == -1 || sub[big[x]] < sub[i]) {
big[x] = i;
}
}
}
int mx[100010 * 4];
int mn[100010 * 4];
int prop[100010 * 4];
int par[100010];
int dp[20][100010];
const int logn = 19;
void pushdown(int c) {
if(prop[c] == -1) return ;
int l = c << 1;
int r = l + 1;
prop[l] = prop[r] = prop[c];
mx[l] = mx[r] = prop[c];
mn[l] = mn[r] = prop[c];
prop[c] = -1;
}
void update(int x, int y, int val, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
mn[c] = mx[c] = val;
prop[c] = val;
return ;
}
if(y < b || e < x) return ;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
update(x, y, val, l, b, m);
update(x, y, val, r, m+1, e);
mn[c] = min(mn[l], mn[r]);
mx[c] = max(mx[l], mx[r]);
}
int query(int x, int val, int c = 1, int b = 1, int e = n) {
if(mx[c] == mn[c]) {
return mx[c] == val ? x - b + 1 : 0;
}
if(b == e) return mx[c] == val;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
if(x <= m) return query(x, val, l, b, m);
else {
int len = query(x, val, r, m + 1, e);
if(x - m == len) {
return query(m, val, l, b, m) + len;
} else {
return len;
}
}
}
int queryMin(int x, int y, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) return mn[c];
if(y < b || e < x) return INT_MAX;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
return min(queryMin(x, y, l, b, m), queryMin(x, y, r, m+1, e));
}
int queryMax(int x, int y, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) return mx[c];
if(y < b || e < x) return INT_MIN;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
return max(queryMax(x, y, l, b, m), queryMax(x, y, r, m+1, e));
}
int search(int b, int e, int idx) {
if(b == e) {
return idx - b + 1;
}
int m = (b + e) >> 1;
if(queryMin(m, idx) == queryMax(m, idx)) return search(b, m, idx);
else return search(m + 1, e, idx);
}
int get(int x, int c = 1, int b = 1, int e = n) {
if(b == e) return mx[c];
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
if(x <= m) return get(x, l, b, m);
else return get(x, r, m+1, e);
}
void precalc() {
memset(prop, -1, sizeof prop);
int cur = 0;
for(int i = 1; i <= n; i++) {
if(par[i] == -1 || big[par[i]] != i) {
for(int j = i; j != -1; j = big[j]) {
pos[j] = ++cur;
head[j] = i;
}
}
}
for(int i = 1; i <= n; i++) {
update(pos[i], pos[i], c[i]);
}
dp[0][1] = 0;
for(int i = 2; i <= n; i++) dp[0][i] = par[i];
for(int i = 1; i <= logn; i++) {
for(int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
}
void change(int x) {
int cur = x;
while(cur != -1) {
update(pos[head[cur]], pos[cur], c[x]);
cur = head[cur];
cur = par[cur];
}
}
int lift(int node, int up) {
for(int i = logn; i >= 0; i--) {
if((1 << i) <= up) {
node = dp[i][node];
up -= 1 << i;
}
}
return node;
}
long long inverse(vector <pii> v) {
long long ans = 0;
for(int i = 0; i < v.size(); i++) {
for(int j = i + 1; j < v.size(); j++) {
if(v[i].first < v[j].first) ans += 1LL * v[i].second * v[j].second;
}
}
return ans;
}
long long getCost(int x) {
vector <pii> v;
int cur = par[x];
while(cur > 0) {
int val = get(pos[cur]);
int len = search(pos[head[cur]], pos[cur], pos[cur]); // query(pos[cur], val);
v.push_back(pii(val, len));
cur = lift(cur, len);
}
return inverse(v);
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
}
par[1] = -1;
for(int i = 1; i < n; i++) {
scanf("%d %d", &l[i], &r[i]);
g[l[i]].push_back(r[i]);
par[r[i]] = l[i];
}
dfs(1);
precalc();
for(int i = 1; i < n; i++) {
printf("%lld\n", getCost(r[i]));
change(r[i]);
}
return 0;
}
Compilation message
construction.cpp: In function 'long long int inverse(std::vector<std::pair<int, int> >)':
construction.cpp:159:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
construction.cpp:160:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i + 1; j < v.size(); j++) {
~~^~~~~~~~~~
construction.cpp: In function 'int main(int, const char**)':
construction.cpp:179:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:181:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:185:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &l[i], &r[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4404 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
13 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4456 KB |
Output is correct |
5 |
Correct |
7 ms |
4564 KB |
Output is correct |
6 |
Correct |
6 ms |
4564 KB |
Output is correct |
7 |
Correct |
6 ms |
4564 KB |
Output is correct |
8 |
Correct |
6 ms |
4608 KB |
Output is correct |
9 |
Correct |
7 ms |
4608 KB |
Output is correct |
10 |
Correct |
6 ms |
4608 KB |
Output is correct |
11 |
Correct |
7 ms |
4608 KB |
Output is correct |
12 |
Correct |
7 ms |
4608 KB |
Output is correct |
13 |
Correct |
7 ms |
4608 KB |
Output is correct |
14 |
Correct |
7 ms |
4616 KB |
Output is correct |
15 |
Correct |
8 ms |
4616 KB |
Output is correct |
16 |
Correct |
6 ms |
4616 KB |
Output is correct |
17 |
Correct |
6 ms |
4632 KB |
Output is correct |
18 |
Correct |
7 ms |
4688 KB |
Output is correct |
19 |
Correct |
7 ms |
4692 KB |
Output is correct |
20 |
Correct |
7 ms |
4692 KB |
Output is correct |
21 |
Correct |
8 ms |
4808 KB |
Output is correct |
22 |
Correct |
7 ms |
4808 KB |
Output is correct |
23 |
Correct |
6 ms |
4808 KB |
Output is correct |
24 |
Correct |
7 ms |
4808 KB |
Output is correct |
25 |
Correct |
6 ms |
4808 KB |
Output is correct |
26 |
Correct |
6 ms |
4808 KB |
Output is correct |
27 |
Correct |
7 ms |
4808 KB |
Output is correct |
28 |
Correct |
7 ms |
4808 KB |
Output is correct |
29 |
Correct |
7 ms |
4808 KB |
Output is correct |
30 |
Correct |
6 ms |
4808 KB |
Output is correct |
31 |
Correct |
7 ms |
4828 KB |
Output is correct |
32 |
Correct |
8 ms |
4828 KB |
Output is correct |
33 |
Correct |
7 ms |
4828 KB |
Output is correct |
34 |
Correct |
7 ms |
4828 KB |
Output is correct |
35 |
Correct |
7 ms |
4828 KB |
Output is correct |
36 |
Correct |
7 ms |
4828 KB |
Output is correct |
37 |
Correct |
7 ms |
4828 KB |
Output is correct |
38 |
Correct |
7 ms |
4828 KB |
Output is correct |
39 |
Correct |
6 ms |
4828 KB |
Output is correct |
40 |
Correct |
7 ms |
4828 KB |
Output is correct |
41 |
Correct |
7 ms |
4828 KB |
Output is correct |
42 |
Correct |
7 ms |
4828 KB |
Output is correct |
43 |
Correct |
7 ms |
4828 KB |
Output is correct |
44 |
Correct |
7 ms |
4828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4404 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
13 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4456 KB |
Output is correct |
5 |
Correct |
7 ms |
4564 KB |
Output is correct |
6 |
Correct |
6 ms |
4564 KB |
Output is correct |
7 |
Correct |
6 ms |
4564 KB |
Output is correct |
8 |
Correct |
6 ms |
4608 KB |
Output is correct |
9 |
Correct |
7 ms |
4608 KB |
Output is correct |
10 |
Correct |
6 ms |
4608 KB |
Output is correct |
11 |
Correct |
7 ms |
4608 KB |
Output is correct |
12 |
Correct |
7 ms |
4608 KB |
Output is correct |
13 |
Correct |
7 ms |
4608 KB |
Output is correct |
14 |
Correct |
7 ms |
4616 KB |
Output is correct |
15 |
Correct |
8 ms |
4616 KB |
Output is correct |
16 |
Correct |
6 ms |
4616 KB |
Output is correct |
17 |
Correct |
6 ms |
4632 KB |
Output is correct |
18 |
Correct |
7 ms |
4688 KB |
Output is correct |
19 |
Correct |
7 ms |
4692 KB |
Output is correct |
20 |
Correct |
7 ms |
4692 KB |
Output is correct |
21 |
Correct |
8 ms |
4808 KB |
Output is correct |
22 |
Correct |
7 ms |
4808 KB |
Output is correct |
23 |
Correct |
6 ms |
4808 KB |
Output is correct |
24 |
Correct |
7 ms |
4808 KB |
Output is correct |
25 |
Correct |
6 ms |
4808 KB |
Output is correct |
26 |
Correct |
6 ms |
4808 KB |
Output is correct |
27 |
Correct |
7 ms |
4808 KB |
Output is correct |
28 |
Correct |
7 ms |
4808 KB |
Output is correct |
29 |
Correct |
7 ms |
4808 KB |
Output is correct |
30 |
Correct |
6 ms |
4808 KB |
Output is correct |
31 |
Correct |
7 ms |
4828 KB |
Output is correct |
32 |
Correct |
8 ms |
4828 KB |
Output is correct |
33 |
Correct |
7 ms |
4828 KB |
Output is correct |
34 |
Correct |
7 ms |
4828 KB |
Output is correct |
35 |
Correct |
7 ms |
4828 KB |
Output is correct |
36 |
Correct |
7 ms |
4828 KB |
Output is correct |
37 |
Correct |
7 ms |
4828 KB |
Output is correct |
38 |
Correct |
7 ms |
4828 KB |
Output is correct |
39 |
Correct |
6 ms |
4828 KB |
Output is correct |
40 |
Correct |
7 ms |
4828 KB |
Output is correct |
41 |
Correct |
7 ms |
4828 KB |
Output is correct |
42 |
Correct |
7 ms |
4828 KB |
Output is correct |
43 |
Correct |
7 ms |
4828 KB |
Output is correct |
44 |
Correct |
7 ms |
4828 KB |
Output is correct |
45 |
Correct |
8 ms |
4828 KB |
Output is correct |
46 |
Correct |
26 ms |
5236 KB |
Output is correct |
47 |
Correct |
27 ms |
5236 KB |
Output is correct |
48 |
Correct |
23 ms |
5284 KB |
Output is correct |
49 |
Correct |
39 ms |
5508 KB |
Output is correct |
50 |
Correct |
32 ms |
5636 KB |
Output is correct |
51 |
Correct |
30 ms |
5636 KB |
Output is correct |
52 |
Correct |
40 ms |
5636 KB |
Output is correct |
53 |
Correct |
44 ms |
5636 KB |
Output is correct |
54 |
Correct |
41 ms |
5636 KB |
Output is correct |
55 |
Correct |
37 ms |
5636 KB |
Output is correct |
56 |
Correct |
36 ms |
5636 KB |
Output is correct |
57 |
Correct |
34 ms |
5636 KB |
Output is correct |
58 |
Correct |
27 ms |
5636 KB |
Output is correct |
59 |
Correct |
27 ms |
5636 KB |
Output is correct |
60 |
Correct |
33 ms |
5636 KB |
Output is correct |
61 |
Correct |
34 ms |
5636 KB |
Output is correct |
62 |
Correct |
40 ms |
5636 KB |
Output is correct |
63 |
Correct |
33 ms |
5636 KB |
Output is correct |
64 |
Correct |
20 ms |
5636 KB |
Output is correct |
65 |
Correct |
26 ms |
5636 KB |
Output is correct |
66 |
Correct |
23 ms |
5636 KB |
Output is correct |
67 |
Correct |
21 ms |
5636 KB |
Output is correct |
68 |
Correct |
32 ms |
5636 KB |
Output is correct |
69 |
Correct |
36 ms |
5636 KB |
Output is correct |
70 |
Correct |
40 ms |
5636 KB |
Output is correct |
71 |
Correct |
31 ms |
5636 KB |
Output is correct |
72 |
Correct |
33 ms |
5636 KB |
Output is correct |
73 |
Correct |
28 ms |
5636 KB |
Output is correct |
74 |
Correct |
40 ms |
5636 KB |
Output is correct |
75 |
Correct |
30 ms |
5636 KB |
Output is correct |
76 |
Correct |
35 ms |
5636 KB |
Output is correct |
77 |
Correct |
28 ms |
5636 KB |
Output is correct |
78 |
Correct |
32 ms |
5636 KB |
Output is correct |
79 |
Correct |
35 ms |
5636 KB |
Output is correct |
80 |
Correct |
27 ms |
5636 KB |
Output is correct |
81 |
Correct |
49 ms |
5636 KB |
Output is correct |
82 |
Correct |
31 ms |
5636 KB |
Output is correct |
83 |
Correct |
29 ms |
5636 KB |
Output is correct |
84 |
Correct |
34 ms |
5636 KB |
Output is correct |
85 |
Correct |
37 ms |
5636 KB |
Output is correct |
86 |
Correct |
29 ms |
5636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4404 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
13 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4456 KB |
Output is correct |
5 |
Correct |
7 ms |
4564 KB |
Output is correct |
6 |
Correct |
6 ms |
4564 KB |
Output is correct |
7 |
Correct |
6 ms |
4564 KB |
Output is correct |
8 |
Correct |
6 ms |
4608 KB |
Output is correct |
9 |
Correct |
7 ms |
4608 KB |
Output is correct |
10 |
Correct |
6 ms |
4608 KB |
Output is correct |
11 |
Correct |
7 ms |
4608 KB |
Output is correct |
12 |
Correct |
7 ms |
4608 KB |
Output is correct |
13 |
Correct |
7 ms |
4608 KB |
Output is correct |
14 |
Correct |
7 ms |
4616 KB |
Output is correct |
15 |
Correct |
8 ms |
4616 KB |
Output is correct |
16 |
Correct |
6 ms |
4616 KB |
Output is correct |
17 |
Correct |
6 ms |
4632 KB |
Output is correct |
18 |
Correct |
7 ms |
4688 KB |
Output is correct |
19 |
Correct |
7 ms |
4692 KB |
Output is correct |
20 |
Correct |
7 ms |
4692 KB |
Output is correct |
21 |
Correct |
8 ms |
4808 KB |
Output is correct |
22 |
Correct |
7 ms |
4808 KB |
Output is correct |
23 |
Correct |
6 ms |
4808 KB |
Output is correct |
24 |
Correct |
7 ms |
4808 KB |
Output is correct |
25 |
Correct |
6 ms |
4808 KB |
Output is correct |
26 |
Correct |
6 ms |
4808 KB |
Output is correct |
27 |
Correct |
7 ms |
4808 KB |
Output is correct |
28 |
Correct |
7 ms |
4808 KB |
Output is correct |
29 |
Correct |
7 ms |
4808 KB |
Output is correct |
30 |
Correct |
6 ms |
4808 KB |
Output is correct |
31 |
Correct |
7 ms |
4828 KB |
Output is correct |
32 |
Correct |
8 ms |
4828 KB |
Output is correct |
33 |
Correct |
7 ms |
4828 KB |
Output is correct |
34 |
Correct |
7 ms |
4828 KB |
Output is correct |
35 |
Correct |
7 ms |
4828 KB |
Output is correct |
36 |
Correct |
7 ms |
4828 KB |
Output is correct |
37 |
Correct |
7 ms |
4828 KB |
Output is correct |
38 |
Correct |
7 ms |
4828 KB |
Output is correct |
39 |
Correct |
6 ms |
4828 KB |
Output is correct |
40 |
Correct |
7 ms |
4828 KB |
Output is correct |
41 |
Correct |
7 ms |
4828 KB |
Output is correct |
42 |
Correct |
7 ms |
4828 KB |
Output is correct |
43 |
Correct |
7 ms |
4828 KB |
Output is correct |
44 |
Correct |
7 ms |
4828 KB |
Output is correct |
45 |
Correct |
8 ms |
4828 KB |
Output is correct |
46 |
Correct |
26 ms |
5236 KB |
Output is correct |
47 |
Correct |
27 ms |
5236 KB |
Output is correct |
48 |
Correct |
23 ms |
5284 KB |
Output is correct |
49 |
Correct |
39 ms |
5508 KB |
Output is correct |
50 |
Correct |
32 ms |
5636 KB |
Output is correct |
51 |
Correct |
30 ms |
5636 KB |
Output is correct |
52 |
Correct |
40 ms |
5636 KB |
Output is correct |
53 |
Correct |
44 ms |
5636 KB |
Output is correct |
54 |
Correct |
41 ms |
5636 KB |
Output is correct |
55 |
Correct |
37 ms |
5636 KB |
Output is correct |
56 |
Correct |
36 ms |
5636 KB |
Output is correct |
57 |
Correct |
34 ms |
5636 KB |
Output is correct |
58 |
Correct |
27 ms |
5636 KB |
Output is correct |
59 |
Correct |
27 ms |
5636 KB |
Output is correct |
60 |
Correct |
33 ms |
5636 KB |
Output is correct |
61 |
Correct |
34 ms |
5636 KB |
Output is correct |
62 |
Correct |
40 ms |
5636 KB |
Output is correct |
63 |
Correct |
33 ms |
5636 KB |
Output is correct |
64 |
Correct |
20 ms |
5636 KB |
Output is correct |
65 |
Correct |
26 ms |
5636 KB |
Output is correct |
66 |
Correct |
23 ms |
5636 KB |
Output is correct |
67 |
Correct |
21 ms |
5636 KB |
Output is correct |
68 |
Correct |
32 ms |
5636 KB |
Output is correct |
69 |
Correct |
36 ms |
5636 KB |
Output is correct |
70 |
Correct |
40 ms |
5636 KB |
Output is correct |
71 |
Correct |
31 ms |
5636 KB |
Output is correct |
72 |
Correct |
33 ms |
5636 KB |
Output is correct |
73 |
Correct |
28 ms |
5636 KB |
Output is correct |
74 |
Correct |
40 ms |
5636 KB |
Output is correct |
75 |
Correct |
30 ms |
5636 KB |
Output is correct |
76 |
Correct |
35 ms |
5636 KB |
Output is correct |
77 |
Correct |
28 ms |
5636 KB |
Output is correct |
78 |
Correct |
32 ms |
5636 KB |
Output is correct |
79 |
Correct |
35 ms |
5636 KB |
Output is correct |
80 |
Correct |
27 ms |
5636 KB |
Output is correct |
81 |
Correct |
49 ms |
5636 KB |
Output is correct |
82 |
Correct |
31 ms |
5636 KB |
Output is correct |
83 |
Correct |
29 ms |
5636 KB |
Output is correct |
84 |
Correct |
34 ms |
5636 KB |
Output is correct |
85 |
Correct |
37 ms |
5636 KB |
Output is correct |
86 |
Correct |
29 ms |
5636 KB |
Output is correct |
87 |
Correct |
59 ms |
6188 KB |
Output is correct |
88 |
Correct |
179 ms |
9000 KB |
Output is correct |
89 |
Correct |
897 ms |
19500 KB |
Output is correct |
90 |
Correct |
883 ms |
19500 KB |
Output is correct |
91 |
Correct |
929 ms |
19588 KB |
Output is correct |
92 |
Execution timed out |
1086 ms |
25656 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |