Submission #577119

#TimeUsernameProblemLanguageResultExecution timeMemory
577119eecsExercise Deadlines (CCO20_day1problem2)C++17
25 / 25
91 ms6192 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, d[maxn], p[maxn]; #define mid ((l + r) >> 1) #define ls (k << 1) #define rs (k << 1 | 1) int mx[maxn << 2]; void build(int k, int l, int r) { if (l == r) { mx[k] = d[l]; return; } build(ls, l, mid), build(rs, mid + 1, r); mx[k] = max(mx[ls], mx[rs]); } void upd(int k, int l, int r, int p) { if (l == r) { mx[k] = 0; return; } mid >= p ? upd(ls, l, mid, p) : upd(rs, mid + 1, r, p); mx[k] = max(mx[ls], mx[rs]); } int find(int k, int l, int r, int v) { if (l == r) return l; return mx[rs] >= v ? find(rs, mid + 1, r, v) : find(ls, l, mid, v); } namespace BIT { int c[maxn]; void add(int p, int v) { for (; p <= n; p += p & -p) c[p] += v; } int sum(int p) { int s = 0; for (; p; p -= p & -p) s += c[p]; return s; } } // namespace BIT int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> d[i]; } build(1, 1, n); for (int i = n; i; i--) { if (mx[1] < i) puts("-1"), exit(0); int j = find(1, 1, n, i); p[j] = i, upd(1, 1, n, j); } long long ans = 0; for (int i = n; i; i--) { ans += BIT::sum(p[i]), BIT::add(p[i], 1); } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...