#include <cstdio> #include <algorithm> const int max_n = 300000; int n; int down[max_n]; int up[max_n]; int prefMaxUp[max_n]; int prefMaxDown[max_n]; int suffMinUp[max_n]; int suffMinDown[max_n]; long long answer[max_n]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &down[i]); up[down[i]] = i; answer[i] = 0; } prefMaxDown[1] = down[1]; prefMaxUp[1] = up[1]; suffMinDown[n] = down[n]; suffMinUp[n] = up[n]; for (int i = 2; i <= n; ++i) { prefMaxUp[i] = std::max(prefMaxUp[i - 1], up[i]); prefMaxDown[i] = std::max(prefMaxDown[i - 1], down[i]); } for (int i = n - 1; i >= 1; --i) { suffMinUp[i] = std::min(suffMinUp[i + 1], up[i]); suffMinDown[i] = std::min(suffMinDown[i + 1], down[i]); } for (int i = 1; i <= n; ++i) { // printf("COMPUTE %d\n", i); answer[i] = 0; int a = i; int b = down[i]; int dist = 1; for (int j = a + 1; j <= prefMaxUp[b]; ++j) { if (down[j] < b) answer[i] += dist; } for (int j = b + 1; j <= prefMaxDown[a]; ++j) { if (up[j] < a) answer[i] += dist; } // printf("A: %d %d %d\n", a, b, answer[i]); while (true) { int nb = prefMaxDown[a]; int na = prefMaxUp[b]; // printf("(%d %d) (%d %d)\n", a, na, b, nb); ++dist; for (int j = a + 1; j <= na; ++j) { if (down[j] > b) answer[i] += dist; } for (int j = b + 1; j <= nb; ++j) { if (up[j] > na) answer[i] += dist; } if (na == a && nb == b) break; b = nb; a = na; } a = i; b = down[i]; dist = 1; while (true) { int nb = suffMinDown[a]; int na = suffMinUp[b]; // printf("R (%d %d) (%d %d)\n", a, na, b, nb); ++dist; for (int j = a - 1; j >= na; --j) { if (down[j] < b) answer[i] += dist; } for (int j = b - 1; j >= nb; --j) { if (up[j] < na) answer[i] += dist; } if (na == a && nb == b) break; b = nb; a = na; } } for (int i = 1; i <= n; ++i) { printf("%lld ", answer[i]); } printf("\n"); 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <cstdio> #include <algorithm> const int max_n = 300000; int n; int down[max_n]; int up[max_n]; int prefMaxUp[max_n]; int prefMaxDown[max_n]; int suffMinUp[max_n]; int suffMinDown[max_n]; long long answer[max_n]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &down[i]); up[down[i]] = i; answer[i] = 0; } prefMaxDown[1] = down[1]; prefMaxUp[1] = up[1]; suffMinDown[n] = down[n]; suffMinUp[n] = up[n]; for (int i = 2; i <= n; ++i) { prefMaxUp[i] = std::max(prefMaxUp[i - 1], up[i]); prefMaxDown[i] = std::max(prefMaxDown[i - 1], down[i]); } for (int i = n - 1; i >= 1; --i) { suffMinUp[i] = std::min(suffMinUp[i + 1], up[i]); suffMinDown[i] = std::min(suffMinDown[i + 1], down[i]); } for (int i = 1; i <= n; ++i) { // printf("COMPUTE %d\n", i); answer[i] = 0; int a = i; int b = down[i]; int dist = 1; for (int j = a + 1; j <= prefMaxUp[b]; ++j) { if (down[j] < b) answer[i] += dist; } for (int j = b + 1; j <= prefMaxDown[a]; ++j) { if (up[j] < a) answer[i] += dist; } // printf("A: %d %d %d\n", a, b, answer[i]); while (true) { int nb = prefMaxDown[a]; int na = prefMaxUp[b]; // printf("(%d %d) (%d %d)\n", a, na, b, nb); ++dist; for (int j = a + 1; j <= na; ++j) { if (down[j] > b) answer[i] += dist; } for (int j = b + 1; j <= nb; ++j) { if (up[j] > na) answer[i] += dist; } if (na == a && nb == b) break; b = nb; a = na; } a = i; b = down[i]; dist = 1; while (true) { int nb = suffMinDown[a]; int na = suffMinUp[b]; // printf("R (%d %d) (%d %d)\n", a, na, b, nb); ++dist; for (int j = a - 1; j >= na; --j) { if (down[j] < b) answer[i] += dist; } for (int j = b - 1; j >= nb; --j) { if (up[j] < na) answer[i] += dist; } if (na == a && nb == b) break; b = nb; a = na; } } for (int i = 1; i <= n; ++i) { printf("%lld ", answer[i]); } printf("\n"); return 0; } |