#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
int C[100005];
int Cnew[100005];
int Pa[100005];
int Ch[100005];
int PaC[100005];
long long num;
int findRoot(int u)
{
num = 1;
while (Pa[u] != 0)
{
u = Pa[u];
num++;
}
return u;
}
void cut(int v, int u)
{
// printf("cut %d %d\n", v, u);
PaC[v] = u;
Pa[v] = 0;
}
void link(int v, int u)
{
int r = C[v], uu, vv;
while (u != 0)
{
// printf("link %d %d\n", v, u);
Pa[v] = u;
cut(Ch[u], u);
uu = Ch[u];
Ch[u] = v;
vv = findRoot(u);
Cnew[uu] = Cnew[vv];
v = vv;
u = PaC[v];
}
Cnew[v] = r;
}
long long Inv;
int Fen[100005];
int N;
void update(int pos, int v)
{
for (; pos > 0; pos -= pos&-pos)
Fen[pos] += v;
}
int query(int pos)
{
int r = 0;
for (; pos <= N; pos += pos&-pos)
r += Fen[pos];
return r;
}
std::queue<std::pair<int, int> > V;
void findInv(int u)
{
// printf("findInv %d\n", u);
int uu = findRoot(u);
long long r = num;
if (PaC[uu] == 0)
{
update(Cnew[uu], r);
V.push({Cnew[uu], r});
return;
}
findInv(PaC[uu]);
Inv += r*query(Cnew[uu] + 1);
update(Cnew[uu], r);
V.push({Cnew[uu], r});
}
std::vector<int> S;
int main()
{
scanf("%d", &N);
int i;
for (i = 1; i <= N; i++)
{
scanf("%d", &C[i]);
S.push_back(C[i]);
}
std::sort(S.begin(), S.end());
for (i = 1; i <= N; i++)
C[i] = std::lower_bound(S.begin(), S.end(), C[i]) - S.begin() + 1;
int u, v;
for (i = 1; i < N; i++)
{
scanf("%d %d", &u, &v);
Inv = 0;
findInv(u);
printf("%lld\n", Inv);
while (!V.empty())
{
update(V.front().first, -V.front().second);
V.pop();
}
link(v, u);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
772 KB |
Output is correct |
8 |
Correct |
3 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
864 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
3 ms |
872 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
2 ms |
912 KB |
Output is correct |
20 |
Correct |
3 ms |
928 KB |
Output is correct |
21 |
Correct |
3 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
1008 KB |
Output is correct |
23 |
Correct |
2 ms |
1016 KB |
Output is correct |
24 |
Correct |
2 ms |
1024 KB |
Output is correct |
25 |
Correct |
3 ms |
1032 KB |
Output is correct |
26 |
Correct |
3 ms |
1044 KB |
Output is correct |
27 |
Correct |
2 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1152 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1152 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
3 ms |
1152 KB |
Output is correct |
35 |
Correct |
2 ms |
1152 KB |
Output is correct |
36 |
Correct |
3 ms |
1152 KB |
Output is correct |
37 |
Correct |
2 ms |
1152 KB |
Output is correct |
38 |
Correct |
2 ms |
1152 KB |
Output is correct |
39 |
Correct |
3 ms |
1152 KB |
Output is correct |
40 |
Correct |
2 ms |
1152 KB |
Output is correct |
41 |
Correct |
2 ms |
1160 KB |
Output is correct |
42 |
Correct |
2 ms |
1168 KB |
Output is correct |
43 |
Correct |
2 ms |
1276 KB |
Output is correct |
44 |
Correct |
2 ms |
1276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
772 KB |
Output is correct |
8 |
Correct |
3 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
864 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
3 ms |
872 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
2 ms |
912 KB |
Output is correct |
20 |
Correct |
3 ms |
928 KB |
Output is correct |
21 |
Correct |
3 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
1008 KB |
Output is correct |
23 |
Correct |
2 ms |
1016 KB |
Output is correct |
24 |
Correct |
2 ms |
1024 KB |
Output is correct |
25 |
Correct |
3 ms |
1032 KB |
Output is correct |
26 |
Correct |
3 ms |
1044 KB |
Output is correct |
27 |
Correct |
2 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1152 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1152 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
3 ms |
1152 KB |
Output is correct |
35 |
Correct |
2 ms |
1152 KB |
Output is correct |
36 |
Correct |
3 ms |
1152 KB |
Output is correct |
37 |
Correct |
2 ms |
1152 KB |
Output is correct |
38 |
Correct |
2 ms |
1152 KB |
Output is correct |
39 |
Correct |
3 ms |
1152 KB |
Output is correct |
40 |
Correct |
2 ms |
1152 KB |
Output is correct |
41 |
Correct |
2 ms |
1160 KB |
Output is correct |
42 |
Correct |
2 ms |
1168 KB |
Output is correct |
43 |
Correct |
2 ms |
1276 KB |
Output is correct |
44 |
Correct |
2 ms |
1276 KB |
Output is correct |
45 |
Correct |
3 ms |
1276 KB |
Output is correct |
46 |
Correct |
6 ms |
1340 KB |
Output is correct |
47 |
Correct |
7 ms |
1416 KB |
Output is correct |
48 |
Correct |
6 ms |
1492 KB |
Output is correct |
49 |
Correct |
49 ms |
1696 KB |
Output is correct |
50 |
Correct |
49 ms |
1696 KB |
Output is correct |
51 |
Correct |
50 ms |
1848 KB |
Output is correct |
52 |
Correct |
30 ms |
1928 KB |
Output is correct |
53 |
Correct |
31 ms |
1948 KB |
Output is correct |
54 |
Correct |
28 ms |
2060 KB |
Output is correct |
55 |
Correct |
27 ms |
2136 KB |
Output is correct |
56 |
Correct |
28 ms |
2136 KB |
Output is correct |
57 |
Correct |
8 ms |
2160 KB |
Output is correct |
58 |
Correct |
8 ms |
2280 KB |
Output is correct |
59 |
Correct |
8 ms |
2280 KB |
Output is correct |
60 |
Correct |
8 ms |
2388 KB |
Output is correct |
61 |
Correct |
27 ms |
2540 KB |
Output is correct |
62 |
Correct |
28 ms |
2664 KB |
Output is correct |
63 |
Correct |
27 ms |
2664 KB |
Output is correct |
64 |
Correct |
6 ms |
2688 KB |
Output is correct |
65 |
Correct |
5 ms |
2688 KB |
Output is correct |
66 |
Correct |
7 ms |
2784 KB |
Output is correct |
67 |
Correct |
6 ms |
2836 KB |
Output is correct |
68 |
Correct |
50 ms |
2916 KB |
Output is correct |
69 |
Correct |
28 ms |
3068 KB |
Output is correct |
70 |
Correct |
27 ms |
3124 KB |
Output is correct |
71 |
Correct |
28 ms |
3124 KB |
Output is correct |
72 |
Correct |
10 ms |
3124 KB |
Output is correct |
73 |
Correct |
8 ms |
3144 KB |
Output is correct |
74 |
Correct |
26 ms |
3196 KB |
Output is correct |
75 |
Correct |
16 ms |
3372 KB |
Output is correct |
76 |
Correct |
12 ms |
3372 KB |
Output is correct |
77 |
Correct |
11 ms |
3404 KB |
Output is correct |
78 |
Correct |
16 ms |
3516 KB |
Output is correct |
79 |
Correct |
12 ms |
3516 KB |
Output is correct |
80 |
Correct |
10 ms |
3628 KB |
Output is correct |
81 |
Correct |
18 ms |
3804 KB |
Output is correct |
82 |
Correct |
13 ms |
3804 KB |
Output is correct |
83 |
Correct |
11 ms |
3868 KB |
Output is correct |
84 |
Correct |
16 ms |
4060 KB |
Output is correct |
85 |
Correct |
15 ms |
4060 KB |
Output is correct |
86 |
Correct |
11 ms |
4060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
772 KB |
Output is correct |
8 |
Correct |
3 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
864 KB |
Output is correct |
16 |
Correct |
2 ms |
872 KB |
Output is correct |
17 |
Correct |
3 ms |
872 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
2 ms |
912 KB |
Output is correct |
20 |
Correct |
3 ms |
928 KB |
Output is correct |
21 |
Correct |
3 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
1008 KB |
Output is correct |
23 |
Correct |
2 ms |
1016 KB |
Output is correct |
24 |
Correct |
2 ms |
1024 KB |
Output is correct |
25 |
Correct |
3 ms |
1032 KB |
Output is correct |
26 |
Correct |
3 ms |
1044 KB |
Output is correct |
27 |
Correct |
2 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1152 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1152 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
3 ms |
1152 KB |
Output is correct |
35 |
Correct |
2 ms |
1152 KB |
Output is correct |
36 |
Correct |
3 ms |
1152 KB |
Output is correct |
37 |
Correct |
2 ms |
1152 KB |
Output is correct |
38 |
Correct |
2 ms |
1152 KB |
Output is correct |
39 |
Correct |
3 ms |
1152 KB |
Output is correct |
40 |
Correct |
2 ms |
1152 KB |
Output is correct |
41 |
Correct |
2 ms |
1160 KB |
Output is correct |
42 |
Correct |
2 ms |
1168 KB |
Output is correct |
43 |
Correct |
2 ms |
1276 KB |
Output is correct |
44 |
Correct |
2 ms |
1276 KB |
Output is correct |
45 |
Correct |
3 ms |
1276 KB |
Output is correct |
46 |
Correct |
6 ms |
1340 KB |
Output is correct |
47 |
Correct |
7 ms |
1416 KB |
Output is correct |
48 |
Correct |
6 ms |
1492 KB |
Output is correct |
49 |
Correct |
49 ms |
1696 KB |
Output is correct |
50 |
Correct |
49 ms |
1696 KB |
Output is correct |
51 |
Correct |
50 ms |
1848 KB |
Output is correct |
52 |
Correct |
30 ms |
1928 KB |
Output is correct |
53 |
Correct |
31 ms |
1948 KB |
Output is correct |
54 |
Correct |
28 ms |
2060 KB |
Output is correct |
55 |
Correct |
27 ms |
2136 KB |
Output is correct |
56 |
Correct |
28 ms |
2136 KB |
Output is correct |
57 |
Correct |
8 ms |
2160 KB |
Output is correct |
58 |
Correct |
8 ms |
2280 KB |
Output is correct |
59 |
Correct |
8 ms |
2280 KB |
Output is correct |
60 |
Correct |
8 ms |
2388 KB |
Output is correct |
61 |
Correct |
27 ms |
2540 KB |
Output is correct |
62 |
Correct |
28 ms |
2664 KB |
Output is correct |
63 |
Correct |
27 ms |
2664 KB |
Output is correct |
64 |
Correct |
6 ms |
2688 KB |
Output is correct |
65 |
Correct |
5 ms |
2688 KB |
Output is correct |
66 |
Correct |
7 ms |
2784 KB |
Output is correct |
67 |
Correct |
6 ms |
2836 KB |
Output is correct |
68 |
Correct |
50 ms |
2916 KB |
Output is correct |
69 |
Correct |
28 ms |
3068 KB |
Output is correct |
70 |
Correct |
27 ms |
3124 KB |
Output is correct |
71 |
Correct |
28 ms |
3124 KB |
Output is correct |
72 |
Correct |
10 ms |
3124 KB |
Output is correct |
73 |
Correct |
8 ms |
3144 KB |
Output is correct |
74 |
Correct |
26 ms |
3196 KB |
Output is correct |
75 |
Correct |
16 ms |
3372 KB |
Output is correct |
76 |
Correct |
12 ms |
3372 KB |
Output is correct |
77 |
Correct |
11 ms |
3404 KB |
Output is correct |
78 |
Correct |
16 ms |
3516 KB |
Output is correct |
79 |
Correct |
12 ms |
3516 KB |
Output is correct |
80 |
Correct |
10 ms |
3628 KB |
Output is correct |
81 |
Correct |
18 ms |
3804 KB |
Output is correct |
82 |
Correct |
13 ms |
3804 KB |
Output is correct |
83 |
Correct |
11 ms |
3868 KB |
Output is correct |
84 |
Correct |
16 ms |
4060 KB |
Output is correct |
85 |
Correct |
15 ms |
4060 KB |
Output is correct |
86 |
Correct |
11 ms |
4060 KB |
Output is correct |
87 |
Correct |
15 ms |
4336 KB |
Output is correct |
88 |
Correct |
48 ms |
5680 KB |
Output is correct |
89 |
Correct |
216 ms |
9812 KB |
Output is correct |
90 |
Correct |
194 ms |
11988 KB |
Output is correct |
91 |
Correct |
195 ms |
14164 KB |
Output is correct |
92 |
Execution timed out |
1077 ms |
14572 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |