#include <iostream> #include <vector> #include <cassert> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (int i = 0; (i) < (n); ++(i)) #define fori(i, a, b) for (int i = (a); (i) <= (b); ++(i)) #define all(x) (x).begin(), (x).end() typedef int64_t ll; int read() { int n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } struct segtree { int n, k; vec<vec<pair<int, int>>> st; // {num, occ} vec<int> lazy; segtree(int n, int k) : n(n), k(k), st(4*n, vec<pair<int, int>>(k, {1e9, 0})), lazy(4*n) { _build(1, 0, n); } void _build(int v, int l, int r) { // cout << l << " " << r << endl; if (l+1 == r) { st[v][0] = {0, 1}; return; } int m = (l+r)/2; _build(2*v, l, m); _build(2*v+1, m, r); _merge(v); } void push(int v) { if (!lazy[v]) return; lazy[2*v] += lazy[v]; lazy[2*v+1] += lazy[v]; foru (i, k) st[2*v][i].x += lazy[v]; foru (i, k) st[2*v+1][i].x += lazy[v]; lazy[v] = 0; } void _merge(int v) { // df("merge %d with:\n"); int a = 0, b = 0; foru (i, k) { const auto& nl = st[2*v][a], nr = st[2*v+1][b]; // df("choose %d or %d [%d %d]\n", nl.x, nr.x, a, b); if (nl.x == nr.x) { st[v][i] = {nl.x, nl.y + nr.y}; ++a, ++b; } else if (nl.x < nr.x) { st[v][i] = nl; ++a; } else { st[v][i] = nr; ++b; } } // df("\n"); } void _update(int v, int l, int r, int ql, int qr, int val) { // [ql, qr) if (qr <= ql) return; // df("[%d %d] [%d %d] val%d\n", l, r, ql, qr, val); if (ql == l && qr == r) { foru (i, k) st[v][i].x += val; lazy[v] += val; return; } push(v); int m = (l+r)/2; _update(2*v, l, m, ql, min(qr, m), val); _update(2*v+1, m, r, max(ql, m), qr, val); _merge(v); } void update(int l, int r, int val) { _update(1, 0, n, l, r, val); } }; vec<pair<int, bool>> pos; vec<vec<int>> col; int n, k; int _c; bool o(int x, bool y) { return col[(x+n)%n][y] < _c; } int get(int x, bool y) { return col[(x+n)%n][y]; } int calc(int c) { _c = c; if (c == 1e9) return 0; auto [x, y] = pos[c]; int d = 1; if (o(x, !y)) { d -= 1; // opposite if (o(x+1, y)) d -= !o(x+1, !y); // sub only if not connected to opposite if (o(x-1, y)) d -= !o(x-1, !y); } else { d -= o(x-1, y) + o(x+1, y); } return d; } int vmax(vec<int> vs) { int x = -int(1e9); foru (i, vs.size()) { /* df("x = max(%d, %d)\n", x, vs[i]); */ x = max(x, vs[i]); } return x; } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif cin >> n >> k; segtree st(2*n, k+1); // sru d(n); pos.resize(2*n); col.resize(n, vec<int>(2, 1e9)); // padded for easier calc() impl vec<int> ds(2*n); foru (i, n) { cin >> col[i][0]; --col[i][0]; pos[col[i][0]] = {i, 0}; } foru (i, n) { cin >> col[i][1]; --col[i][1]; pos[col[i][1]] = {i, 1}; } foru (i, 2*n) { st.update(i, 2*n, ds[i] = calc(i)); } vec<ll> ans(k+1); int tconn = 0; int aaa; foru (x, n) { // cout << get(x, 0) << " " << get(x, 1) << " " << get(x+1, 0) << " " << get(x-1, 1) << endl; // cout << " " << vmax({get(x, 0), get(x, 1), get(x+1, 0), get(x-1, 1)}) << endl; // cout << " " << vmax({get(x, 0), get(x, 1), get(x-1, 0), get(x+1, 1)}) << endl; // cout << " " << vmax({get(x-1, 0), get(x, 0), get(x+1, 0)}) << endl; // cout << " " << vmax({get(x-1, 1), get(x, 1), get(x+1, 1)}) << endl; tconn = max(tconn, -vmax({ -vmax({get(x, 0), get(x, 1), get(x+1, 0), get(x-1, 1)}), -vmax({get(x, 0), get(x, 1), get(x-1, 0), get(x+1, 1)}), -vmax({get(x-1, 0), get(x, 0), get(x+1, 0)}), -vmax({get(x-1, 1), get(x, 1), get(x+1, 1)}) })); // cout << aaa << endl; } foru (i, 2*n) { if (tconn != 1e9) st.update(tconn, 2*n, 1e8); for (int i = 0; i <= k && st.st[1][i].x <= k; ++i) { ans[st.st[1][i].x] += ll(st.st[1][i].y); } int _s = 0; df("tconn: %d\n", tconn); // foru (i, 2*n) cout << ds[i] << " "; // cout << "\n"; // foru (i, 2*n) { _s += ds[i]; cout << _s << " "; } // cout << "\n"; foru (i, k+1) { df("{%d, %d} ", st.st[1][i].x, st.st[1][i].y); } df("\n"); if (tconn != 1e9) { st.update(tconn, 2*n, -1e8); ans[1] += ll(2*n - tconn); } st.update(i, 2*n, -ds[i]); ds[i] = 0; auto [x, y] = pos[i]; col[x][y] = 1e9; tconn = vmax({tconn, get(x-1, !y), get(x, !y), get(x+1, !y)}); for (int dx = -1; dx <= 1; ++dx) { foru (yy, 2) { if (dx == 0 && yy == y) continue; int c = col[(x+dx+n)%n][yy]; if (c == 1e9) continue; int nd = calc(c); st.update(c, 2*n, nd-ds[c]); ds[c] = nd; } } } fori (i, 1, k) cout << ans[i] << " "; // foru (i, k) cout << ans[i+1] << " "; // cout << "\n"; // assert(ans[0] == 0); 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 | #include <iostream> #include <vector> #include <cassert> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (int i = 0; (i) < (n); ++(i)) #define fori(i, a, b) for (int i = (a); (i) <= (b); ++(i)) #define all(x) (x).begin(), (x).end() typedef int64_t ll; int read() { int n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } struct segtree { int n, k; vec<vec<pair<int, int>>> st; // {num, occ} vec<int> lazy; segtree(int n, int k) : n(n), k(k), st(4*n, vec<pair<int, int>>(k, {1e9, 0})), lazy(4*n) { _build(1, 0, n); } void _build(int v, int l, int r) { // cout << l << " " << r << endl; if (l+1 == r) { st[v][0] = {0, 1}; return; } int m = (l+r)/2; _build(2*v, l, m); _build(2*v+1, m, r); _merge(v); } void push(int v) { if (!lazy[v]) return; lazy[2*v] += lazy[v]; lazy[2*v+1] += lazy[v]; foru (i, k) st[2*v][i].x += lazy[v]; foru (i, k) st[2*v+1][i].x += lazy[v]; lazy[v] = 0; } void _merge(int v) { // df("merge %d with:\n"); int a = 0, b = 0; foru (i, k) { const auto& nl = st[2*v][a], nr = st[2*v+1][b]; // df("choose %d or %d [%d %d]\n", nl.x, nr.x, a, b); if (nl.x == nr.x) { st[v][i] = {nl.x, nl.y + nr.y}; ++a, ++b; } else if (nl.x < nr.x) { st[v][i] = nl; ++a; } else { st[v][i] = nr; ++b; } } // df("\n"); } void _update(int v, int l, int r, int ql, int qr, int val) { // [ql, qr) if (qr <= ql) return; // df("[%d %d] [%d %d] val%d\n", l, r, ql, qr, val); if (ql == l && qr == r) { foru (i, k) st[v][i].x += val; lazy[v] += val; return; } push(v); int m = (l+r)/2; _update(2*v, l, m, ql, min(qr, m), val); _update(2*v+1, m, r, max(ql, m), qr, val); _merge(v); } void update(int l, int r, int val) { _update(1, 0, n, l, r, val); } }; vec<pair<int, bool>> pos; vec<vec<int>> col; int n, k; int _c; bool o(int x, bool y) { return col[(x+n)%n][y] < _c; } int get(int x, bool y) { return col[(x+n)%n][y]; } int calc(int c) { _c = c; if (c == 1e9) return 0; auto [x, y] = pos[c]; int d = 1; if (o(x, !y)) { d -= 1; // opposite if (o(x+1, y)) d -= !o(x+1, !y); // sub only if not connected to opposite if (o(x-1, y)) d -= !o(x-1, !y); } else { d -= o(x-1, y) + o(x+1, y); } return d; } int vmax(vec<int> vs) { int x = -int(1e9); foru (i, vs.size()) { /* df("x = max(%d, %d)\n", x, vs[i]); */ x = max(x, vs[i]); } return x; } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif cin >> n >> k; segtree st(2*n, k+1); // sru d(n); pos.resize(2*n); col.resize(n, vec<int>(2, 1e9)); // padded for easier calc() impl vec<int> ds(2*n); foru (i, n) { cin >> col[i][0]; --col[i][0]; pos[col[i][0]] = {i, 0}; } foru (i, n) { cin >> col[i][1]; --col[i][1]; pos[col[i][1]] = {i, 1}; } foru (i, 2*n) { st.update(i, 2*n, ds[i] = calc(i)); } vec<ll> ans(k+1); int tconn = 0; int aaa; foru (x, n) { // cout << get(x, 0) << " " << get(x, 1) << " " << get(x+1, 0) << " " << get(x-1, 1) << endl; // cout << " " << vmax({get(x, 0), get(x, 1), get(x+1, 0), get(x-1, 1)}) << endl; // cout << " " << vmax({get(x, 0), get(x, 1), get(x-1, 0), get(x+1, 1)}) << endl; // cout << " " << vmax({get(x-1, 0), get(x, 0), get(x+1, 0)}) << endl; // cout << " " << vmax({get(x-1, 1), get(x, 1), get(x+1, 1)}) << endl; tconn = max(tconn, -vmax({ -vmax({get(x, 0), get(x, 1), get(x+1, 0), get(x-1, 1)}), -vmax({get(x, 0), get(x, 1), get(x-1, 0), get(x+1, 1)}), -vmax({get(x-1, 0), get(x, 0), get(x+1, 0)}), -vmax({get(x-1, 1), get(x, 1), get(x+1, 1)}) })); // cout << aaa << endl; } foru (i, 2*n) { if (tconn != 1e9) st.update(tconn, 2*n, 1e8); for (int i = 0; i <= k && st.st[1][i].x <= k; ++i) { ans[st.st[1][i].x] += ll(st.st[1][i].y); } int _s = 0; df("tconn: %d\n", tconn); // foru (i, 2*n) cout << ds[i] << " "; // cout << "\n"; // foru (i, 2*n) { _s += ds[i]; cout << _s << " "; } // cout << "\n"; foru (i, k+1) { df("{%d, %d} ", st.st[1][i].x, st.st[1][i].y); } df("\n"); if (tconn != 1e9) { st.update(tconn, 2*n, -1e8); ans[1] += ll(2*n - tconn); } st.update(i, 2*n, -ds[i]); ds[i] = 0; auto [x, y] = pos[i]; col[x][y] = 1e9; tconn = vmax({tconn, get(x-1, !y), get(x, !y), get(x+1, !y)}); for (int dx = -1; dx <= 1; ++dx) { foru (yy, 2) { if (dx == 0 && yy == y) continue; int c = col[(x+dx+n)%n][yy]; if (c == 1e9) continue; int nd = calc(c); st.update(c, 2*n, nd-ds[c]); ds[c] = nd; } } } fori (i, 1, k) cout << ans[i] << " "; // foru (i, k) cout << ans[i+1] << " "; // cout << "\n"; // assert(ans[0] == 0); return 0; } |