#include <bits/stdc++.h> using namespace std; const int B = 1 << 20; int n, t[1 << 20], pos[1 << 20]; int tree[2 * B + 5]; int l = 1e9, r; int32_t main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; pos[t[i]] = i; } int res = 2 * n + 1; long long cnt = 0; for (int x = 1; x <= n; x++) { if (x % 2 == 0) { int med_1 = (res - x) / 2; int med_2 = med_1 + 1; l = min({l, pos[med_1]}); r = max({r, pos[med_1]}); } else { int med = (res - x) / 2; l = min({l, pos[med]}); r = max({r, pos[med]}); } int zostalo = x - (r - l + 1); if (zostalo < 0) continue; if (zostalo == 0) { cnt++; continue; } int lewo = l - 1; int prawo = n - r; int mx = min(zostalo, lewo); int mi = max(0, zostalo - prawo); cnt += max(0, mx - mi + 1); } cout << res << " " << cnt << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <bits/stdc++.h> using namespace std; const int B = 1 << 20; int n, t[1 << 20], pos[1 << 20]; int tree[2 * B + 5]; int l = 1e9, r; int32_t main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; pos[t[i]] = i; } int res = 2 * n + 1; long long cnt = 0; for (int x = 1; x <= n; x++) { if (x % 2 == 0) { int med_1 = (res - x) / 2; int med_2 = med_1 + 1; l = min({l, pos[med_1]}); r = max({r, pos[med_1]}); } else { int med = (res - x) / 2; l = min({l, pos[med]}); r = max({r, pos[med]}); } int zostalo = x - (r - l + 1); if (zostalo < 0) continue; if (zostalo == 0) { cnt++; continue; } int lewo = l - 1; int prawo = n - r; int mx = min(zostalo, lewo); int mi = max(0, zostalo - prawo); cnt += max(0, mx - mi + 1); } cout << res << " " << cnt << "\n"; } |