This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |