#include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define vi vector<int> #define pi pair<int, int> #define mod 998244353 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; const int maxn = 1000005; int a[maxn], p[maxn]; int mx[maxn], mn[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; mx[n] = mn[n] = p[n]; for (int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], p[i]), mn[i] = min(mn[i + 1], p[i]); ll ans = 0; for (int l = 1; l <= n; l++) { int ct = (l + 2) / 2; int u = mn[n - ct + 1], v = mx[n - ct + 1]; int ls = u - 1, rs = n - v, nd = l - (v - u + 1); int lmx = min(ls, nd), lmn = max(0, nd - rs); if (lmx >= lmn) ans += lmx - lmn + 1; } cout << 2 * n + 1 << ' ' << ans << endl; return (0-0); }
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 | #include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define vi vector<int> #define pi pair<int, int> #define mod 998244353 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; const int maxn = 1000005; int a[maxn], p[maxn]; int mx[maxn], mn[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; mx[n] = mn[n] = p[n]; for (int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], p[i]), mn[i] = min(mn[i + 1], p[i]); ll ans = 0; for (int l = 1; l <= n; l++) { int ct = (l + 2) / 2; int u = mn[n - ct + 1], v = mx[n - ct + 1]; int ls = u - 1, rs = n - v, nd = l - (v - u + 1); int lmx = min(ls, nd), lmn = max(0, nd - rs); if (lmx >= lmn) ans += lmx - lmn + 1; } cout << 2 * n + 1 << ' ' << ans << endl; return (0-0); } |