#include <cstdio> typedef long long int lli; const int NN = 1000001; int V[NN]; int G[NN]; inline lli as(lli n) { return n * (n + 1) / 2; } inline lli addage(int loose, int left, int right) { return as(loose + 1) - as(loose - left) - as(loose - right); } inline int _min(int a, int b) { return (a < b) ? a : b; } inline int _max(int a, int b) { return (a > b) ? a : b; } lli compute(int N, int *V, int *G) { lli opts = 0; for (int i = 0; i < N; ++i) { G[N - V[i]] = i; } int left = N, right = 0; int far_l = G[0], far_r = G[0]; int loose, last_i = -1; for (int i = 0; i < N; ++i) { left = _min(left, G[i]); right = _max(right, G[i]); int loose_cap = N - right + left; loose = _min(i - right + i + left, loose_cap); if (loose >= 0) { while (i < N - 1 && left < G[i + 1] && G[i + 1] < right) { ++i; loose = _min(loose + 2, loose_cap); } far_l = _max(0, left - loose); far_r = _min(N - 1, right + loose); opts += addage(loose, left - far_l, far_r - right); if (last_i != -1) { int counted_loose = _min(loose_cap, last_i - right + last_i + left); if (counted_loose >= 0) { int cfl = _min(left, counted_loose); int cfr = _min(N - 1 - right, counted_loose); opts -= addage(counted_loose, cfl, cfr); } } last_i = i; } } return opts; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &V[i]); } lli result = compute(N, V, G); printf("%lld %lld\n", (lli)2 * N + 1, result); 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <cstdio> typedef long long int lli; const int NN = 1000001; int V[NN]; int G[NN]; inline lli as(lli n) { return n * (n + 1) / 2; } inline lli addage(int loose, int left, int right) { return as(loose + 1) - as(loose - left) - as(loose - right); } inline int _min(int a, int b) { return (a < b) ? a : b; } inline int _max(int a, int b) { return (a > b) ? a : b; } lli compute(int N, int *V, int *G) { lli opts = 0; for (int i = 0; i < N; ++i) { G[N - V[i]] = i; } int left = N, right = 0; int far_l = G[0], far_r = G[0]; int loose, last_i = -1; for (int i = 0; i < N; ++i) { left = _min(left, G[i]); right = _max(right, G[i]); int loose_cap = N - right + left; loose = _min(i - right + i + left, loose_cap); if (loose >= 0) { while (i < N - 1 && left < G[i + 1] && G[i + 1] < right) { ++i; loose = _min(loose + 2, loose_cap); } far_l = _max(0, left - loose); far_r = _min(N - 1, right + loose); opts += addage(loose, left - far_l, far_r - right); if (last_i != -1) { int counted_loose = _min(loose_cap, last_i - right + last_i + left); if (counted_loose >= 0) { int cfl = _min(left, counted_loose); int cfr = _min(N - 1 - right, counted_loose); opts -= addage(counted_loose, cfl, cfr); } } last_i = i; } } return opts; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &V[i]); } lli result = compute(N, V, G); printf("%lld %lld\n", (lli)2 * N + 1, result); return 0; } |