#include <bits/stdc++.h> using namespace std; const int MAXN = 201000; int A[MAXN]; int B[MAXN]; long long int out[MAXN]; int n, k; vector<pair<int, int>> rect_push[MAXN]; vector<pair<int, int>> rect_pop[MAXN]; const int NBUCKS = 200; const int BUCKSIZE = 1010; int load_pt[MAXN]; int load_buck[NBUCKS]; int out_pt[NBUCKS][MAXN]; void ds_add(int l, int r, int v) { for (int i = l; i <= r; ) { if (i % BUCKSIZE == 0 && i + BUCKSIZE - 1 <= r) { load_buck[i / BUCKSIZE] += v; i += BUCKSIZE; } else { int bid = i / BUCKSIZE; out_pt[bid][load_pt[i]]--; load_pt[i] += v; out_pt[bid][load_pt[i]]++; i++; } } } int ds_get(int i) { int ret = 0; for (int j = 0; j < NBUCKS; j++) { int ri = i - load_buck[j]; if (ri >= 0 && ri < MAXN) ret += out_pt[j][ri]; } return ret; } // a1, a2 must be in the interval and d1, d2 cannot be void add_border(int a1, int a2, int d1, int d2) { if (a1 > a2) swap(a1, a2); if (a1 <= d1 && d1 <= a2) { return; // impossible } if (a1 <= d2 && d2 <= a2) { return; // impossible } int lb = 1; int ub = 2 * n; if (d1 < a1) lb = max(lb, d1 + 1); if (d2 < a1) lb = max(lb, d2 + 1); if (d1 > a2) ub = min(ub, d1 - 1); if (d2 > a2) ub = min(ub, d2 - 1); rect_push[lb].push_back({ a2, ub }); rect_pop[a1 + 1].push_back({ a2, ub }); } int main() { for (int i = 0; i < NBUCKS; i++) { out_pt[i][0] = BUCKSIZE; } scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", A + i); } for (int i = 1; i <= n; i++) { scanf("%d", B + i); } A[0] = A[n]; B[0] = B[n]; for (int i = 0; i < n; i++) { add_border(A[i], A[i], A[i + 1], B[i]); add_border(B[i], B[i], B[i + 1], A[i]); add_border(A[i], B[i], A[i + 1], B[i + 1]); } for (int l = 1; l <= 2 * n; l++) { for (auto p : rect_push[l]) { ds_add(p.first, p.second, +1); } for (auto p : rect_pop[l]) { ds_add(p.first, p.second, -1); } for (int i = 1; i <= k; i++) { out[i] += ds_get(i); } out[1] += ds_get(0); } out[1] -= (long long int)(2 * n) * (2 * n - 1) / 2; out[1] -= (long long int)(2 * n) * (NBUCKS * BUCKSIZE - 2 * n); for (int i = 1; i <= k; i++) { printf("%lld ", out[i]); } puts(""); }
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <bits/stdc++.h> using namespace std; const int MAXN = 201000; int A[MAXN]; int B[MAXN]; long long int out[MAXN]; int n, k; vector<pair<int, int>> rect_push[MAXN]; vector<pair<int, int>> rect_pop[MAXN]; const int NBUCKS = 200; const int BUCKSIZE = 1010; int load_pt[MAXN]; int load_buck[NBUCKS]; int out_pt[NBUCKS][MAXN]; void ds_add(int l, int r, int v) { for (int i = l; i <= r; ) { if (i % BUCKSIZE == 0 && i + BUCKSIZE - 1 <= r) { load_buck[i / BUCKSIZE] += v; i += BUCKSIZE; } else { int bid = i / BUCKSIZE; out_pt[bid][load_pt[i]]--; load_pt[i] += v; out_pt[bid][load_pt[i]]++; i++; } } } int ds_get(int i) { int ret = 0; for (int j = 0; j < NBUCKS; j++) { int ri = i - load_buck[j]; if (ri >= 0 && ri < MAXN) ret += out_pt[j][ri]; } return ret; } // a1, a2 must be in the interval and d1, d2 cannot be void add_border(int a1, int a2, int d1, int d2) { if (a1 > a2) swap(a1, a2); if (a1 <= d1 && d1 <= a2) { return; // impossible } if (a1 <= d2 && d2 <= a2) { return; // impossible } int lb = 1; int ub = 2 * n; if (d1 < a1) lb = max(lb, d1 + 1); if (d2 < a1) lb = max(lb, d2 + 1); if (d1 > a2) ub = min(ub, d1 - 1); if (d2 > a2) ub = min(ub, d2 - 1); rect_push[lb].push_back({ a2, ub }); rect_pop[a1 + 1].push_back({ a2, ub }); } int main() { for (int i = 0; i < NBUCKS; i++) { out_pt[i][0] = BUCKSIZE; } scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", A + i); } for (int i = 1; i <= n; i++) { scanf("%d", B + i); } A[0] = A[n]; B[0] = B[n]; for (int i = 0; i < n; i++) { add_border(A[i], A[i], A[i + 1], B[i]); add_border(B[i], B[i], B[i + 1], A[i]); add_border(A[i], B[i], A[i + 1], B[i + 1]); } for (int l = 1; l <= 2 * n; l++) { for (auto p : rect_push[l]) { ds_add(p.first, p.second, +1); } for (auto p : rect_pop[l]) { ds_add(p.first, p.second, -1); } for (int i = 1; i <= k; i++) { out[i] += ds_get(i); } out[1] += ds_get(0); } out[1] -= (long long int)(2 * n) * (2 * n - 1) / 2; out[1] -= (long long int)(2 * n) * (NBUCKS * BUCKSIZE - 2 * n); for (int i = 1; i <= k; i++) { printf("%lld ", out[i]); } puts(""); } |