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
#include <stdio.h>
#include <vector>
#include <algorithm>
const int C=300001, D = 1001, MD=201, K=12; //Rozmiar sekwa < DD
using namespace std;

struct event{
	int place;
	std::pair<int, int> limits;	
	int sgn;
};

void parse_event(vector<event>& events, int surrounded, pair<int, int> walls, int n){
	int small_wall = min(walls.first, walls.second), large_wall = max(walls.first, walls.second);

	if (surrounded < small_wall){
		events.push_back({1, {surrounded, small_wall-1}, 1});
		events.push_back({surrounded+1, {surrounded, small_wall-1}, -1});
	}
	else if (surrounded > large_wall){
		events.push_back({large_wall+1, {surrounded, n}, 1});
		events.push_back({surrounded+1, {surrounded, n}, -1});
	}
	else{
		events.push_back({small_wall+1, {surrounded, large_wall-1}, 1});
		events.push_back({surrounded+1, {surrounded, large_wall-1}, -1});
	}
}

void parse_double_event(vector<event>& events, pair<int, int> surrounded, pair<int, int> walls, int n){
	int small_wall = min(walls.first, walls.second), large_wall = max(walls.first, walls.second);
	int small_surrounded = min(surrounded.first, surrounded.second), large_surrounded = max(surrounded.first, surrounded.second);

	if (large_surrounded < small_wall){
		events.push_back({1, {large_surrounded, small_wall-1}, 1});
		events.push_back({small_surrounded+1, {large_surrounded, small_wall-1}, -1});
	}

	if (small_surrounded > large_wall){
		events.push_back({large_wall+1, {large_surrounded, n}, 1});
		events.push_back({small_surrounded+1, {large_surrounded, n}, -1});
	}
	else if (small_surrounded > small_wall && large_surrounded < large_wall){
		events.push_back({small_wall+1, {large_surrounded, large_wall-1}, 1});
		events.push_back({small_surrounded+1, {large_surrounded, large_wall-1}, -1});

	}
	//Czwarty stan zostawiony w piachu intencjonalnie
}

int element[2][C];
vector<event> events; //{event, sgn}, seq_limits
int sqrt_index[C], res[D][K], counter[D], sorted_seq[D][MD+5], unsorted_seq[D][MD+5], k_values[D][K], amount_of_k_values[D];
pair<int, int> indexing[D];

void linear_batch_operations(int index, event parsed_event, int start, int end, int k){
	for (int cur_pos = start; cur_pos <= end; cur_pos++) unsorted_seq[index][cur_pos] += parsed_event.sgn;
	int ln = indexing[index].second - indexing[index].first + 1;
	for (int j=0; j<ln; j++) unsorted_seq[index][j] += counter[index];
	counter[index] = 0;
	for (int j=0; j<ln; j++) sorted_seq[index][j] = unsorted_seq[index][j];
	sort(sorted_seq[index], sorted_seq[index]+ln);

	for (int j=0; j<=k; j++) res[index][j] = 0;
	for (int j=0; j<ln; j++){
		if (unsorted_seq[index][j] <= k) res[index][unsorted_seq[index][j]]++;
	}

	int j=1, i;
	k_values[index][0] = 0;
	for (i=1; i<ln && j<k+2; i++){
		if (sorted_seq[index][i] != sorted_seq[index][i-1]){
			k_values[index][j] = i;
		       	j++;
		}
		//if (j < k+2) k_values[index][j] = ln, j++;
	}
	amount_of_k_values[index] = j;
}


int final_res[K];
int main(){
	int n, k;
	scanf ("%d %d", &n, &k);

	for (int row=0; row<2; row++){
		for (int column=0; column<n; column++){
			scanf ("%d", &element[row][column]);
		}
	}

	for (int column=0; column<n; column++){
		int prev_column = ((column-1) + n)%n;
		parse_event(events, element[0][column], {element[0][prev_column], element[1][column]}, 2*n);
		parse_event(events, element[1][column], {element[1][prev_column], element[0][column]}, 2*n);
		parse_double_event(events, {element[0][column], element[1][column]}, {element[0][prev_column], element[1][prev_column]}, 2*n);
	}
	sort(events.begin(), events.end(), [](event& a, event& b){return a.place < b.place;});
	
	for (int i=1; i<=2*n; i++) sqrt_index[i] = i/MD;
	for (int i = 2*n+1; i <= 2*n+MD; i++) sqrt_index[i] = -1;

	indexing[0] = {1, min(MD-1, 2*n)};
	for (int i=MD; i<=2*n; i+=MD) indexing[i/MD] = {i, min(i+MD-1, 2*n)};

	int cur_event = 0;
	int last_sqrt_index = (2*n)/MD;

	for (int index = 1; index <= last_sqrt_index; index++){
		int ln = indexing[index].second - indexing[index].first + 1;

		for (int j=0; j<ln; j++){
			if (unsorted_seq[index][j] <= k) res[index][unsorted_seq[index][j]]++;
		}

		int j=1;
		k_values[index][0] = 0;
		for (int i=1; i<ln && j<k+2; i++){
			if (sorted_seq[index][i] != sorted_seq[index][i-1]){
				k_values[index][j] = i;
				j++;
			}
		}
		amount_of_k_values[index] = j;
	}


	for (int i=1; i<=2*n; i++){
		//printf ("%d %d %d %d\n", cur_event, events.size(), events[cur_event].place, i);
		for (; cur_event < events.size() && events[cur_event].place == i; cur_event++){
			event parsed_event = events[cur_event];
			//printf ("Event description: %d {%d %d} %d\n", parsed_event.place, parsed_event.limits.first, parsed_event.limits.second, parsed_event.sgn);
			int start = parsed_event.limits.first;
			int end = parsed_event.limits.second;

			int start_sqrt_index = sqrt_index[start];
			int end_sqrt_index = sqrt_index[end];
			if (start > end) continue;

			//counter, sgn, resorta, valid res
			linear_batch_operations(start_sqrt_index, parsed_event, start - indexing[start_sqrt_index].first, min((start_sqrt_index+1)*MD-1, end) - indexing[start_sqrt_index].first, k);
			if (start_sqrt_index == end_sqrt_index) continue;

			for (int index = start_sqrt_index+1; index != end_sqrt_index; index++){ 
				counter[index] += parsed_event.sgn;
				if (parsed_event.sgn == -1){
					for (int j=0; j<k; j++) res[index][j] = res[index][j+1];
					res[index][k] = 0;

					for (int j=amount_of_k_values[index]-2; j>=0; j--){
						if (sorted_seq[index][k_values[index][j]] + counter[index] == k){
							res[index][k] = k_values[index][j+1] - k_values[index][j];
							break;
						}
					}
				}
				else{
					for (int j=k; j>=1; j--) res[index][j] = res[index][j-1];
					res[index][0] = 0;
				}
			}

			linear_batch_operations(end_sqrt_index, parsed_event, 0, end - indexing[end_sqrt_index].first, k);
		}

		int start_sqrt_index = sqrt_index[i];
		int position_in_batch = i - indexing[start_sqrt_index].first;
		int ln = indexing[start_sqrt_index].second - indexing[start_sqrt_index].first + 1;
		for (int cur_pos = position_in_batch; cur_pos < ln; cur_pos++){
			int value = unsorted_seq[start_sqrt_index][cur_pos] + counter[start_sqrt_index];
			if (value <= k && value != 0) final_res[value]++;
			else if (value == 0) final_res[1]++;
		}

		for (int index = start_sqrt_index+1; index <= last_sqrt_index; index++){
			for (int connecteds = 1; connecteds <= k; connecteds++) final_res[connecteds] += res[index][connecteds];
			final_res[1] += res[index][0];
		}
	}
	for (int i=1; i <= k; i++) printf ("%d ", final_res[i]);
	printf ("\n");

return 0;}