#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } int k; struct Node { int lazy = 0; int offset = 0; // minimum int cnt[11] = {0,0,0,0,0,0,0,0,0,0}; void add(int diff) { offset += diff; lazy += diff; } void merge(const Node& left, const Node& right) { assert(left.cnt[0] >= 1); assert(right.cnt[0] >= 1); assert(lazy == 0); offset = min(left.offset, right.offset); for (int i = 0; i <= 10; i++) { cnt[i] = 0; int j = i + offset - left.offset; if (j >= 0) cnt[i] += left.cnt[j]; j = i + offset - right.offset; if (j >= 0) cnt[i] += right.cnt[j]; } assert(cnt[0] >= 1); } }; vector<Node> tree; int BASE = 1; void rec(int id, int L, int R, int queryL, int queryR, int diff) { if (R < queryL || queryR < L) { return; } if (queryL <= L && R <= queryR) { tree[id].add(diff); return; } for (int child : {2 * id, 2 * id + 1}) { tree[child].add(tree[id].lazy); } tree[id].lazy = 0; int lastLeft = (L + R) / 2; rec(2 * id, L, lastLeft, queryL, queryR, diff); rec(2 * id + 1, lastLeft + 1, R, queryL, queryR, diff); tree[id].merge(tree[2*id], tree[2*id+1]); } int main() { int n; scanf("%d%d", &n, &k); vector<vector<int>> grid(2, vector<int>(n)); for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { scanf("%d", &grid[i][j]); } } vector<vector<vector<int>>> events(2*n+2); auto add_rectangle = [&](int a, int b, int c, int d, int diff) { events[a].push_back({c,d,diff}); events[b+1].push_back({c,d,-diff}); }; // vector<vector<int>> rectangles; auto allow = [&](int a, int b, int c, int d) { // want a & b // don't want c & d // debug dd; if (a > b) { swap(a, b); } // dd << ">" imie(a) imie(b) imie(c) imie(d); pair<int,int> start = {1, a}; pair<int,int> end = {b, 2 * n}; for (int x : {c, d}) { if (a <= x && x <= b) { return; } if (x <= a) { start.first = max(start.first, x + 1); } if (x >= b) { end.second = min(end.second, x - 1); } } // dd << imie(start) imie(end); if (start.first <= start.second && end.first <= end.second) { // dd << "YES"; add_rectangle(start.first, start.second, end.first, end.second, +1); } }; for (int c = 0; c < n; c++) { int d = (c + 1) % n; allow(grid[0][d], grid[0][d], grid[0][c], grid[1][d]); allow(grid[1][d], grid[1][d], grid[1][c], grid[0][d]); allow(grid[0][d], grid[1][d], grid[0][c], grid[1][c]); } vector<long long> answer(k+1); while (BASE <= 2 * n) { BASE *= 2; } tree.resize(2 * BASE); for (int i = 0; i <= 2 * n; i++) { tree[BASE+i].offset = 0; // OK tree[BASE+i].cnt[0] = 1; } for (int i = BASE + 2 * n + 1; i < 2 * BASE; i++) { tree[i].offset = k + 1; tree[i].cnt[0] = 1; } for (int i = BASE - 1; i >= 1; i--) { tree[i].merge(tree[2*i], tree[2*i+1]); } for (int s = 1; s <= 2 * n; s++) { debug() << imie(s); events[s].push_back({s-1, s-1, k+1}); // forbidding s-1 for (vector<int> event : events[s]) { int L = event[0]; int R = event[1]; int diff = event[2]; // -1 or +1 debug() << "RANGE_INCREASE" imie(L) imie(R) imie(diff); rec(1, 0, BASE - 1, L, R, diff); } debug() << imie(tree[1].offset) imie(range(tree[1].cnt + 0, tree[1].cnt + k + 1)); for (int i = 0; i <= k; i++) { int x = tree[1].offset + i; if (x <= k) { answer[x] += tree[1].cnt[i]; } } } answer[1] += answer[0]; for (int i = 1; i <= k; i++) { printf("%lld ", answer[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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } int k; struct Node { int lazy = 0; int offset = 0; // minimum int cnt[11] = {0,0,0,0,0,0,0,0,0,0}; void add(int diff) { offset += diff; lazy += diff; } void merge(const Node& left, const Node& right) { assert(left.cnt[0] >= 1); assert(right.cnt[0] >= 1); assert(lazy == 0); offset = min(left.offset, right.offset); for (int i = 0; i <= 10; i++) { cnt[i] = 0; int j = i + offset - left.offset; if (j >= 0) cnt[i] += left.cnt[j]; j = i + offset - right.offset; if (j >= 0) cnt[i] += right.cnt[j]; } assert(cnt[0] >= 1); } }; vector<Node> tree; int BASE = 1; void rec(int id, int L, int R, int queryL, int queryR, int diff) { if (R < queryL || queryR < L) { return; } if (queryL <= L && R <= queryR) { tree[id].add(diff); return; } for (int child : {2 * id, 2 * id + 1}) { tree[child].add(tree[id].lazy); } tree[id].lazy = 0; int lastLeft = (L + R) / 2; rec(2 * id, L, lastLeft, queryL, queryR, diff); rec(2 * id + 1, lastLeft + 1, R, queryL, queryR, diff); tree[id].merge(tree[2*id], tree[2*id+1]); } int main() { int n; scanf("%d%d", &n, &k); vector<vector<int>> grid(2, vector<int>(n)); for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { scanf("%d", &grid[i][j]); } } vector<vector<vector<int>>> events(2*n+2); auto add_rectangle = [&](int a, int b, int c, int d, int diff) { events[a].push_back({c,d,diff}); events[b+1].push_back({c,d,-diff}); }; // vector<vector<int>> rectangles; auto allow = [&](int a, int b, int c, int d) { // want a & b // don't want c & d // debug dd; if (a > b) { swap(a, b); } // dd << ">" imie(a) imie(b) imie(c) imie(d); pair<int,int> start = {1, a}; pair<int,int> end = {b, 2 * n}; for (int x : {c, d}) { if (a <= x && x <= b) { return; } if (x <= a) { start.first = max(start.first, x + 1); } if (x >= b) { end.second = min(end.second, x - 1); } } // dd << imie(start) imie(end); if (start.first <= start.second && end.first <= end.second) { // dd << "YES"; add_rectangle(start.first, start.second, end.first, end.second, +1); } }; for (int c = 0; c < n; c++) { int d = (c + 1) % n; allow(grid[0][d], grid[0][d], grid[0][c], grid[1][d]); allow(grid[1][d], grid[1][d], grid[1][c], grid[0][d]); allow(grid[0][d], grid[1][d], grid[0][c], grid[1][c]); } vector<long long> answer(k+1); while (BASE <= 2 * n) { BASE *= 2; } tree.resize(2 * BASE); for (int i = 0; i <= 2 * n; i++) { tree[BASE+i].offset = 0; // OK tree[BASE+i].cnt[0] = 1; } for (int i = BASE + 2 * n + 1; i < 2 * BASE; i++) { tree[i].offset = k + 1; tree[i].cnt[0] = 1; } for (int i = BASE - 1; i >= 1; i--) { tree[i].merge(tree[2*i], tree[2*i+1]); } for (int s = 1; s <= 2 * n; s++) { debug() << imie(s); events[s].push_back({s-1, s-1, k+1}); // forbidding s-1 for (vector<int> event : events[s]) { int L = event[0]; int R = event[1]; int diff = event[2]; // -1 or +1 debug() << "RANGE_INCREASE" imie(L) imie(R) imie(diff); rec(1, 0, BASE - 1, L, R, diff); } debug() << imie(tree[1].offset) imie(range(tree[1].cnt + 0, tree[1].cnt + k + 1)); for (int i = 0; i <= k; i++) { int x = tree[1].offset + i; if (x <= k) { answer[x] += tree[1].cnt[i]; } } } answer[1] += answer[0]; for (int i = 1; i <= k; i++) { printf("%lld ", answer[i]); } puts(""); } |