#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (ll)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 2e5 + 10;
const ll INF = 1e18;
int N, timer = 1; ll ans; vi child[2 * MX]; Tree<int> T[2 * MX];
void DFS1(int X) {
int Y; cin >> Y;
if (Y == 0) {
child[X].pb(++timer); DFS1(timer);
child[X].pb(++timer); DFS1(timer);
}
else T[X].insert(Y);
}
void DFS2(int X) {
EACH(Y, child[X]) {
DFS2(Y); if (sz(T[X]) < sz(T[Y])) T[X].swap(T[Y]);
ll num = 0; EACH(C, T[Y]) num += T[X].order_of_key(C);
ans += min(num, sz(T[X]) * sz(T[Y]) - num);
EACH(C, T[Y]) T[X].insert(C); T[Y].clear();
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N; DFS1(1); DFS2(1); cout << ans;
}
Compilation message
rot.cpp: In function 'void DFS2(int)':
rot.cpp:46:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
46 | #define EACH(a, x) for (auto& a : x)
| ^~~
rot.cpp:71:9: note: in expansion of macro 'EACH'
71 | EACH(C, T[Y]) T[X].insert(C); T[Y].clear();
| ^~~~
rot.cpp:71:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
71 | EACH(C, T[Y]) T[X].insert(C); T[Y].clear();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
41004 KB |
Output is correct |
2 |
Correct |
43 ms |
41024 KB |
Output is correct |
3 |
Correct |
35 ms |
40892 KB |
Output is correct |
4 |
Correct |
36 ms |
40916 KB |
Output is correct |
5 |
Correct |
35 ms |
40880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
40900 KB |
Output is correct |
2 |
Correct |
35 ms |
40892 KB |
Output is correct |
3 |
Correct |
37 ms |
41036 KB |
Output is correct |
4 |
Correct |
39 ms |
40924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
41000 KB |
Output is correct |
2 |
Correct |
35 ms |
41008 KB |
Output is correct |
3 |
Correct |
36 ms |
41028 KB |
Output is correct |
4 |
Correct |
34 ms |
41156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
41540 KB |
Output is correct |
2 |
Correct |
41 ms |
41292 KB |
Output is correct |
3 |
Correct |
39 ms |
41668 KB |
Output is correct |
4 |
Correct |
38 ms |
41672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
42972 KB |
Output is correct |
2 |
Correct |
54 ms |
42180 KB |
Output is correct |
3 |
Correct |
101 ms |
44260 KB |
Output is correct |
4 |
Correct |
50 ms |
43012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
46412 KB |
Output is correct |
2 |
Correct |
100 ms |
47836 KB |
Output is correct |
3 |
Correct |
106 ms |
49540 KB |
Output is correct |
4 |
Correct |
106 ms |
49864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
55524 KB |
Output is correct |
2 |
Correct |
143 ms |
54288 KB |
Output is correct |
3 |
Correct |
160 ms |
50124 KB |
Output is correct |
4 |
Correct |
136 ms |
49436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
200 ms |
51780 KB |
Output is correct |
2 |
Correct |
188 ms |
53044 KB |
Output is correct |
3 |
Correct |
169 ms |
59328 KB |
Output is correct |
4 |
Correct |
181 ms |
59500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
395 ms |
62404 KB |
Output is correct |
2 |
Correct |
296 ms |
58212 KB |
Output is correct |
3 |
Correct |
315 ms |
59768 KB |
Output is correct |
4 |
Correct |
342 ms |
60468 KB |
Output is correct |
5 |
Correct |
522 ms |
59100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
59840 KB |
Output is correct |
2 |
Runtime error |
277 ms |
65540 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
390 ms |
64120 KB |
Output is correct |
2 |
Correct |
315 ms |
60724 KB |
Output is correct |
3 |
Correct |
469 ms |
60484 KB |
Output is correct |
4 |
Correct |
413 ms |
64304 KB |
Output is correct |
5 |
Runtime error |
115 ms |
65540 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |