#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));
while(len--) cur = par[cur];
}
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]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
5 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4524 KB |
Output is correct |
5 |
Correct |
6 ms |
4524 KB |
Output is correct |
6 |
Correct |
6 ms |
4540 KB |
Output is correct |
7 |
Correct |
6 ms |
4652 KB |
Output is correct |
8 |
Correct |
7 ms |
4652 KB |
Output is correct |
9 |
Correct |
7 ms |
4652 KB |
Output is correct |
10 |
Correct |
7 ms |
4860 KB |
Output is correct |
11 |
Correct |
7 ms |
4860 KB |
Output is correct |
12 |
Correct |
7 ms |
4860 KB |
Output is correct |
13 |
Correct |
7 ms |
4860 KB |
Output is correct |
14 |
Correct |
7 ms |
4860 KB |
Output is correct |
15 |
Correct |
7 ms |
4860 KB |
Output is correct |
16 |
Correct |
6 ms |
4860 KB |
Output is correct |
17 |
Correct |
7 ms |
4860 KB |
Output is correct |
18 |
Correct |
7 ms |
4860 KB |
Output is correct |
19 |
Correct |
8 ms |
4860 KB |
Output is correct |
20 |
Correct |
9 ms |
4860 KB |
Output is correct |
21 |
Correct |
7 ms |
4860 KB |
Output is correct |
22 |
Correct |
7 ms |
4860 KB |
Output is correct |
23 |
Correct |
8 ms |
4860 KB |
Output is correct |
24 |
Correct |
6 ms |
4876 KB |
Output is correct |
25 |
Correct |
9 ms |
5028 KB |
Output is correct |
26 |
Correct |
8 ms |
5028 KB |
Output is correct |
27 |
Correct |
9 ms |
5028 KB |
Output is correct |
28 |
Correct |
9 ms |
5028 KB |
Output is correct |
29 |
Correct |
8 ms |
5028 KB |
Output is correct |
30 |
Correct |
7 ms |
5028 KB |
Output is correct |
31 |
Correct |
9 ms |
5028 KB |
Output is correct |
32 |
Correct |
10 ms |
5028 KB |
Output is correct |
33 |
Correct |
9 ms |
5028 KB |
Output is correct |
34 |
Correct |
9 ms |
5028 KB |
Output is correct |
35 |
Correct |
8 ms |
5028 KB |
Output is correct |
36 |
Correct |
9 ms |
5040 KB |
Output is correct |
37 |
Correct |
9 ms |
5044 KB |
Output is correct |
38 |
Correct |
9 ms |
5084 KB |
Output is correct |
39 |
Correct |
8 ms |
5092 KB |
Output is correct |
40 |
Correct |
9 ms |
5100 KB |
Output is correct |
41 |
Correct |
8 ms |
5108 KB |
Output is correct |
42 |
Correct |
8 ms |
5108 KB |
Output is correct |
43 |
Correct |
9 ms |
5128 KB |
Output is correct |
44 |
Correct |
7 ms |
5264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
5 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4524 KB |
Output is correct |
5 |
Correct |
6 ms |
4524 KB |
Output is correct |
6 |
Correct |
6 ms |
4540 KB |
Output is correct |
7 |
Correct |
6 ms |
4652 KB |
Output is correct |
8 |
Correct |
7 ms |
4652 KB |
Output is correct |
9 |
Correct |
7 ms |
4652 KB |
Output is correct |
10 |
Correct |
7 ms |
4860 KB |
Output is correct |
11 |
Correct |
7 ms |
4860 KB |
Output is correct |
12 |
Correct |
7 ms |
4860 KB |
Output is correct |
13 |
Correct |
7 ms |
4860 KB |
Output is correct |
14 |
Correct |
7 ms |
4860 KB |
Output is correct |
15 |
Correct |
7 ms |
4860 KB |
Output is correct |
16 |
Correct |
6 ms |
4860 KB |
Output is correct |
17 |
Correct |
7 ms |
4860 KB |
Output is correct |
18 |
Correct |
7 ms |
4860 KB |
Output is correct |
19 |
Correct |
8 ms |
4860 KB |
Output is correct |
20 |
Correct |
9 ms |
4860 KB |
Output is correct |
21 |
Correct |
7 ms |
4860 KB |
Output is correct |
22 |
Correct |
7 ms |
4860 KB |
Output is correct |
23 |
Correct |
8 ms |
4860 KB |
Output is correct |
24 |
Correct |
6 ms |
4876 KB |
Output is correct |
25 |
Correct |
9 ms |
5028 KB |
Output is correct |
26 |
Correct |
8 ms |
5028 KB |
Output is correct |
27 |
Correct |
9 ms |
5028 KB |
Output is correct |
28 |
Correct |
9 ms |
5028 KB |
Output is correct |
29 |
Correct |
8 ms |
5028 KB |
Output is correct |
30 |
Correct |
7 ms |
5028 KB |
Output is correct |
31 |
Correct |
9 ms |
5028 KB |
Output is correct |
32 |
Correct |
10 ms |
5028 KB |
Output is correct |
33 |
Correct |
9 ms |
5028 KB |
Output is correct |
34 |
Correct |
9 ms |
5028 KB |
Output is correct |
35 |
Correct |
8 ms |
5028 KB |
Output is correct |
36 |
Correct |
9 ms |
5040 KB |
Output is correct |
37 |
Correct |
9 ms |
5044 KB |
Output is correct |
38 |
Correct |
9 ms |
5084 KB |
Output is correct |
39 |
Correct |
8 ms |
5092 KB |
Output is correct |
40 |
Correct |
9 ms |
5100 KB |
Output is correct |
41 |
Correct |
8 ms |
5108 KB |
Output is correct |
42 |
Correct |
8 ms |
5108 KB |
Output is correct |
43 |
Correct |
9 ms |
5128 KB |
Output is correct |
44 |
Correct |
7 ms |
5264 KB |
Output is correct |
45 |
Correct |
11 ms |
5264 KB |
Output is correct |
46 |
Correct |
27 ms |
5804 KB |
Output is correct |
47 |
Correct |
32 ms |
5880 KB |
Output is correct |
48 |
Correct |
29 ms |
5880 KB |
Output is correct |
49 |
Correct |
46 ms |
6240 KB |
Output is correct |
50 |
Correct |
65 ms |
6312 KB |
Output is correct |
51 |
Correct |
55 ms |
6440 KB |
Output is correct |
52 |
Correct |
64 ms |
6440 KB |
Output is correct |
53 |
Correct |
49 ms |
6440 KB |
Output is correct |
54 |
Correct |
68 ms |
6552 KB |
Output is correct |
55 |
Correct |
51 ms |
6552 KB |
Output is correct |
56 |
Correct |
43 ms |
6576 KB |
Output is correct |
57 |
Correct |
26 ms |
6576 KB |
Output is correct |
58 |
Correct |
30 ms |
6752 KB |
Output is correct |
59 |
Correct |
28 ms |
6752 KB |
Output is correct |
60 |
Correct |
30 ms |
6752 KB |
Output is correct |
61 |
Correct |
43 ms |
6936 KB |
Output is correct |
62 |
Correct |
47 ms |
6948 KB |
Output is correct |
63 |
Correct |
44 ms |
7216 KB |
Output is correct |
64 |
Correct |
21 ms |
7216 KB |
Output is correct |
65 |
Correct |
23 ms |
7216 KB |
Output is correct |
66 |
Correct |
28 ms |
7344 KB |
Output is correct |
67 |
Correct |
30 ms |
7344 KB |
Output is correct |
68 |
Correct |
44 ms |
7532 KB |
Output is correct |
69 |
Correct |
42 ms |
7532 KB |
Output is correct |
70 |
Correct |
47 ms |
7800 KB |
Output is correct |
71 |
Correct |
42 ms |
7800 KB |
Output is correct |
72 |
Correct |
26 ms |
7800 KB |
Output is correct |
73 |
Correct |
27 ms |
7800 KB |
Output is correct |
74 |
Correct |
44 ms |
7800 KB |
Output is correct |
75 |
Correct |
42 ms |
7800 KB |
Output is correct |
76 |
Correct |
33 ms |
7800 KB |
Output is correct |
77 |
Correct |
31 ms |
7800 KB |
Output is correct |
78 |
Correct |
35 ms |
7840 KB |
Output is correct |
79 |
Correct |
31 ms |
7896 KB |
Output is correct |
80 |
Correct |
32 ms |
8060 KB |
Output is correct |
81 |
Correct |
46 ms |
8060 KB |
Output is correct |
82 |
Correct |
34 ms |
8160 KB |
Output is correct |
83 |
Correct |
36 ms |
8160 KB |
Output is correct |
84 |
Correct |
36 ms |
8172 KB |
Output is correct |
85 |
Correct |
34 ms |
8224 KB |
Output is correct |
86 |
Correct |
31 ms |
8268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4456 KB |
Output is correct |
3 |
Correct |
5 ms |
4456 KB |
Output is correct |
4 |
Correct |
5 ms |
4524 KB |
Output is correct |
5 |
Correct |
6 ms |
4524 KB |
Output is correct |
6 |
Correct |
6 ms |
4540 KB |
Output is correct |
7 |
Correct |
6 ms |
4652 KB |
Output is correct |
8 |
Correct |
7 ms |
4652 KB |
Output is correct |
9 |
Correct |
7 ms |
4652 KB |
Output is correct |
10 |
Correct |
7 ms |
4860 KB |
Output is correct |
11 |
Correct |
7 ms |
4860 KB |
Output is correct |
12 |
Correct |
7 ms |
4860 KB |
Output is correct |
13 |
Correct |
7 ms |
4860 KB |
Output is correct |
14 |
Correct |
7 ms |
4860 KB |
Output is correct |
15 |
Correct |
7 ms |
4860 KB |
Output is correct |
16 |
Correct |
6 ms |
4860 KB |
Output is correct |
17 |
Correct |
7 ms |
4860 KB |
Output is correct |
18 |
Correct |
7 ms |
4860 KB |
Output is correct |
19 |
Correct |
8 ms |
4860 KB |
Output is correct |
20 |
Correct |
9 ms |
4860 KB |
Output is correct |
21 |
Correct |
7 ms |
4860 KB |
Output is correct |
22 |
Correct |
7 ms |
4860 KB |
Output is correct |
23 |
Correct |
8 ms |
4860 KB |
Output is correct |
24 |
Correct |
6 ms |
4876 KB |
Output is correct |
25 |
Correct |
9 ms |
5028 KB |
Output is correct |
26 |
Correct |
8 ms |
5028 KB |
Output is correct |
27 |
Correct |
9 ms |
5028 KB |
Output is correct |
28 |
Correct |
9 ms |
5028 KB |
Output is correct |
29 |
Correct |
8 ms |
5028 KB |
Output is correct |
30 |
Correct |
7 ms |
5028 KB |
Output is correct |
31 |
Correct |
9 ms |
5028 KB |
Output is correct |
32 |
Correct |
10 ms |
5028 KB |
Output is correct |
33 |
Correct |
9 ms |
5028 KB |
Output is correct |
34 |
Correct |
9 ms |
5028 KB |
Output is correct |
35 |
Correct |
8 ms |
5028 KB |
Output is correct |
36 |
Correct |
9 ms |
5040 KB |
Output is correct |
37 |
Correct |
9 ms |
5044 KB |
Output is correct |
38 |
Correct |
9 ms |
5084 KB |
Output is correct |
39 |
Correct |
8 ms |
5092 KB |
Output is correct |
40 |
Correct |
9 ms |
5100 KB |
Output is correct |
41 |
Correct |
8 ms |
5108 KB |
Output is correct |
42 |
Correct |
8 ms |
5108 KB |
Output is correct |
43 |
Correct |
9 ms |
5128 KB |
Output is correct |
44 |
Correct |
7 ms |
5264 KB |
Output is correct |
45 |
Correct |
11 ms |
5264 KB |
Output is correct |
46 |
Correct |
27 ms |
5804 KB |
Output is correct |
47 |
Correct |
32 ms |
5880 KB |
Output is correct |
48 |
Correct |
29 ms |
5880 KB |
Output is correct |
49 |
Correct |
46 ms |
6240 KB |
Output is correct |
50 |
Correct |
65 ms |
6312 KB |
Output is correct |
51 |
Correct |
55 ms |
6440 KB |
Output is correct |
52 |
Correct |
64 ms |
6440 KB |
Output is correct |
53 |
Correct |
49 ms |
6440 KB |
Output is correct |
54 |
Correct |
68 ms |
6552 KB |
Output is correct |
55 |
Correct |
51 ms |
6552 KB |
Output is correct |
56 |
Correct |
43 ms |
6576 KB |
Output is correct |
57 |
Correct |
26 ms |
6576 KB |
Output is correct |
58 |
Correct |
30 ms |
6752 KB |
Output is correct |
59 |
Correct |
28 ms |
6752 KB |
Output is correct |
60 |
Correct |
30 ms |
6752 KB |
Output is correct |
61 |
Correct |
43 ms |
6936 KB |
Output is correct |
62 |
Correct |
47 ms |
6948 KB |
Output is correct |
63 |
Correct |
44 ms |
7216 KB |
Output is correct |
64 |
Correct |
21 ms |
7216 KB |
Output is correct |
65 |
Correct |
23 ms |
7216 KB |
Output is correct |
66 |
Correct |
28 ms |
7344 KB |
Output is correct |
67 |
Correct |
30 ms |
7344 KB |
Output is correct |
68 |
Correct |
44 ms |
7532 KB |
Output is correct |
69 |
Correct |
42 ms |
7532 KB |
Output is correct |
70 |
Correct |
47 ms |
7800 KB |
Output is correct |
71 |
Correct |
42 ms |
7800 KB |
Output is correct |
72 |
Correct |
26 ms |
7800 KB |
Output is correct |
73 |
Correct |
27 ms |
7800 KB |
Output is correct |
74 |
Correct |
44 ms |
7800 KB |
Output is correct |
75 |
Correct |
42 ms |
7800 KB |
Output is correct |
76 |
Correct |
33 ms |
7800 KB |
Output is correct |
77 |
Correct |
31 ms |
7800 KB |
Output is correct |
78 |
Correct |
35 ms |
7840 KB |
Output is correct |
79 |
Correct |
31 ms |
7896 KB |
Output is correct |
80 |
Correct |
32 ms |
8060 KB |
Output is correct |
81 |
Correct |
46 ms |
8060 KB |
Output is correct |
82 |
Correct |
34 ms |
8160 KB |
Output is correct |
83 |
Correct |
36 ms |
8160 KB |
Output is correct |
84 |
Correct |
36 ms |
8172 KB |
Output is correct |
85 |
Correct |
34 ms |
8224 KB |
Output is correct |
86 |
Correct |
31 ms |
8268 KB |
Output is correct |
87 |
Correct |
56 ms |
9468 KB |
Output is correct |
88 |
Correct |
185 ms |
12984 KB |
Output is correct |
89 |
Correct |
910 ms |
25520 KB |
Output is correct |
90 |
Correct |
888 ms |
27604 KB |
Output is correct |
91 |
Correct |
886 ms |
29784 KB |
Output is correct |
92 |
Execution timed out |
1073 ms |
37864 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |