#include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int base = (1 << 18); const int maxK = 10; pii t2[base * 2]; int lz[base * 2]; void put(int v, int k) { t2[v].st += k; lz[v] += k; } void push(int v) { lz[v * 2] += lz[v]; lz[v * 2 + 1] += lz[v]; t2[v * 2].st += lz[v]; t2[v * 2 + 1].st += lz[v]; lz[v] = 0; } void pull(int v) { if (t2[v * 2].st > t2[v * 2 + 1].st) t2[v] = t2[v * 2]; else if (t2[v * 2 + 1].st > t2[v * 2].st) t2[v] = t2[v * 2 + 1]; else t2[v] = make_pair(t2[v * 2].st, t2[v * 2].nd + t2[v * 2 + 1].nd); } void upd2(int l, int r, int k, int cl = 0, int cr = base - 1, int v = 1) { if (l > cr || r < cl) return; if (cl >= l && cr <= r) { put(v, k); return; } push(v); int m = (cl + cr) / 2; upd2(l, r, k, cl, m, v * 2); upd2(l, r, k, m + 1, cr, v * 2 + 1); pull(v); } int t[base * 2][maxK + 1]; int d[base * 2]; void mrg(int v) { if (v >= base) { rep(i, maxK + 1) t[v][i] = 0; if (d[v] <= maxK) t[v][d[v]] = 1; return; } rep(i, maxK + 1) t[v][i] = 0; rep(i, maxK + 1) if (i + d[v] <= maxK) t[v][i + d[v]] = t[v * 2][i] + t[v * 2 + 1][i]; } void upd(int l, int r, int k) { l += base; r += base; d[l] += k; mrg(l); if (l != r) { d[r] += k; mrg(r); } while (l / 2 != r / 2) { if (l % 2 == 0) { d[l + 1] += k; mrg(l + 1); } if (r % 2 == 1) { d[r - 1] += k; mrg(r - 1); } l /= 2; r /= 2; mrg(l); mrg(r); } while (l / 2 != 0) { l /= 2; mrg(l); } } void prep() { fwd(i, base, base * 2) { t[i][0] = 1; t2[i] = make_pair(0, 1); } for (int i = base - 1; i >= 1; -- i) { t[i][0] = t[i * 2][0] + t[i * 2 + 1][0]; t2[i] = make_pair(0, t2[i * 2].nd + t2[i * 2 + 1].nd); } } int32_t main() { fastio; prep(); int n, k; cin >> n >> k; vi a(n), b(n); rep(i, n) { cin >> a[i]; -- a[i]; } rep(i, n) { cin >> b[i]; -- b[i]; } vector<array<int, 4> > ivals; vector<array<int, 4> > ivals2; auto make_ivals = [&] (int x, int y, int l, int r) { if (r < l) swap(l, r); if (y < x) swap(x, y); if ((x >= l && x <= r) || (y >= l && y <= r)) return; if (y < l) { ivals.push_back({y + 1, l, r, 2 * n - 1}); return; } if (x > r) { ivals.push_back({0, l, r, x - 1}); return; } ivals.push_back({x + 1, l, r, y - 1}); }; auto make_ivals2 = [&] (int x, int y, int l, int r) { if (y < x) swap(x, y); if (r < l) swap(l, r); if (l < x) { swap(x, l); swap(y, r); } if (r <= y) { ivals2.push_back({0, l, r, 2 * n - 1}); return; } ivals2.push_back({0, l, r, 2 * n - 1}); ivals2.push_back({0, x, y, r - 1}); }; rep(i, n) { make_ivals(a[i], b[i], a[(i + 1) % n], b[(i + 1) % n]); make_ivals(a[i], b[(i + 1) % n], a[(i + 1) % n], a[(i + 1) % n]); make_ivals(a[(i + 1) % n], b[i], b[(i + 1) % n], b[(i + 1) % n]); make_ivals2(a[i], a[(i + 1) % n], b[i], b[(i + 1) % n]); } vector<vector<array<int, 3> > > upds(2 * n); vector<vector<array<int, 3> > > upds2(2 * n); for (auto [x, y, l, r] : ivals) { upds[x].push_back({l, r, 1}); if (y + 1 < 2 * n) upds[y + 1].push_back({l, r, -1}); } for (auto [x, y, l, r] : ivals2) { upds2[x].push_back({l, r, 1}); if (y + 1 < 2 * n) upds2[y + 1].push_back({l, r, -1}); } vector<ll> ans(k + 1, 0); rep(i, 2 * n) { for (auto [l, r, delta] : upds[i]) upd(l, r, delta); rep(j, k + 1) ans[j] += t[1][j]; for (auto [l, r, delta] : upds2[i]) upd2(l, r, delta); if (t2[1].st == n) ans[1] += t2[1].nd; } fwd(i, 1, k + 1) cout << ans[i] << ' '; cout << '\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 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | #include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int base = (1 << 18); const int maxK = 10; pii t2[base * 2]; int lz[base * 2]; void put(int v, int k) { t2[v].st += k; lz[v] += k; } void push(int v) { lz[v * 2] += lz[v]; lz[v * 2 + 1] += lz[v]; t2[v * 2].st += lz[v]; t2[v * 2 + 1].st += lz[v]; lz[v] = 0; } void pull(int v) { if (t2[v * 2].st > t2[v * 2 + 1].st) t2[v] = t2[v * 2]; else if (t2[v * 2 + 1].st > t2[v * 2].st) t2[v] = t2[v * 2 + 1]; else t2[v] = make_pair(t2[v * 2].st, t2[v * 2].nd + t2[v * 2 + 1].nd); } void upd2(int l, int r, int k, int cl = 0, int cr = base - 1, int v = 1) { if (l > cr || r < cl) return; if (cl >= l && cr <= r) { put(v, k); return; } push(v); int m = (cl + cr) / 2; upd2(l, r, k, cl, m, v * 2); upd2(l, r, k, m + 1, cr, v * 2 + 1); pull(v); } int t[base * 2][maxK + 1]; int d[base * 2]; void mrg(int v) { if (v >= base) { rep(i, maxK + 1) t[v][i] = 0; if (d[v] <= maxK) t[v][d[v]] = 1; return; } rep(i, maxK + 1) t[v][i] = 0; rep(i, maxK + 1) if (i + d[v] <= maxK) t[v][i + d[v]] = t[v * 2][i] + t[v * 2 + 1][i]; } void upd(int l, int r, int k) { l += base; r += base; d[l] += k; mrg(l); if (l != r) { d[r] += k; mrg(r); } while (l / 2 != r / 2) { if (l % 2 == 0) { d[l + 1] += k; mrg(l + 1); } if (r % 2 == 1) { d[r - 1] += k; mrg(r - 1); } l /= 2; r /= 2; mrg(l); mrg(r); } while (l / 2 != 0) { l /= 2; mrg(l); } } void prep() { fwd(i, base, base * 2) { t[i][0] = 1; t2[i] = make_pair(0, 1); } for (int i = base - 1; i >= 1; -- i) { t[i][0] = t[i * 2][0] + t[i * 2 + 1][0]; t2[i] = make_pair(0, t2[i * 2].nd + t2[i * 2 + 1].nd); } } int32_t main() { fastio; prep(); int n, k; cin >> n >> k; vi a(n), b(n); rep(i, n) { cin >> a[i]; -- a[i]; } rep(i, n) { cin >> b[i]; -- b[i]; } vector<array<int, 4> > ivals; vector<array<int, 4> > ivals2; auto make_ivals = [&] (int x, int y, int l, int r) { if (r < l) swap(l, r); if (y < x) swap(x, y); if ((x >= l && x <= r) || (y >= l && y <= r)) return; if (y < l) { ivals.push_back({y + 1, l, r, 2 * n - 1}); return; } if (x > r) { ivals.push_back({0, l, r, x - 1}); return; } ivals.push_back({x + 1, l, r, y - 1}); }; auto make_ivals2 = [&] (int x, int y, int l, int r) { if (y < x) swap(x, y); if (r < l) swap(l, r); if (l < x) { swap(x, l); swap(y, r); } if (r <= y) { ivals2.push_back({0, l, r, 2 * n - 1}); return; } ivals2.push_back({0, l, r, 2 * n - 1}); ivals2.push_back({0, x, y, r - 1}); }; rep(i, n) { make_ivals(a[i], b[i], a[(i + 1) % n], b[(i + 1) % n]); make_ivals(a[i], b[(i + 1) % n], a[(i + 1) % n], a[(i + 1) % n]); make_ivals(a[(i + 1) % n], b[i], b[(i + 1) % n], b[(i + 1) % n]); make_ivals2(a[i], a[(i + 1) % n], b[i], b[(i + 1) % n]); } vector<vector<array<int, 3> > > upds(2 * n); vector<vector<array<int, 3> > > upds2(2 * n); for (auto [x, y, l, r] : ivals) { upds[x].push_back({l, r, 1}); if (y + 1 < 2 * n) upds[y + 1].push_back({l, r, -1}); } for (auto [x, y, l, r] : ivals2) { upds2[x].push_back({l, r, 1}); if (y + 1 < 2 * n) upds2[y + 1].push_back({l, r, -1}); } vector<ll> ans(k + 1, 0); rep(i, 2 * n) { for (auto [l, r, delta] : upds[i]) upd(l, r, delta); rep(j, k + 1) ans[j] += t[1][j]; for (auto [l, r, delta] : upds2[i]) upd2(l, r, delta); if (t2[1].st == n) ans[1] += t2[1].nd; } fwd(i, 1, k + 1) cout << ans[i] << ' '; cout << '\n'; return 0; } |