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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cassert>
#include "osalib.h"

struct Fenwick {
	std::vector<int> tree;
	Fenwick(int len) : tree(len) {}

	void add(int i, int val) {
		for (; i < int(tree.size()); i |= i+1) {
			tree[i] += val;
		}
	}

	int sum(int i) {
		int val = 0;
		for (; i > 0; i &= i-1) {
			val += tree[i-1];
		}
		return val;
	}
};

enum VertType {
	FREE,
	VILLAGE,
	BLOCKED
};

struct Vertex {
	int x, y;
	VertType type{FREE};

	Vertex* parent{nullptr};
	std::vector<Vertex*> edges;

	int area{0};
	Vertex* fuParent{nullptr};
	int fuSize{1}, fuDepth{0};

	Vertex* getFuParent() {
		if (fuParent) {
			fuParent = fuParent->getFuParent();
			return fuParent;
		}
		return this;
	}

	int getArea();
};

// ---

int hei, wid, nVillages;
std::vector<std::vector<Vertex>> verts;
std::vector<Fenwick> prefs, adds;

int Vertex::getArea() {
	return area + adds[x].sum(y);
}

Vertex* fuUnion(Vertex* l, Vertex* r) {
	l = l->getFuParent();
	r = r->getFuParent();

	if (l->fuDepth < r->fuDepth) {
		l->fuParent = r;
		r->fuSize += l->fuSize;
		return r;
	}

	r->fuParent = l;
	l->fuSize += r->fuSize;
	if (l->fuDepth == r->fuDepth) {
		l->fuDepth++;
	}
	return l;
}

bool checkCycle(Vertex* newVert, Vertex* v1, Vertex* v2) {
	Vertex* parent1 = v1->getFuParent();
	Vertex* parent2 = v2->getFuParent();

	if (parent1 != parent2) {
		return false;
	}

	int area = v1->getArea() - v2->getArea();
	int col = prefs[newVert->x].sum(newVert->y);

	area += (newVert->x - v1->x) * (prefs[v1->x].sum(v1->y) + col);
	area += (v2->x - newVert->x) * (prefs[v2->x].sum(v2->y) + col);
	area = abs(area) / 2;

	return area != 0 && area != nVillages;
}

void dfsMerge(Vertex* vert, int parentSum) {
	Vertex* parent = vert->parent;
	int sum = prefs[vert->x].sum(vert->y);

	vert->area = parent->getArea() - adds[vert->x].sum(vert->y);
	vert->area += (vert->x - parent->x) * (sum + parentSum);

	for (Vertex* child : vert->edges) {
		if (child != parent) {
			child->parent = vert;
			dfsMerge(child, sum);
		}
	}
}

void merge(Vertex* v1, Vertex* v2) {
	Vertex* parent1 = v1->getFuParent();
	Vertex* parent2 = v2->getFuParent();

	if (parent1 == parent2) { // Keep being tree
		return;
	}

	if (parent1->fuSize < parent2->fuSize) { // Merge smaller to greater
		std::swap(v1, v2);
		std::swap(parent1, parent2);
	}
	fuUnion(parent1, parent2);

	v2->parent = v1;
	v1->edges.push_back(v2);
	v2->edges.push_back(v1);
	dfsMerge(v2, prefs[v1->x].sum(v1->y));
}

int NowaWarownia(int y, int x) {
	std::vector<Vertex*> neigh;
	assert(verts[y][x].type == FREE);
	Vertex* cur = &verts[y][x];

	// Find blocking neighbours

	for (int dy = -1; dy <= 1; dy++) {
		if (y+dy < 0 || y+dy >= hei) {
			continue;
		}

		for (int dx = -1; dx <= 1; dx++) {
			if (x+dx < 0 || x+dx >= wid) {
				continue;
			}

			Vertex& candidate = verts[y+dy][x+dx];
			if (candidate.type == BLOCKED) {
				neigh.push_back(&candidate);
			}
		}
	}

	// Check for cycles

	for (size_t i = 0; i < neigh.size(); i++) {
		for (size_t j = i+1; j < neigh.size(); j++) {
			int dx = abs(neigh[i]->x - neigh[j]->x), dy = abs(neigh[i]->y - neigh[j]->y);
			if ((dx > 1 || dy > 1) && checkCycle(cur, neigh[i], neigh[j])) {
				return 0;
			}
		}
	}

	// Merge

	cur->type = BLOCKED;
	for (Vertex* vert : neigh) {
		merge(vert, cur);
	}
	return 1;
}

void PrzeniesOsade(int y1, int x1, int y2, int x2) {
	assert(verts[y1][x1].type == VILLAGE && verts[y2][x2].type == FREE);
	verts[y1][x1].type = FREE;
	verts[y2][x2].type = VILLAGE;
	
	prefs[x1].add(y1, -1);
	prefs[x2].add(y2, 1);
	if (x1 == x2) {
		return;
	}

	adds[x1].add(y1, (x1 < x2 ? -1 : 1));
	adds[x2].add(y2, (x1 < x2 ? -1 : 1));
}

void NowaWyspa(int n, int m, char **board) {
	hei = n+2;
	wid = m+2;

	verts.resize(hei, std::vector<Vertex>(wid));
	prefs.resize(wid, Fenwick(hei));
	adds.resize(wid, Fenwick(hei));

	for (int y = 0; y < hei; y++) {
		for (int x = 0; x < wid; x++) {
			verts[y][x].x = x;
			verts[y][x].y = y;
		}
	}

	for (int y = 0; y < n; y++) {
		for (int x = 0; x < m; x++) {
			if (board[y][x] == 'K') {
				prefs[x+1].add(y+1, 1);
				verts[y+1][x+1].type = VILLAGE;
				nVillages++;
			}
		}
	}

	for (int x = 1; x < wid-1; x++) {
		assert(NowaWarownia(0, x));
		assert(NowaWarownia(hei-1, x));
	}
	for (int y = 1; y < hei-1; y++) {
		assert(NowaWarownia(y, 0));
		assert(NowaWarownia(y, wid-1));
	}

	for (int y = 0; y < n; y++) {
		for (int x = 0; x < m; x++) {
			if (board[y][x] == 'W') {
				assert(NowaWarownia(y+1, x+1));
			}
		}
	}
}