#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[MX]; Tree<int> T[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 |
19 ms |
20556 KB |
Output is correct |
2 |
Correct |
19 ms |
20556 KB |
Output is correct |
3 |
Correct |
20 ms |
20596 KB |
Output is correct |
4 |
Correct |
20 ms |
20672 KB |
Output is correct |
5 |
Correct |
19 ms |
20660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
20672 KB |
Output is correct |
2 |
Correct |
19 ms |
20684 KB |
Output is correct |
3 |
Correct |
21 ms |
20568 KB |
Output is correct |
4 |
Correct |
20 ms |
20676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
20632 KB |
Output is correct |
2 |
Correct |
20 ms |
20764 KB |
Output is correct |
3 |
Correct |
21 ms |
20704 KB |
Output is correct |
4 |
Correct |
20 ms |
20812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
21240 KB |
Output is correct |
2 |
Correct |
25 ms |
21060 KB |
Output is correct |
3 |
Correct |
23 ms |
21444 KB |
Output is correct |
4 |
Correct |
23 ms |
21328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
22732 KB |
Output is correct |
2 |
Correct |
39 ms |
21828 KB |
Output is correct |
3 |
Correct |
91 ms |
23800 KB |
Output is correct |
4 |
Correct |
34 ms |
22768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
25924 KB |
Output is correct |
2 |
Correct |
90 ms |
27876 KB |
Output is correct |
3 |
Correct |
94 ms |
29792 KB |
Output is correct |
4 |
Correct |
90 ms |
29956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
36328 KB |
Output is correct |
2 |
Correct |
119 ms |
34432 KB |
Output is correct |
3 |
Correct |
155 ms |
30184 KB |
Output is correct |
4 |
Correct |
120 ms |
29192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
58820 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
82 ms |
58576 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
60576 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
83 ms |
60076 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |