#include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) // int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int where[n]; FOR(i, n) { int x; cin >> x; where[x - 1] = i; } int mi = INT_MAX; int ma = -INT_MAX; long long result = 0; OF(x, 0, n) { auto i = where[x]; REMIN(mi, i); REMAX(ma, i); FO(len, 2 * (n - x) - 2, 2 * (n - x)) { if (len < ma - mi + 1) continue; int a0 = max(0, ma - len + 1); int a1 = min(n - len, mi); // cerr << "len " << len << " result:" << a1 - a0 + 1 << endl; result += max(0, a1 - a0 + 1); } } cout << 2 * n + 1 << " " << result << endl; return 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) // int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int where[n]; FOR(i, n) { int x; cin >> x; where[x - 1] = i; } int mi = INT_MAX; int ma = -INT_MAX; long long result = 0; OF(x, 0, n) { auto i = where[x]; REMIN(mi, i); REMAX(ma, i); FO(len, 2 * (n - x) - 2, 2 * (n - x)) { if (len < ma - mi + 1) continue; int a0 = max(0, ma - len + 1); int a1 = min(n - len, mi); // cerr << "len " << len << " result:" << a1 - a0 + 1 << endl; result += max(0, a1 - a0 + 1); } } cout << 2 * n + 1 << " " << result << endl; return 0; } |