#include <cstdio>
#include <vector>
#include <algorithm>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
const int N = 1e5 + 500;
const int LOG = 17;
const int OFF = (1 << LOG);
ll inv, ans[N];
vector < int > saz, v[N];
int loga[N], siz = 0, koga[N], C[N], n, cur;
int A[N], B[N], L[N], R[N];
int vrh[N], dep[N], dno[N], par[N][LOG], lan[N];
void add(int x, int y){
siz += y;
for(; x < N ; x += x & -x)
loga[x] += y;
}
int query(int x){
int ret = 0;
for(; x ; x -= x & -x)
ret += loga[x];
return ret;
}
void dfs(int x,int lst){
par[x][0] = lst; dep[x] = dep[lst] + 1;
L[x] = cur++;
for(int y : v[x])
dfs(y, x);
R[x] = cur - 1;
}
void precompute(){
for(int j = 1;j < LOG;j++)
for(int i = 1;i <= n;i++)
par[i][j] = par[par[i][j - 1]][j - 1];
}
int digni(int x,int k){
for(int i = LOG - 1;i >= 0;i--)
if(k & (1 << i)) x = par[x][i];
return x;
}
int tour[2 * OFF];
void update(int i,int x){
tour[i + OFF] = x;
for(i = (i + OFF) / 2; i ; i /= 2)
tour[i] = max(tour[2 * i], tour[2 * i + 1]);
}
int query(int i,int a,int b,int lo,int hi){
if(lo <= a && b <= hi) return tour[i];
if(a > hi || b < lo) return 0;
return max(query(2 * i, a, (a + b) / 2, lo, hi), query(2 * i + 1, (a + b) / 2 + 1, b, lo, hi));
}
int gdje(int i){
return query(1, 0, OFF - 1, L[i], R[i]);
}
ll odradi(int st){
int x = gdje(st);
ll inv = 0;
vector < pii > brisi;
for(;;){
inv += (ll)query(C[dno[x]] - 1) * (dep[st] - dep[vrh[x]] + 1);
add(C[dno[x]], dep[st] - dep[vrh[x]] + 1);
brisi.PB({C[dno[x]], dep[st] - dep[vrh[x]] + 1});
int ol_vrh = vrh[x];
if(st != dno[x]){
vrh[x] = digni(dno[x], dep[dno[x]] - dep[st] - 1);
}
if(!(ol_vrh - 1)) break;
st = par[ol_vrh][0];
x = gdje(st);
}
for(pii tmp : brisi)
add(tmp.X, -tmp.Y);
return inv;
}
int main(){
scanf("%d", &n);
for(int i = 1;i <= n;i++){
scanf("%d", C + i), saz.PB(C[i]);
}
sort(saz.begin(), saz.end());
for(int i = 1;i <= n;i++)
C[i] = lower_bound(saz.begin(), saz.end(), C[i]) - saz.begin() + 1;
for(int i = 1;i < n;i++){
scanf("%d%d", A + i, B + i);
v[A[i]].PB(B[i]);
}
dfs(1, 1); precompute();
vrh[0] = 1, dno[0] = 1;
for(int i = 1;i < n;i++){
printf("%lld\n", odradi(A[i]));
dno[i] = B[i], vrh[i] = 1;
update(L[B[i]], i);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:100:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", C + i), saz.PB(C[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
construction.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", A + i, B + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2944 KB |
Output is correct |
6 |
Correct |
7 ms |
2944 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
7 ms |
2944 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2944 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
7 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
7 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
9 ms |
2944 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2944 KB |
Output is correct |
6 |
Correct |
7 ms |
2944 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
7 ms |
2944 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2944 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
7 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
7 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
9 ms |
2944 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
45 |
Correct |
8 ms |
2944 KB |
Output is correct |
46 |
Correct |
16 ms |
3456 KB |
Output is correct |
47 |
Correct |
16 ms |
3328 KB |
Output is correct |
48 |
Correct |
16 ms |
3328 KB |
Output is correct |
49 |
Correct |
11 ms |
3584 KB |
Output is correct |
50 |
Correct |
11 ms |
3584 KB |
Output is correct |
51 |
Correct |
11 ms |
3584 KB |
Output is correct |
52 |
Correct |
11 ms |
3456 KB |
Output is correct |
53 |
Correct |
14 ms |
3668 KB |
Output is correct |
54 |
Correct |
11 ms |
3456 KB |
Output is correct |
55 |
Correct |
11 ms |
3456 KB |
Output is correct |
56 |
Correct |
12 ms |
3584 KB |
Output is correct |
57 |
Correct |
23 ms |
3328 KB |
Output is correct |
58 |
Correct |
24 ms |
3328 KB |
Output is correct |
59 |
Correct |
23 ms |
3320 KB |
Output is correct |
60 |
Correct |
23 ms |
3328 KB |
Output is correct |
61 |
Correct |
12 ms |
3456 KB |
Output is correct |
62 |
Correct |
12 ms |
3456 KB |
Output is correct |
63 |
Correct |
13 ms |
3668 KB |
Output is correct |
64 |
Correct |
14 ms |
3328 KB |
Output is correct |
65 |
Correct |
15 ms |
3376 KB |
Output is correct |
66 |
Correct |
16 ms |
3328 KB |
Output is correct |
67 |
Correct |
16 ms |
3328 KB |
Output is correct |
68 |
Correct |
10 ms |
3584 KB |
Output is correct |
69 |
Correct |
12 ms |
3456 KB |
Output is correct |
70 |
Correct |
11 ms |
3456 KB |
Output is correct |
71 |
Correct |
11 ms |
3456 KB |
Output is correct |
72 |
Correct |
23 ms |
3328 KB |
Output is correct |
73 |
Correct |
23 ms |
3328 KB |
Output is correct |
74 |
Correct |
12 ms |
3456 KB |
Output is correct |
75 |
Correct |
12 ms |
3456 KB |
Output is correct |
76 |
Correct |
12 ms |
3456 KB |
Output is correct |
77 |
Correct |
11 ms |
3328 KB |
Output is correct |
78 |
Correct |
11 ms |
3328 KB |
Output is correct |
79 |
Correct |
11 ms |
3328 KB |
Output is correct |
80 |
Correct |
11 ms |
3328 KB |
Output is correct |
81 |
Correct |
12 ms |
3456 KB |
Output is correct |
82 |
Correct |
14 ms |
3328 KB |
Output is correct |
83 |
Correct |
12 ms |
3328 KB |
Output is correct |
84 |
Correct |
12 ms |
3456 KB |
Output is correct |
85 |
Correct |
12 ms |
3328 KB |
Output is correct |
86 |
Correct |
12 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2944 KB |
Output is correct |
6 |
Correct |
7 ms |
2944 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
7 ms |
2944 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2944 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
7 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
7 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
9 ms |
2944 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
7 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
45 |
Correct |
8 ms |
2944 KB |
Output is correct |
46 |
Correct |
16 ms |
3456 KB |
Output is correct |
47 |
Correct |
16 ms |
3328 KB |
Output is correct |
48 |
Correct |
16 ms |
3328 KB |
Output is correct |
49 |
Correct |
11 ms |
3584 KB |
Output is correct |
50 |
Correct |
11 ms |
3584 KB |
Output is correct |
51 |
Correct |
11 ms |
3584 KB |
Output is correct |
52 |
Correct |
11 ms |
3456 KB |
Output is correct |
53 |
Correct |
14 ms |
3668 KB |
Output is correct |
54 |
Correct |
11 ms |
3456 KB |
Output is correct |
55 |
Correct |
11 ms |
3456 KB |
Output is correct |
56 |
Correct |
12 ms |
3584 KB |
Output is correct |
57 |
Correct |
23 ms |
3328 KB |
Output is correct |
58 |
Correct |
24 ms |
3328 KB |
Output is correct |
59 |
Correct |
23 ms |
3320 KB |
Output is correct |
60 |
Correct |
23 ms |
3328 KB |
Output is correct |
61 |
Correct |
12 ms |
3456 KB |
Output is correct |
62 |
Correct |
12 ms |
3456 KB |
Output is correct |
63 |
Correct |
13 ms |
3668 KB |
Output is correct |
64 |
Correct |
14 ms |
3328 KB |
Output is correct |
65 |
Correct |
15 ms |
3376 KB |
Output is correct |
66 |
Correct |
16 ms |
3328 KB |
Output is correct |
67 |
Correct |
16 ms |
3328 KB |
Output is correct |
68 |
Correct |
10 ms |
3584 KB |
Output is correct |
69 |
Correct |
12 ms |
3456 KB |
Output is correct |
70 |
Correct |
11 ms |
3456 KB |
Output is correct |
71 |
Correct |
11 ms |
3456 KB |
Output is correct |
72 |
Correct |
23 ms |
3328 KB |
Output is correct |
73 |
Correct |
23 ms |
3328 KB |
Output is correct |
74 |
Correct |
12 ms |
3456 KB |
Output is correct |
75 |
Correct |
12 ms |
3456 KB |
Output is correct |
76 |
Correct |
12 ms |
3456 KB |
Output is correct |
77 |
Correct |
11 ms |
3328 KB |
Output is correct |
78 |
Correct |
11 ms |
3328 KB |
Output is correct |
79 |
Correct |
11 ms |
3328 KB |
Output is correct |
80 |
Correct |
11 ms |
3328 KB |
Output is correct |
81 |
Correct |
12 ms |
3456 KB |
Output is correct |
82 |
Correct |
14 ms |
3328 KB |
Output is correct |
83 |
Correct |
12 ms |
3328 KB |
Output is correct |
84 |
Correct |
12 ms |
3456 KB |
Output is correct |
85 |
Correct |
12 ms |
3328 KB |
Output is correct |
86 |
Correct |
12 ms |
3456 KB |
Output is correct |
87 |
Correct |
33 ms |
4344 KB |
Output is correct |
88 |
Correct |
102 ms |
7416 KB |
Output is correct |
89 |
Correct |
416 ms |
18288 KB |
Output is correct |
90 |
Correct |
431 ms |
18160 KB |
Output is correct |
91 |
Correct |
416 ms |
18160 KB |
Output is correct |
92 |
Correct |
163 ms |
24176 KB |
Output is correct |
93 |
Correct |
173 ms |
24176 KB |
Output is correct |
94 |
Correct |
163 ms |
24176 KB |
Output is correct |
95 |
Correct |
184 ms |
20720 KB |
Output is correct |
96 |
Correct |
216 ms |
21228 KB |
Output is correct |
97 |
Correct |
192 ms |
21104 KB |
Output is correct |
98 |
Correct |
197 ms |
21104 KB |
Output is correct |
99 |
Correct |
201 ms |
20720 KB |
Output is correct |
100 |
Correct |
822 ms |
18416 KB |
Output is correct |
101 |
Correct |
826 ms |
18672 KB |
Output is correct |
102 |
Correct |
822 ms |
18544 KB |
Output is correct |
103 |
Correct |
807 ms |
18544 KB |
Output is correct |
104 |
Correct |
239 ms |
20464 KB |
Output is correct |
105 |
Correct |
228 ms |
20464 KB |
Output is correct |
106 |
Correct |
253 ms |
20592 KB |
Output is correct |
107 |
Correct |
398 ms |
17008 KB |
Output is correct |
108 |
Correct |
416 ms |
17680 KB |
Output is correct |
109 |
Correct |
427 ms |
17644 KB |
Output is correct |
110 |
Correct |
159 ms |
23660 KB |
Output is correct |
111 |
Correct |
188 ms |
20716 KB |
Output is correct |
112 |
Correct |
183 ms |
20464 KB |
Output is correct |
113 |
Correct |
180 ms |
19824 KB |
Output is correct |
114 |
Correct |
792 ms |
18288 KB |
Output is correct |
115 |
Correct |
806 ms |
18140 KB |
Output is correct |
116 |
Correct |
229 ms |
19952 KB |
Output is correct |
117 |
Correct |
194 ms |
18924 KB |
Output is correct |
118 |
Correct |
180 ms |
18548 KB |
Output is correct |
119 |
Correct |
183 ms |
18552 KB |
Output is correct |
120 |
Correct |
174 ms |
18540 KB |
Output is correct |
121 |
Correct |
176 ms |
18288 KB |
Output is correct |
122 |
Correct |
186 ms |
17992 KB |
Output is correct |
123 |
Correct |
240 ms |
19056 KB |
Output is correct |
124 |
Correct |
244 ms |
18672 KB |
Output is correct |
125 |
Correct |
254 ms |
18640 KB |
Output is correct |
126 |
Correct |
226 ms |
18672 KB |
Output is correct |
127 |
Correct |
231 ms |
18284 KB |
Output is correct |
128 |
Correct |
234 ms |
18156 KB |
Output is correct |