#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();
}
child[X].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 |
35 ms |
41004 KB |
Output is correct |
2 |
Correct |
34 ms |
40948 KB |
Output is correct |
3 |
Correct |
35 ms |
40948 KB |
Output is correct |
4 |
Correct |
35 ms |
40920 KB |
Output is correct |
5 |
Correct |
35 ms |
40936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
40908 KB |
Output is correct |
2 |
Correct |
35 ms |
41000 KB |
Output is correct |
3 |
Correct |
37 ms |
40920 KB |
Output is correct |
4 |
Correct |
36 ms |
40960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
41028 KB |
Output is correct |
2 |
Correct |
36 ms |
41036 KB |
Output is correct |
3 |
Correct |
37 ms |
41148 KB |
Output is correct |
4 |
Correct |
42 ms |
41044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
41540 KB |
Output is correct |
2 |
Correct |
41 ms |
41280 KB |
Output is correct |
3 |
Correct |
39 ms |
41736 KB |
Output is correct |
4 |
Correct |
39 ms |
41752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
43180 KB |
Output is correct |
2 |
Correct |
55 ms |
42124 KB |
Output is correct |
3 |
Correct |
101 ms |
44236 KB |
Output is correct |
4 |
Correct |
49 ms |
43076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
46352 KB |
Output is correct |
2 |
Correct |
107 ms |
48324 KB |
Output is correct |
3 |
Correct |
110 ms |
50120 KB |
Output is correct |
4 |
Correct |
104 ms |
50236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
56696 KB |
Output is correct |
2 |
Correct |
131 ms |
54852 KB |
Output is correct |
3 |
Correct |
168 ms |
50616 KB |
Output is correct |
4 |
Correct |
136 ms |
49496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
195 ms |
51780 KB |
Output is correct |
2 |
Correct |
192 ms |
53436 KB |
Output is correct |
3 |
Correct |
170 ms |
60380 KB |
Output is correct |
4 |
Correct |
173 ms |
60356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
63076 KB |
Output is correct |
2 |
Correct |
284 ms |
58440 KB |
Output is correct |
3 |
Correct |
296 ms |
60276 KB |
Output is correct |
4 |
Correct |
329 ms |
60816 KB |
Output is correct |
5 |
Correct |
501 ms |
59176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
337 ms |
60160 KB |
Output is correct |
2 |
Runtime error |
110 ms |
65540 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
64288 KB |
Output is correct |
2 |
Correct |
325 ms |
61124 KB |
Output is correct |
3 |
Correct |
468 ms |
60500 KB |
Output is correct |
4 |
Correct |
407 ms |
64320 KB |
Output is correct |
5 |
Runtime error |
112 ms |
65540 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |