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
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
	return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
	o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif

using Val = pair<int, array<int, 11>>;

Val empty_val = [] {
	Val ret;
	ret.first = 11;
	fill(ret.second.begin(), ret.second.end(), 0);
	return ret;
}();

Val merge(const Val &l, const Val &r) {
	Val ret = empty_val;
	ret.first = min(l.first, r.first);
	REP(i, 11) {
		auto retrieve = [&](const Val &x) {
			if(ret.first + i < x.first)
				return 0;
			return x.second[ret.first - x.first + i];
		};
		ret.second[i] = retrieve(l) + retrieve(r);
	}
	return ret;
}

struct Node {
	Val val;
	int lazy = 0;
	int subsz = 1;
	Node() {
		val = empty_val;
	}
};

Node merge(const Node &l, const Node &r) {
	Node ret;
	ret.val = merge(l.val, r.val);
	ret.subsz = l.subsz + r.subsz;
	return ret;
}

struct Tree {
	int sz = 1;
	vector<Node> tree;

	Tree(vector<int> initial_comp_cnt) {
		int n = ssize(initial_comp_cnt);
		while(sz < n)
			sz *= 2;
		tree.resize(2 * sz);
		REP(i, n) {
			tree[i + sz].val.first = initial_comp_cnt[i];
			tree[i + sz].val.second[0] = 1;
		}
		for(int v = sz - 1; v > 0; --v)
			tree[v] = merge(tree[2 * v], tree[2 * v + 1]);
	}

	void push(int v) {
		for(int u : {2 * v, 2 * v + 1}) {
			tree[u].val.first += tree[v].lazy;
			tree[u].lazy += tree[v].lazy;
		}
		tree[v].lazy = 0;
	}

	void disable(int i, int v = 1) {
		if(v >= sz) {
			tree[v] = Node();
			return;
		}
		push(v);
		int m2 = tree[v].subsz / 2;
		disable(i >= m2 ? i - m2 : i, 2 * v + bool(i >= m2));
		tree[v] = merge(tree[2 * v], tree[2 * v + 1]);
	}

	void add(int l, int r, int delta, int v = 1) {
		if(v == 1)
			debug(l, r, delta);
		if(l == 0 and r == tree[v].subsz - 1) {
			tree[v].val.first += delta;
			tree[v].lazy += delta;
			return;
		}
		push(v);
		int m2 = tree[v].subsz / 2;
		if(r < m2)
			add(l, r, delta, 2 * v);
		else if(l >= m2)
			add(l - m2, r - m2, delta, 2 * v + 1);
		else {
			add(l, m2 - 1, delta, 2 * v);
			add(0, r - m2, delta, 2 * v + 1);
		}
		tree[v] = merge(tree[2 * v], tree[2 * v + 1]);
	}

	Val get() {
		return tree[1].val;
	}
};

int main() {
	cin.tie(0)->sync_with_stdio(0);

#ifdef TESTRUN
	int n = int(1e5);
	int k = 10;
	mt19937 rng(0);
	vector<int> perm(2 * n);
	iota(perm.begin(), perm.end(), 0);
	shuffle(perm.begin(), perm.end(), rng);
	vector<array<int, 2>> in(n);
	REP(y, 2)
		REP(x, n)
			in[x][y] = perm[y * n + x];
#else
	int n, k;
	cin >> n >> k;
	vector<array<int, 2>> in(n);
	REP(y, 2)
		REP(x, n) {
			cin >> in[x][y];
			--in[x][y];
		}
#endif
	debug(n, k, in);

	vector<pair<int, int>> col_to_coord(2 * n);
	REP(x, n)
		REP(y, 2)
			col_to_coord[in[x][y]] = {x, y};
	debug(col_to_coord);

	vector<int> initial_comp_cnt(2 * n);
	vector<array<bool, 2>> lit(n, {{false, false}});

	auto get_diff_comp_cnt = [&](int x, int y) {
		int xl = (x == 0 ? n - 1 : x - 1);
		int xr = (x == n - 1 ? 0 : x + 1);
		int y1 = y ^ 1;

		int by_side = int(lit[xl][y]) + int(lit[x][y1]) + int(lit[xr][y]);
		int by_point = int(lit[xl][y1]) + int(lit[xr][y1]);

		if(not lit[xl][y] and not lit[x][y1] and not lit[xr][y])
			return 1;
		else if(by_side == 3 and by_point == 2)
			return 0;
		else if(by_side == 1)
			return 0;
		else if(by_side == 2 and lit[x][y1] and ((lit[xl][y] and lit[xl][y1]) or (lit[xr][y] and lit[xr][y1])))
			return 0;
		else if(by_side == 2)
			return -1;
		else if(by_side == 3 and by_point == 1)
			return -1;
		else if(by_side == 3 and by_point == 0)
			return -2;
		else
			assert(false);
	};

	REP(end, 2 * n) {
		static int comp_cnt = 0;
		auto [x, y] = col_to_coord[end];
		comp_cnt += get_diff_comp_cnt(x, y);
		lit[x][y] = true;
		debug(end, comp_cnt);
		initial_comp_cnt[end] = comp_cnt;
	}
	debug(initial_comp_cnt);

	vector<LL> ans(k + 1);
	REP(end, 2 * n)
		if(initial_comp_cnt[end] <= k)
			ans[max(1, initial_comp_cnt[end])] += 1;

	Tree tree(initial_comp_cnt);

	REP(x, n)
		REP(y, 2)
			lit[x][y] = false;

	REP(removing, 2 * n) {
		auto [x, y] = col_to_coord[removing];
		debug(removing, x, y);
		vector<int> time_points_changes = {2 * n};
		for(int xx : {x == 0 ? n - 1 : x - 1, x, x == n - 1 ? 0 : x + 1})
			for(int yy : {y, (y ^ 1)})
				if(in[xx][yy] >= in[x][y])
					time_points_changes.emplace_back(in[xx][yy]);
		sort(time_points_changes.begin(), time_points_changes.end());
		debug(removing, time_points_changes);
		assert(time_points_changes.front() == removing and time_points_changes.back() == 2 * n);

		REP(it, ssize(time_points_changes) - 1) {
			int l = time_points_changes[it];
			int r = time_points_changes[it + 1] - 1;
			auto [xx, yy] = col_to_coord[l];
			lit[xx][yy] = true;
			debug(removing, l, lit);
			int diff = get_diff_comp_cnt(x, y);
			debug(removing, l, r, diff);
			if(l <= r)
				tree.add(l, r, -diff);
		}
		REP(it, ssize(time_points_changes) - 1) {
			auto [xx, yy] = col_to_coord[time_points_changes[it]];
			lit[xx][yy] = false;
		}
		tree.disable(removing);

		Val tree_val = tree.get();
		REP(i, 11) {
			int comp_cnt = max(1, tree_val.first + i);
			if(comp_cnt <= k)
				ans[comp_cnt] += tree_val.second[i];
		}
	}

	FOR(comp_cnt, 1, k)
		cout << ans[comp_cnt] << ' ';
	cout << '\n';
}