#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 100100;
vector < int > adj[N];
int sz[N], head[N], up[N], depth[N], C[N];
deque < pair < int, int > > HLD[N];
vector < pair < int, int > > V, edges;
ll tree[N];
stack < pair < int, int > > st;
void upd(int pos, int val) {
for(; pos < N; tree[pos] += val, pos += -pos&pos);
}
ll sum(int x) {
ll ans = 0;
for(; x; ans += tree[x], x ^= -x&x);
return ans;
}
void dfs(int v) {
if(!head[v]) {
head[v] = v;
}
HLD[head[v]].push_back({C[v], 1});
int mx = 0;
for(auto &u : adj[v]) {
if(!mx || sz[u] > sz[mx]) {
mx = u;
}
}
if(mx) {
head[mx] = head[v];
}
for(auto &u : adj[v]) {
dfs(u);
}
}
ll cnt_inv() {
ll ans = 0;
for(auto &[x, cnt] : V) {
ans += cnt * sum(x - 1);
upd(x, cnt);
}
for(; !V.empty(); V.pop_back()) {
upd(V.back().first, -V.back().second);
}
return ans;
}
void cut(int i, int k, int b) {
for(int t = k; t;) {
if(HLD[i].front().second <= t) {
st.push(HLD[i].front());
t -= st.top().second;
HLD[i].pop_front();
} else {
st.push({HLD[i].front().first, t});
HLD[i].front().second -= t;
t = 0;
}
}
HLD[i].push_front({b, k});
for(; !st.empty(); st.pop()) {
V.push_back(st.top());
}
}
void solve(int x, int y) {
for(y = C[y]; x;) {
cut(head[x], depth[x] - depth[head[x]] + 1, y);
x = up[head[x]];
}
cout << cnt_inv() << '\n';
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
edges.resize(n);
for(int i = 1; i <= n; ++ i) {
cin >> edges[i - 1].first;
edges[i - 1].second = i;
sz[i] = 1;
}
sort(edges.begin(), edges.end());
for(int i = 0, j = 1; i < n; ++ i) {
j += (i && edges[i - 1].first < edges[i].first);
C[edges[i].second] = j;
}
edges.pop_back();
for(auto &[a, b] : edges) {
cin >> a >> b;
adj[a].push_back(b);
up[b] = a;
depth[b] = depth[a] + 1;
}
for(int i = n - 1; i --> 0;) {
sz[edges[i].first] += sz[edges[i].second];
}
dfs(1);
for(auto &[x, y] : edges) {
solve(x, y);
}
}
Compilation message
construction.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
70088 KB |
Output is correct |
2 |
Correct |
35 ms |
69992 KB |
Output is correct |
3 |
Correct |
35 ms |
70024 KB |
Output is correct |
4 |
Correct |
35 ms |
70068 KB |
Output is correct |
5 |
Correct |
36 ms |
70072 KB |
Output is correct |
6 |
Correct |
35 ms |
70092 KB |
Output is correct |
7 |
Correct |
35 ms |
70104 KB |
Output is correct |
8 |
Correct |
36 ms |
70096 KB |
Output is correct |
9 |
Correct |
35 ms |
70064 KB |
Output is correct |
10 |
Correct |
35 ms |
70184 KB |
Output is correct |
11 |
Correct |
35 ms |
70080 KB |
Output is correct |
12 |
Correct |
37 ms |
70084 KB |
Output is correct |
13 |
Correct |
40 ms |
70048 KB |
Output is correct |
14 |
Correct |
36 ms |
70044 KB |
Output is correct |
15 |
Correct |
36 ms |
70152 KB |
Output is correct |
16 |
Correct |
37 ms |
70092 KB |
Output is correct |
17 |
Correct |
37 ms |
70092 KB |
Output is correct |
18 |
Correct |
42 ms |
70136 KB |
Output is correct |
19 |
Correct |
38 ms |
70124 KB |
Output is correct |
20 |
Correct |
36 ms |
70104 KB |
Output is correct |
21 |
Correct |
38 ms |
70144 KB |
Output is correct |
22 |
Correct |
37 ms |
70100 KB |
Output is correct |
23 |
Correct |
37 ms |
70120 KB |
Output is correct |
24 |
Correct |
36 ms |
70132 KB |
Output is correct |
25 |
Correct |
37 ms |
70136 KB |
Output is correct |
26 |
Correct |
36 ms |
70092 KB |
Output is correct |
27 |
Correct |
36 ms |
70052 KB |
Output is correct |
28 |
Correct |
36 ms |
70068 KB |
Output is correct |
29 |
Correct |
36 ms |
70092 KB |
Output is correct |
30 |
Correct |
37 ms |
70120 KB |
Output is correct |
31 |
Correct |
38 ms |
70152 KB |
Output is correct |
32 |
Correct |
36 ms |
70148 KB |
Output is correct |
33 |
Correct |
37 ms |
70096 KB |
Output is correct |
34 |
Correct |
37 ms |
70088 KB |
Output is correct |
35 |
Correct |
38 ms |
70088 KB |
Output is correct |
36 |
Correct |
39 ms |
70140 KB |
Output is correct |
37 |
Correct |
38 ms |
70072 KB |
Output is correct |
38 |
Correct |
37 ms |
70060 KB |
Output is correct |
39 |
Correct |
37 ms |
70072 KB |
Output is correct |
40 |
Correct |
37 ms |
70092 KB |
Output is correct |
41 |
Correct |
38 ms |
70136 KB |
Output is correct |
42 |
Correct |
46 ms |
70100 KB |
Output is correct |
43 |
Correct |
41 ms |
70092 KB |
Output is correct |
44 |
Correct |
37 ms |
70008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
70088 KB |
Output is correct |
2 |
Correct |
35 ms |
69992 KB |
Output is correct |
3 |
Correct |
35 ms |
70024 KB |
Output is correct |
4 |
Correct |
35 ms |
70068 KB |
Output is correct |
5 |
Correct |
36 ms |
70072 KB |
Output is correct |
6 |
Correct |
35 ms |
70092 KB |
Output is correct |
7 |
Correct |
35 ms |
70104 KB |
Output is correct |
8 |
Correct |
36 ms |
70096 KB |
Output is correct |
9 |
Correct |
35 ms |
70064 KB |
Output is correct |
10 |
Correct |
35 ms |
70184 KB |
Output is correct |
11 |
Correct |
35 ms |
70080 KB |
Output is correct |
12 |
Correct |
37 ms |
70084 KB |
Output is correct |
13 |
Correct |
40 ms |
70048 KB |
Output is correct |
14 |
Correct |
36 ms |
70044 KB |
Output is correct |
15 |
Correct |
36 ms |
70152 KB |
Output is correct |
16 |
Correct |
37 ms |
70092 KB |
Output is correct |
17 |
Correct |
37 ms |
70092 KB |
Output is correct |
18 |
Correct |
42 ms |
70136 KB |
Output is correct |
19 |
Correct |
38 ms |
70124 KB |
Output is correct |
20 |
Correct |
36 ms |
70104 KB |
Output is correct |
21 |
Correct |
38 ms |
70144 KB |
Output is correct |
22 |
Correct |
37 ms |
70100 KB |
Output is correct |
23 |
Correct |
37 ms |
70120 KB |
Output is correct |
24 |
Correct |
36 ms |
70132 KB |
Output is correct |
25 |
Correct |
37 ms |
70136 KB |
Output is correct |
26 |
Correct |
36 ms |
70092 KB |
Output is correct |
27 |
Correct |
36 ms |
70052 KB |
Output is correct |
28 |
Correct |
36 ms |
70068 KB |
Output is correct |
29 |
Correct |
36 ms |
70092 KB |
Output is correct |
30 |
Correct |
37 ms |
70120 KB |
Output is correct |
31 |
Correct |
38 ms |
70152 KB |
Output is correct |
32 |
Correct |
36 ms |
70148 KB |
Output is correct |
33 |
Correct |
37 ms |
70096 KB |
Output is correct |
34 |
Correct |
37 ms |
70088 KB |
Output is correct |
35 |
Correct |
38 ms |
70088 KB |
Output is correct |
36 |
Correct |
39 ms |
70140 KB |
Output is correct |
37 |
Correct |
38 ms |
70072 KB |
Output is correct |
38 |
Correct |
37 ms |
70060 KB |
Output is correct |
39 |
Correct |
37 ms |
70072 KB |
Output is correct |
40 |
Correct |
37 ms |
70092 KB |
Output is correct |
41 |
Correct |
38 ms |
70136 KB |
Output is correct |
42 |
Correct |
46 ms |
70100 KB |
Output is correct |
43 |
Correct |
41 ms |
70092 KB |
Output is correct |
44 |
Correct |
37 ms |
70008 KB |
Output is correct |
45 |
Correct |
37 ms |
70116 KB |
Output is correct |
46 |
Correct |
39 ms |
70320 KB |
Output is correct |
47 |
Correct |
39 ms |
70328 KB |
Output is correct |
48 |
Correct |
40 ms |
70444 KB |
Output is correct |
49 |
Correct |
38 ms |
70680 KB |
Output is correct |
50 |
Correct |
38 ms |
70664 KB |
Output is correct |
51 |
Correct |
41 ms |
70740 KB |
Output is correct |
52 |
Correct |
38 ms |
70448 KB |
Output is correct |
53 |
Correct |
38 ms |
70552 KB |
Output is correct |
54 |
Correct |
38 ms |
70484 KB |
Output is correct |
55 |
Correct |
45 ms |
70484 KB |
Output is correct |
56 |
Correct |
37 ms |
70432 KB |
Output is correct |
57 |
Correct |
39 ms |
70380 KB |
Output is correct |
58 |
Correct |
45 ms |
70564 KB |
Output is correct |
59 |
Correct |
41 ms |
70388 KB |
Output is correct |
60 |
Correct |
40 ms |
70268 KB |
Output is correct |
61 |
Correct |
39 ms |
70516 KB |
Output is correct |
62 |
Correct |
38 ms |
70468 KB |
Output is correct |
63 |
Correct |
39 ms |
70404 KB |
Output is correct |
64 |
Correct |
39 ms |
70324 KB |
Output is correct |
65 |
Correct |
40 ms |
70256 KB |
Output is correct |
66 |
Correct |
39 ms |
70264 KB |
Output is correct |
67 |
Correct |
40 ms |
70368 KB |
Output is correct |
68 |
Correct |
39 ms |
70604 KB |
Output is correct |
69 |
Correct |
38 ms |
70460 KB |
Output is correct |
70 |
Correct |
38 ms |
70452 KB |
Output is correct |
71 |
Correct |
42 ms |
70476 KB |
Output is correct |
72 |
Correct |
40 ms |
70336 KB |
Output is correct |
73 |
Correct |
41 ms |
70356 KB |
Output is correct |
74 |
Correct |
41 ms |
70476 KB |
Output is correct |
75 |
Correct |
39 ms |
70424 KB |
Output is correct |
76 |
Correct |
44 ms |
70448 KB |
Output is correct |
77 |
Correct |
38 ms |
70316 KB |
Output is correct |
78 |
Correct |
39 ms |
70412 KB |
Output is correct |
79 |
Correct |
39 ms |
70344 KB |
Output is correct |
80 |
Correct |
39 ms |
70348 KB |
Output is correct |
81 |
Correct |
40 ms |
70320 KB |
Output is correct |
82 |
Correct |
39 ms |
70372 KB |
Output is correct |
83 |
Correct |
38 ms |
70396 KB |
Output is correct |
84 |
Correct |
38 ms |
70308 KB |
Output is correct |
85 |
Correct |
43 ms |
70276 KB |
Output is correct |
86 |
Correct |
38 ms |
70324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
70088 KB |
Output is correct |
2 |
Correct |
35 ms |
69992 KB |
Output is correct |
3 |
Correct |
35 ms |
70024 KB |
Output is correct |
4 |
Correct |
35 ms |
70068 KB |
Output is correct |
5 |
Correct |
36 ms |
70072 KB |
Output is correct |
6 |
Correct |
35 ms |
70092 KB |
Output is correct |
7 |
Correct |
35 ms |
70104 KB |
Output is correct |
8 |
Correct |
36 ms |
70096 KB |
Output is correct |
9 |
Correct |
35 ms |
70064 KB |
Output is correct |
10 |
Correct |
35 ms |
70184 KB |
Output is correct |
11 |
Correct |
35 ms |
70080 KB |
Output is correct |
12 |
Correct |
37 ms |
70084 KB |
Output is correct |
13 |
Correct |
40 ms |
70048 KB |
Output is correct |
14 |
Correct |
36 ms |
70044 KB |
Output is correct |
15 |
Correct |
36 ms |
70152 KB |
Output is correct |
16 |
Correct |
37 ms |
70092 KB |
Output is correct |
17 |
Correct |
37 ms |
70092 KB |
Output is correct |
18 |
Correct |
42 ms |
70136 KB |
Output is correct |
19 |
Correct |
38 ms |
70124 KB |
Output is correct |
20 |
Correct |
36 ms |
70104 KB |
Output is correct |
21 |
Correct |
38 ms |
70144 KB |
Output is correct |
22 |
Correct |
37 ms |
70100 KB |
Output is correct |
23 |
Correct |
37 ms |
70120 KB |
Output is correct |
24 |
Correct |
36 ms |
70132 KB |
Output is correct |
25 |
Correct |
37 ms |
70136 KB |
Output is correct |
26 |
Correct |
36 ms |
70092 KB |
Output is correct |
27 |
Correct |
36 ms |
70052 KB |
Output is correct |
28 |
Correct |
36 ms |
70068 KB |
Output is correct |
29 |
Correct |
36 ms |
70092 KB |
Output is correct |
30 |
Correct |
37 ms |
70120 KB |
Output is correct |
31 |
Correct |
38 ms |
70152 KB |
Output is correct |
32 |
Correct |
36 ms |
70148 KB |
Output is correct |
33 |
Correct |
37 ms |
70096 KB |
Output is correct |
34 |
Correct |
37 ms |
70088 KB |
Output is correct |
35 |
Correct |
38 ms |
70088 KB |
Output is correct |
36 |
Correct |
39 ms |
70140 KB |
Output is correct |
37 |
Correct |
38 ms |
70072 KB |
Output is correct |
38 |
Correct |
37 ms |
70060 KB |
Output is correct |
39 |
Correct |
37 ms |
70072 KB |
Output is correct |
40 |
Correct |
37 ms |
70092 KB |
Output is correct |
41 |
Correct |
38 ms |
70136 KB |
Output is correct |
42 |
Correct |
46 ms |
70100 KB |
Output is correct |
43 |
Correct |
41 ms |
70092 KB |
Output is correct |
44 |
Correct |
37 ms |
70008 KB |
Output is correct |
45 |
Correct |
37 ms |
70116 KB |
Output is correct |
46 |
Correct |
39 ms |
70320 KB |
Output is correct |
47 |
Correct |
39 ms |
70328 KB |
Output is correct |
48 |
Correct |
40 ms |
70444 KB |
Output is correct |
49 |
Correct |
38 ms |
70680 KB |
Output is correct |
50 |
Correct |
38 ms |
70664 KB |
Output is correct |
51 |
Correct |
41 ms |
70740 KB |
Output is correct |
52 |
Correct |
38 ms |
70448 KB |
Output is correct |
53 |
Correct |
38 ms |
70552 KB |
Output is correct |
54 |
Correct |
38 ms |
70484 KB |
Output is correct |
55 |
Correct |
45 ms |
70484 KB |
Output is correct |
56 |
Correct |
37 ms |
70432 KB |
Output is correct |
57 |
Correct |
39 ms |
70380 KB |
Output is correct |
58 |
Correct |
45 ms |
70564 KB |
Output is correct |
59 |
Correct |
41 ms |
70388 KB |
Output is correct |
60 |
Correct |
40 ms |
70268 KB |
Output is correct |
61 |
Correct |
39 ms |
70516 KB |
Output is correct |
62 |
Correct |
38 ms |
70468 KB |
Output is correct |
63 |
Correct |
39 ms |
70404 KB |
Output is correct |
64 |
Correct |
39 ms |
70324 KB |
Output is correct |
65 |
Correct |
40 ms |
70256 KB |
Output is correct |
66 |
Correct |
39 ms |
70264 KB |
Output is correct |
67 |
Correct |
40 ms |
70368 KB |
Output is correct |
68 |
Correct |
39 ms |
70604 KB |
Output is correct |
69 |
Correct |
38 ms |
70460 KB |
Output is correct |
70 |
Correct |
38 ms |
70452 KB |
Output is correct |
71 |
Correct |
42 ms |
70476 KB |
Output is correct |
72 |
Correct |
40 ms |
70336 KB |
Output is correct |
73 |
Correct |
41 ms |
70356 KB |
Output is correct |
74 |
Correct |
41 ms |
70476 KB |
Output is correct |
75 |
Correct |
39 ms |
70424 KB |
Output is correct |
76 |
Correct |
44 ms |
70448 KB |
Output is correct |
77 |
Correct |
38 ms |
70316 KB |
Output is correct |
78 |
Correct |
39 ms |
70412 KB |
Output is correct |
79 |
Correct |
39 ms |
70344 KB |
Output is correct |
80 |
Correct |
39 ms |
70348 KB |
Output is correct |
81 |
Correct |
40 ms |
70320 KB |
Output is correct |
82 |
Correct |
39 ms |
70372 KB |
Output is correct |
83 |
Correct |
38 ms |
70396 KB |
Output is correct |
84 |
Correct |
38 ms |
70308 KB |
Output is correct |
85 |
Correct |
43 ms |
70276 KB |
Output is correct |
86 |
Correct |
38 ms |
70324 KB |
Output is correct |
87 |
Correct |
45 ms |
70752 KB |
Output is correct |
88 |
Correct |
64 ms |
72268 KB |
Output is correct |
89 |
Correct |
156 ms |
77536 KB |
Output is correct |
90 |
Correct |
172 ms |
77552 KB |
Output is correct |
91 |
Correct |
147 ms |
77556 KB |
Output is correct |
92 |
Correct |
93 ms |
86092 KB |
Output is correct |
93 |
Correct |
96 ms |
86020 KB |
Output is correct |
94 |
Correct |
96 ms |
86016 KB |
Output is correct |
95 |
Correct |
102 ms |
81180 KB |
Output is correct |
96 |
Correct |
100 ms |
81624 KB |
Output is correct |
97 |
Correct |
121 ms |
81604 KB |
Output is correct |
98 |
Correct |
105 ms |
81648 KB |
Output is correct |
99 |
Correct |
99 ms |
81076 KB |
Output is correct |
100 |
Correct |
176 ms |
77632 KB |
Output is correct |
101 |
Correct |
208 ms |
77936 KB |
Output is correct |
102 |
Correct |
221 ms |
78060 KB |
Output is correct |
103 |
Correct |
211 ms |
78016 KB |
Output is correct |
104 |
Correct |
112 ms |
81080 KB |
Output is correct |
105 |
Correct |
104 ms |
81052 KB |
Output is correct |
106 |
Correct |
104 ms |
81360 KB |
Output is correct |
107 |
Correct |
139 ms |
75964 KB |
Output is correct |
108 |
Correct |
139 ms |
76048 KB |
Output is correct |
109 |
Correct |
154 ms |
76608 KB |
Output is correct |
110 |
Correct |
97 ms |
84656 KB |
Output is correct |
111 |
Correct |
94 ms |
81224 KB |
Output is correct |
112 |
Correct |
96 ms |
80216 KB |
Output is correct |
113 |
Correct |
93 ms |
79688 KB |
Output is correct |
114 |
Correct |
195 ms |
77684 KB |
Output is correct |
115 |
Correct |
191 ms |
76700 KB |
Output is correct |
116 |
Correct |
98 ms |
79680 KB |
Output is correct |
117 |
Correct |
98 ms |
79212 KB |
Output is correct |
118 |
Correct |
113 ms |
78712 KB |
Output is correct |
119 |
Correct |
95 ms |
78348 KB |
Output is correct |
120 |
Correct |
98 ms |
78004 KB |
Output is correct |
121 |
Correct |
102 ms |
77628 KB |
Output is correct |
122 |
Correct |
98 ms |
77332 KB |
Output is correct |
123 |
Correct |
119 ms |
79220 KB |
Output is correct |
124 |
Correct |
101 ms |
78716 KB |
Output is correct |
125 |
Correct |
100 ms |
78400 KB |
Output is correct |
126 |
Correct |
101 ms |
78236 KB |
Output is correct |
127 |
Correct |
104 ms |
77696 KB |
Output is correct |
128 |
Correct |
105 ms |
77360 KB |
Output is correct |