#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using vpi = vector<pair<int, int>>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } int DEF_INTER; struct iinter { bool bad = false; int minl, maxl, minr, maxr; iinter(int x1, int x2) { minl = 0; maxl = min(x1, x2); minr = max(x1, x2); maxr = 2*DEF_INTER-1; } void ban(int x) { if (x >= maxl && x <= minr) bad = true; else if (x > minr) mini(maxr, x-1); else if (x < maxl) maxi(minl, x+1); else assert(false); } }; void __dbg(iinter x) { if (x.bad) cout << 'e'; else cout << '[' << x.minl << ", " << x.maxl << "] x [" << x.minr << ", " << x.maxr << "]"; } const int MXK = 10; using ans_t = array<ll, MXK+1>; template<int BLOCK_SZ> ans_t sol_n32(int n, vector<vpi> &add, vector<vpi> &rem) { static_assert((BLOCK_SZ & (BLOCK_SZ-1)) == 0); const int BLOCK_CNT = 2*n / BLOCK_SZ + 1; ans_t ans{}, curr{}, tmp{}; curr[0] = 2*n; vi plus_at(2*n), block_mod(BLOCK_CNT); tmp[0] = BLOCK_SZ; vector<ans_t> for_bl(BLOCK_CNT, tmp); for_bl.back()[0] -= BLOCK_CNT * BLOCK_SZ - 2*n; debug(for_bl); auto register_interval = [&](int id){ const int md = block_mod[id]; for (int i = 0; i + md <= MXK; ++i) curr[i + md] += for_bl[id][i]; // debug(id, md, for_bl[id]); }; auto unregister_interval = [&](int id){ const int md = block_mod[id]; for (int i = 0; i + md <= MXK; ++i) curr[i + md] -= for_bl[id][i]; // debug(id, md, for_bl[id]); }; auto partial_add = [&](int from, int to) { // debug(from, to); const int id = from / BLOCK_SZ; assert(id == to / BLOCK_SZ); unregister_interval(id); for (int i = from; i <= to; ++i) { if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] -= 1; ++plus_at[i]; if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] += 1; } register_interval(id); }; auto partial_rem = [&](int from, int to) { const int id = from / BLOCK_SZ; assert(id == to / BLOCK_SZ); unregister_interval(id); for (int i = from; i <= to; ++i) { if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] -= 1; --plus_at[i]; if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] += 1; } register_interval(id); }; rep(l, 2*n) { for (auto [ri, rj] : add[l]) { const int idl = ri / BLOCK_SZ, idr = rj / BLOCK_SZ; // debug(ri, rj); if (idl != idr) { partial_add(ri, ri | (BLOCK_SZ-1)); partial_add(rj &~ (BLOCK_SZ-1), rj); } else { partial_add(ri, rj); } for (int id = idl + 1; id < idr; ++id) { unregister_interval(id); ++block_mod[id]; register_interval(id); } } for (auto [ri, rj] : rem[l]) { const int idl = ri / BLOCK_SZ, idr = rj / BLOCK_SZ; // debug(ri, rj); if (idl != idr) { partial_rem(ri, ri | (BLOCK_SZ-1)); partial_rem(rj &~ (BLOCK_SZ-1), rj); } else { partial_rem(ri, rj); } for (int id = idl + 1; id < idr; ++id) { unregister_interval(id); --block_mod[id]; register_interval(id); } } for (int sm = 0; sm <= MXK; ++sm) // TODO sm = 1 ans[sm] += curr[sm]; // debug(l); // debug(curr); // debug(ans); // debug(plus_at); // debug(block_mod); // debug(for_bl); } return ans; } ll count_fully_connected(int n, const vvi &plo) { vpi where(2*n); rep(i, n) { where[ plo[0][i] ] = {0, i}; where[ plo[1][i] ] = {1, i}; } debug(where); ll ans = 0; int minr = 0, max_ex_l = 2*n; rep(i, n) { const int a = plo[0][i], b = plo[1][i], c = plo[0][(i+1) % n], d = plo[1][(i+1) % n]; const int minimal = min(max(a, c), max(b, d)); maxi(minr, minimal); } for (int l = 0; l < max_ex_l; ++l) { debug(ans, l, minr, max_ex_l); ans += 2*n - minr; maxi(minr, l+1); auto [row, col] = where[l]; maxi(minr, plo[row^1][ col ]); maxi(minr, plo[row^1][ (col+1)%n ]); maxi(minr, plo[row^1][ (col-1+n)%n ]); mini(max_ex_l, 1+plo[row^1][ col ]); mini(max_ex_l, 1+plo[row^1][ (col+1)%n ]); mini(max_ex_l, 1+plo[row^1][ (col-1+n)%n ]); } debug(ans); return ans; } void solve() { int n, k; cin >> n >> k; DEF_INTER = n; vvi plo(2, vi(n)); for (vi &v : plo) for (int &i : v) { cin >> i; --i; } vector<vpi> add(2*n), rem(2*n); auto registeri = [&](const iinter& ii) { if (ii.bad) return; add[ii.minl].emplace_back(ii.minr, ii.maxr); if (ii.maxl+1 < 2*n) rem[ii.maxl+1].emplace_back(ii.minr, ii.maxr); }; rep(i, n) { const int a = plo[0][i], b = plo[1][i], c = plo[0][(i+1) % n], d = plo[1][(i+1) % n]; { iinter inter(a, b); inter.ban(c); inter.ban(d); registeri(inter); } { iinter inter(a, a); inter.ban(b); inter.ban(c); registeri(inter); } { iinter inter(b, b); inter.ban(a); inter.ban(d); registeri(inter); } } debug(add); debug(rem); ans_t ans = sol_n32<256>(n, add, rem); debug(ans); ans[1] += count_fully_connected(n, plo); rep(i, k) cout << ans[i+1] << ' '; cout << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using vpi = vector<pair<int, int>>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } int DEF_INTER; struct iinter { bool bad = false; int minl, maxl, minr, maxr; iinter(int x1, int x2) { minl = 0; maxl = min(x1, x2); minr = max(x1, x2); maxr = 2*DEF_INTER-1; } void ban(int x) { if (x >= maxl && x <= minr) bad = true; else if (x > minr) mini(maxr, x-1); else if (x < maxl) maxi(minl, x+1); else assert(false); } }; void __dbg(iinter x) { if (x.bad) cout << 'e'; else cout << '[' << x.minl << ", " << x.maxl << "] x [" << x.minr << ", " << x.maxr << "]"; } const int MXK = 10; using ans_t = array<ll, MXK+1>; template<int BLOCK_SZ> ans_t sol_n32(int n, vector<vpi> &add, vector<vpi> &rem) { static_assert((BLOCK_SZ & (BLOCK_SZ-1)) == 0); const int BLOCK_CNT = 2*n / BLOCK_SZ + 1; ans_t ans{}, curr{}, tmp{}; curr[0] = 2*n; vi plus_at(2*n), block_mod(BLOCK_CNT); tmp[0] = BLOCK_SZ; vector<ans_t> for_bl(BLOCK_CNT, tmp); for_bl.back()[0] -= BLOCK_CNT * BLOCK_SZ - 2*n; debug(for_bl); auto register_interval = [&](int id){ const int md = block_mod[id]; for (int i = 0; i + md <= MXK; ++i) curr[i + md] += for_bl[id][i]; // debug(id, md, for_bl[id]); }; auto unregister_interval = [&](int id){ const int md = block_mod[id]; for (int i = 0; i + md <= MXK; ++i) curr[i + md] -= for_bl[id][i]; // debug(id, md, for_bl[id]); }; auto partial_add = [&](int from, int to) { // debug(from, to); const int id = from / BLOCK_SZ; assert(id == to / BLOCK_SZ); unregister_interval(id); for (int i = from; i <= to; ++i) { if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] -= 1; ++plus_at[i]; if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] += 1; } register_interval(id); }; auto partial_rem = [&](int from, int to) { const int id = from / BLOCK_SZ; assert(id == to / BLOCK_SZ); unregister_interval(id); for (int i = from; i <= to; ++i) { if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] -= 1; --plus_at[i]; if (plus_at[i] <= MXK) for_bl[id][plus_at[i]] += 1; } register_interval(id); }; rep(l, 2*n) { for (auto [ri, rj] : add[l]) { const int idl = ri / BLOCK_SZ, idr = rj / BLOCK_SZ; // debug(ri, rj); if (idl != idr) { partial_add(ri, ri | (BLOCK_SZ-1)); partial_add(rj &~ (BLOCK_SZ-1), rj); } else { partial_add(ri, rj); } for (int id = idl + 1; id < idr; ++id) { unregister_interval(id); ++block_mod[id]; register_interval(id); } } for (auto [ri, rj] : rem[l]) { const int idl = ri / BLOCK_SZ, idr = rj / BLOCK_SZ; // debug(ri, rj); if (idl != idr) { partial_rem(ri, ri | (BLOCK_SZ-1)); partial_rem(rj &~ (BLOCK_SZ-1), rj); } else { partial_rem(ri, rj); } for (int id = idl + 1; id < idr; ++id) { unregister_interval(id); --block_mod[id]; register_interval(id); } } for (int sm = 0; sm <= MXK; ++sm) // TODO sm = 1 ans[sm] += curr[sm]; // debug(l); // debug(curr); // debug(ans); // debug(plus_at); // debug(block_mod); // debug(for_bl); } return ans; } ll count_fully_connected(int n, const vvi &plo) { vpi where(2*n); rep(i, n) { where[ plo[0][i] ] = {0, i}; where[ plo[1][i] ] = {1, i}; } debug(where); ll ans = 0; int minr = 0, max_ex_l = 2*n; rep(i, n) { const int a = plo[0][i], b = plo[1][i], c = plo[0][(i+1) % n], d = plo[1][(i+1) % n]; const int minimal = min(max(a, c), max(b, d)); maxi(minr, minimal); } for (int l = 0; l < max_ex_l; ++l) { debug(ans, l, minr, max_ex_l); ans += 2*n - minr; maxi(minr, l+1); auto [row, col] = where[l]; maxi(minr, plo[row^1][ col ]); maxi(minr, plo[row^1][ (col+1)%n ]); maxi(minr, plo[row^1][ (col-1+n)%n ]); mini(max_ex_l, 1+plo[row^1][ col ]); mini(max_ex_l, 1+plo[row^1][ (col+1)%n ]); mini(max_ex_l, 1+plo[row^1][ (col-1+n)%n ]); } debug(ans); return ans; } void solve() { int n, k; cin >> n >> k; DEF_INTER = n; vvi plo(2, vi(n)); for (vi &v : plo) for (int &i : v) { cin >> i; --i; } vector<vpi> add(2*n), rem(2*n); auto registeri = [&](const iinter& ii) { if (ii.bad) return; add[ii.minl].emplace_back(ii.minr, ii.maxr); if (ii.maxl+1 < 2*n) rem[ii.maxl+1].emplace_back(ii.minr, ii.maxr); }; rep(i, n) { const int a = plo[0][i], b = plo[1][i], c = plo[0][(i+1) % n], d = plo[1][(i+1) % n]; { iinter inter(a, b); inter.ban(c); inter.ban(d); registeri(inter); } { iinter inter(a, a); inter.ban(b); inter.ban(c); registeri(inter); } { iinter inter(b, b); inter.ban(a); inter.ban(d); registeri(inter); } } debug(add); debug(rem); ans_t ans = sol_n32<256>(n, add, rem); debug(ans); ans[1] += count_fully_connected(n, plo); rep(i, k) cout << ans[i+1] << ' '; cout << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |