classBIT { public: voidupd(int x, int v){ for (; x <= n; x += x & -x) c[x] += v; } LL qry(int x){ LL ret = 0; for (; x; x -= x & -x) ret += c[x]; return ret; } private: LL c[N]; } t ;
boolcheck(){ for (int i = 1; i <= n; ++i) if (a[i] != i) returnfalse; returntrue; }
voidsolve(){ int cnt = 0; for (int i = 1; i <= n; ++i) if (a[i] < a[i + 1]) cut[++cnt] = i; for (int i = 1; i <= cnt; ++i) if (cut[i] - cut[i - 1] != 1) { reverse(a + cut[i - 1] + 1, a + cut[i] + 1); ++ans; } for (int i = 1; i <= n; ++i) { ans += i - 1 - t.qry(a[i]); t.upd(a[i], 1); } cout << ans << endl; }