#include <bits/stdc++.h>
#define fi first
#define se second
const int N = 100100;
const int mod = 1e9 + 7;
using namespace std;
int n;
int a[N];
int A[N], B[N];
int s[N];
int st[N];
int dip[N];
int par[N];
vector<int> v[N];
vector<pair<int, int>> d[N];
void dfs(int x)
{
if (!st[x]) {
st[x] = x;
}
int big = -1;
for (int y: v[x]) {
if (big == -1 || s[y] > s[big]) {
big = y;
}
}
if (big != -1) {
st[big] = st[x];
}
for (int y: v[x]) {
dfs(y);
}
d[st[x]].push_back({a[x], 1});
}
vector<pair<int, int>> ex(int x, int k, int c)
{
int initial_k = k;
vector<pair<int, int>> res;
while (k > 0) {
int g = min(k, d[x].back().se);
d[x].back().se -= g;
k -= g;
res.push_back({d[x].back().fi, g});
if (!d[x].back().se) {
d[x].pop_back();
}
}
d[x].push_back({c, initial_k});
reverse(res.begin(), res.end());
return res;
}
int t[N];
void upd(int x, int g)
{
while (x < N) {
t[x] += g;
x += x & -x;
}
}
int get(int x)
{
int res = 0;
while (x > 0) {
res += t[x];
x -= x & -x;
}
return res;
}
long long inversions(const vector<pair<int, int>>& v)
{
long long res = 0;
for (const auto& p : v) {
res += 1ll * get(p.fi - 1) * p.se;
upd(p.fi, p.se);
}
for (const auto& p : v) {
upd(p.fi, -p.se);
}
return res;
}
long long solve(int A, int c)
{
vector<pair<int, int>> v;
while (A > 0) {
auto g = ex(st[A], dip[A] - dip[st[A]] + 1, c);
v.insert(v.end(), g.begin(), g.end());
A = par[st[A]];
}
return inversions(v);
}
int main() {
#ifdef zxc
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n;
vector<pair<int, int>> ord;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ord.push_back({a[i], i});
s[i] = 1;
}
sort(ord.begin(), ord.end());
for (int i = 0, j = 1; i < n; i++) {
if (i > 0 && ord[i].fi > ord[i - 1].fi) {
j++;
}
a[ord[i].se] = j;
}
for (int i = 1; i < n; i++) {
cin >> A[i] >> B[i];
par[B[i]] = A[i];
dip[B[i]] = dip[A[i]] + 1;
v[A[i]].push_back(B[i]);
}
for (int i = n - 1; i >= 1; i--) {
s[A[i]] += s[B[i]];
}
dfs(1);
for (int i = 1; i < n; i++) {
cout << solve(A[i], a[B[i]]) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5152 KB |
Output is correct |
10 |
Correct |
3 ms |
5128 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5020 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5020 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5024 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5088 KB |
Output is correct |
35 |
Correct |
4 ms |
5028 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5028 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
6 ms |
5020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5152 KB |
Output is correct |
10 |
Correct |
3 ms |
5128 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5020 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5020 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5024 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5088 KB |
Output is correct |
35 |
Correct |
4 ms |
5028 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5028 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
6 ms |
5020 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
8 ms |
5332 KB |
Output is correct |
47 |
Correct |
7 ms |
5416 KB |
Output is correct |
48 |
Correct |
7 ms |
5448 KB |
Output is correct |
49 |
Correct |
6 ms |
5620 KB |
Output is correct |
50 |
Correct |
6 ms |
5676 KB |
Output is correct |
51 |
Correct |
6 ms |
5676 KB |
Output is correct |
52 |
Correct |
9 ms |
5588 KB |
Output is correct |
53 |
Correct |
7 ms |
5588 KB |
Output is correct |
54 |
Correct |
7 ms |
5612 KB |
Output is correct |
55 |
Correct |
9 ms |
5540 KB |
Output is correct |
56 |
Correct |
6 ms |
5524 KB |
Output is correct |
57 |
Correct |
8 ms |
5332 KB |
Output is correct |
58 |
Correct |
9 ms |
5332 KB |
Output is correct |
59 |
Correct |
9 ms |
5332 KB |
Output is correct |
60 |
Correct |
9 ms |
5332 KB |
Output is correct |
61 |
Correct |
6 ms |
5544 KB |
Output is correct |
62 |
Correct |
7 ms |
5548 KB |
Output is correct |
63 |
Correct |
6 ms |
5460 KB |
Output is correct |
64 |
Correct |
7 ms |
5332 KB |
Output is correct |
65 |
Correct |
7 ms |
5416 KB |
Output is correct |
66 |
Correct |
7 ms |
5416 KB |
Output is correct |
67 |
Correct |
8 ms |
5420 KB |
Output is correct |
68 |
Correct |
6 ms |
5588 KB |
Output is correct |
69 |
Correct |
6 ms |
5460 KB |
Output is correct |
70 |
Correct |
6 ms |
5460 KB |
Output is correct |
71 |
Correct |
6 ms |
5460 KB |
Output is correct |
72 |
Correct |
8 ms |
5412 KB |
Output is correct |
73 |
Correct |
8 ms |
5288 KB |
Output is correct |
74 |
Correct |
6 ms |
5460 KB |
Output is correct |
75 |
Correct |
6 ms |
5516 KB |
Output is correct |
76 |
Correct |
6 ms |
5416 KB |
Output is correct |
77 |
Correct |
7 ms |
5548 KB |
Output is correct |
78 |
Correct |
6 ms |
5460 KB |
Output is correct |
79 |
Correct |
7 ms |
5460 KB |
Output is correct |
80 |
Correct |
6 ms |
5332 KB |
Output is correct |
81 |
Correct |
8 ms |
5460 KB |
Output is correct |
82 |
Correct |
6 ms |
5460 KB |
Output is correct |
83 |
Correct |
6 ms |
5460 KB |
Output is correct |
84 |
Correct |
6 ms |
5460 KB |
Output is correct |
85 |
Correct |
6 ms |
5344 KB |
Output is correct |
86 |
Correct |
6 ms |
5448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5152 KB |
Output is correct |
10 |
Correct |
3 ms |
5128 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5020 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5020 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5024 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5088 KB |
Output is correct |
35 |
Correct |
4 ms |
5028 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5028 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
6 ms |
5020 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
8 ms |
5332 KB |
Output is correct |
47 |
Correct |
7 ms |
5416 KB |
Output is correct |
48 |
Correct |
7 ms |
5448 KB |
Output is correct |
49 |
Correct |
6 ms |
5620 KB |
Output is correct |
50 |
Correct |
6 ms |
5676 KB |
Output is correct |
51 |
Correct |
6 ms |
5676 KB |
Output is correct |
52 |
Correct |
9 ms |
5588 KB |
Output is correct |
53 |
Correct |
7 ms |
5588 KB |
Output is correct |
54 |
Correct |
7 ms |
5612 KB |
Output is correct |
55 |
Correct |
9 ms |
5540 KB |
Output is correct |
56 |
Correct |
6 ms |
5524 KB |
Output is correct |
57 |
Correct |
8 ms |
5332 KB |
Output is correct |
58 |
Correct |
9 ms |
5332 KB |
Output is correct |
59 |
Correct |
9 ms |
5332 KB |
Output is correct |
60 |
Correct |
9 ms |
5332 KB |
Output is correct |
61 |
Correct |
6 ms |
5544 KB |
Output is correct |
62 |
Correct |
7 ms |
5548 KB |
Output is correct |
63 |
Correct |
6 ms |
5460 KB |
Output is correct |
64 |
Correct |
7 ms |
5332 KB |
Output is correct |
65 |
Correct |
7 ms |
5416 KB |
Output is correct |
66 |
Correct |
7 ms |
5416 KB |
Output is correct |
67 |
Correct |
8 ms |
5420 KB |
Output is correct |
68 |
Correct |
6 ms |
5588 KB |
Output is correct |
69 |
Correct |
6 ms |
5460 KB |
Output is correct |
70 |
Correct |
6 ms |
5460 KB |
Output is correct |
71 |
Correct |
6 ms |
5460 KB |
Output is correct |
72 |
Correct |
8 ms |
5412 KB |
Output is correct |
73 |
Correct |
8 ms |
5288 KB |
Output is correct |
74 |
Correct |
6 ms |
5460 KB |
Output is correct |
75 |
Correct |
6 ms |
5516 KB |
Output is correct |
76 |
Correct |
6 ms |
5416 KB |
Output is correct |
77 |
Correct |
7 ms |
5548 KB |
Output is correct |
78 |
Correct |
6 ms |
5460 KB |
Output is correct |
79 |
Correct |
7 ms |
5460 KB |
Output is correct |
80 |
Correct |
6 ms |
5332 KB |
Output is correct |
81 |
Correct |
8 ms |
5460 KB |
Output is correct |
82 |
Correct |
6 ms |
5460 KB |
Output is correct |
83 |
Correct |
6 ms |
5460 KB |
Output is correct |
84 |
Correct |
6 ms |
5460 KB |
Output is correct |
85 |
Correct |
6 ms |
5344 KB |
Output is correct |
86 |
Correct |
6 ms |
5448 KB |
Output is correct |
87 |
Correct |
17 ms |
5940 KB |
Output is correct |
88 |
Correct |
47 ms |
7980 KB |
Output is correct |
89 |
Correct |
186 ms |
14760 KB |
Output is correct |
90 |
Correct |
178 ms |
14800 KB |
Output is correct |
91 |
Correct |
167 ms |
14784 KB |
Output is correct |
92 |
Correct |
110 ms |
21468 KB |
Output is correct |
93 |
Correct |
113 ms |
21708 KB |
Output is correct |
94 |
Correct |
112 ms |
21564 KB |
Output is correct |
95 |
Correct |
105 ms |
18296 KB |
Output is correct |
96 |
Correct |
113 ms |
18720 KB |
Output is correct |
97 |
Correct |
126 ms |
18896 KB |
Output is correct |
98 |
Correct |
112 ms |
18676 KB |
Output is correct |
99 |
Correct |
113 ms |
17960 KB |
Output is correct |
100 |
Correct |
204 ms |
14488 KB |
Output is correct |
101 |
Correct |
256 ms |
14868 KB |
Output is correct |
102 |
Correct |
231 ms |
14832 KB |
Output is correct |
103 |
Correct |
231 ms |
14812 KB |
Output is correct |
104 |
Correct |
115 ms |
17912 KB |
Output is correct |
105 |
Correct |
116 ms |
17892 KB |
Output is correct |
106 |
Correct |
121 ms |
17960 KB |
Output is correct |
107 |
Correct |
144 ms |
13596 KB |
Output is correct |
108 |
Correct |
155 ms |
13780 KB |
Output is correct |
109 |
Correct |
172 ms |
14144 KB |
Output is correct |
110 |
Correct |
106 ms |
20924 KB |
Output is correct |
111 |
Correct |
105 ms |
18332 KB |
Output is correct |
112 |
Correct |
102 ms |
17804 KB |
Output is correct |
113 |
Correct |
99 ms |
17044 KB |
Output is correct |
114 |
Correct |
189 ms |
14536 KB |
Output is correct |
115 |
Correct |
215 ms |
13976 KB |
Output is correct |
116 |
Correct |
103 ms |
17052 KB |
Output is correct |
117 |
Correct |
109 ms |
16080 KB |
Output is correct |
118 |
Correct |
109 ms |
15688 KB |
Output is correct |
119 |
Correct |
114 ms |
15332 KB |
Output is correct |
120 |
Correct |
104 ms |
15296 KB |
Output is correct |
121 |
Correct |
104 ms |
14972 KB |
Output is correct |
122 |
Correct |
103 ms |
14676 KB |
Output is correct |
123 |
Correct |
114 ms |
16060 KB |
Output is correct |
124 |
Correct |
132 ms |
15756 KB |
Output is correct |
125 |
Correct |
115 ms |
15292 KB |
Output is correct |
126 |
Correct |
110 ms |
15400 KB |
Output is correct |
127 |
Correct |
108 ms |
15016 KB |
Output is correct |
128 |
Correct |
113 ms |
14748 KB |
Output is correct |