Submission #727807

# Submission time Handle Problem Language Result Execution time Memory
727807 2023-04-21T11:18:02 Z SanguineChameleon Hexagonal Territory (APIO21_hexagon) C++17
47 / 100
648 ms 70724 KB
#include "hexagon.h"
#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool local = true;
	const int subtask = 5;
#else
	const bool local = false;
	const int subtask = -1;
#endif

const long long mod = 1e9 + 7;
const long long one_half = (mod + 1) / 2;
const long long one_third = (mod + 1) / 3;
const int dx[7] = {0, 0, 1, 1, 0, -1, -1};
const int dy[7] = {0, 1, 1, 0, -1, -1, 0};

struct point {
	long long x, y;

	point() {};

	point(long long _x, long long _y): x(_x), y(_y) {};

	point operator*(long long d) {
		return point(x * d, y * d);
	}

	point operator+(point p2) {
		return point(x + p2.x, y + p2.y);
	}

	long long operator^(point p2) {
		return x * p2.y - y * p2.x;
	}
};

point dv[7] = {point(0, 0), point(0, 1), point(1, 1), point(1, 0), point(0, -1), point(-1, -1), point(-1, 0)};

ostream& operator<<(ostream &out, point p) {
	out << "(" << p.x << "," << p.y << ")";
	return out;
}

struct line {
	int type, dir, len;
	point p1, p2;
	int id;

	line() {};

	line(int _type, int _dir, point _p1, point _p2, int _id): type(_type), dir(_dir), p1(_p1), p2(_p2), id(_id) {
		len = p2.x - p1.x + 1;
	};

	long long calc() {
		long long Ly = p1.y;
		long long Ry = p1.y + (dir == 1 ? 0 : len - 1);
		if (type == 1) {
			Ly = 1 - Ly;
			Ry = 1 - Ry;
		}
		return (Ly + Ry) % mod * len % mod * one_half % mod;
	}

	long long eval_x(long long x) {
		if (dir == 1) {
			return p1.y;
		}
		else {
			return p1.y + (x - p1.x);
		}
	}

	void shorten(long long left_x, long long right_x) {
		long long v1 = eval_x(left_x);
		long long v2 = eval_x(right_x);
		p1 = point(left_x, v1);
		p2 = point(right_x, v2);
	}
};

ostream& operator<<(ostream &out, line L) {
	if (L.type == 1) {
		out << "(bottom,";
	}
	else {
		out << "(top,";
	}
	out << L.p1 << "," << L.p2 << ")";
	return out;
}

bool operator<(line L1, line L2) {
	int max_x = max(L1.p1.x, L2.p1.x);
	int v1 = L1.eval_x(max_x);
	int v2 = L2.eval_x(max_x);
	if (v1 != v2) {
		return v1 < v2;
	}
	else {
		return L1.type < L2.type;
	}
};

struct comp {
	line bottom, top;
	int left_x, right_x, len;

	comp(line _bottom, line _top, int _left_x, int _right_x): bottom(_bottom), top(_top), left_x(_left_x), right_x(_right_x) {
		bottom.shorten(left_x, right_x);
		top.shorten(left_x, right_x);
		len = right_x - left_x + 1;
	}

	long long calc() {
		long long Ly = top.p1.y - bottom.p1.y + 1;
		long long Ry = top.p2.y - bottom.p2.y + 1;
		return (Ly + Ry) % mod * len % mod * one_half % mod;
	}
};

ostream& operator<<(ostream &out, comp C) {
	out << "(" << C.bottom << "," << C.top << "," << "(" << C.left_x << "," << C.right_x << "))";
	return out;
}

vector<comp> comps;
vector<vector<int>> adj;

int draw_territory(int N, int A, int B, vector<int> D, vector<int> L) {
	if (N == 3 && (!local || subtask == 1 || subtask == 2)) {
		int len = L[0] + 1;
		long long A_sum = 1LL * len * (len + 1) % mod * one_half % mod;
		long long B_sum = 1LL * len * (len - 1) % mod * one_half % mod + (len - 1) * len % mod * (len * 2 - 1) % mod * one_half % mod * one_third % mod;
		return (A_sum * A + B_sum * B) % mod;
	}
	long long L_sum = 0;
	for (int i = 0; i < N; i++) {
		L_sum += L[i];
	}
	if (L_sum <= 2000 && (!local || subtask == 3)) {
		vector<vector<bool>> border(4069, vector<bool>(4069, false));
		vector<vector<int>> dist(4069, vector<int>(4069, -1));
		int cx = 0;
		int cy = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < L[i]; j++) {
				cx += dx[D[i]];
				cy += dy[D[i]];
				border[cx + 2023][cy + 2023] = true;
			}
		}
		deque<pair<int, int>> q = {{0, 0}};
		dist[0][0] = -2;
		while (!q.empty()) {
			cx = q.front().first;
			cy = q.front().second;
			q.pop_front();
			for (int d = 1; d <= 6; d++) {
				int nx = cx + dx[d];
				int ny = cy + dy[d];
				if (0 <= nx && nx < 4069 && 0 <= ny && ny < 4069 && !border[nx][ny] && dist[nx][ny] == -1) {
					dist[nx][ny] = -2;
					q.push_back({nx, ny});
				}
			}
		}
		q.push_back({2023, 2023});
		dist[2023][2023] = 0;
		long long res = 0;
		while (!q.empty()) {
			cx = q.front().first;
			cy = q.front().second;
			res += 1LL * B * dist[cx][cy] + A;
			res %= mod;
			q.pop_front();
			for (int d = 1; d <= 6; d++) {
				int nx = cx + dx[d];
				int ny = cy + dy[d];
				if (0 <= nx && nx < 4069 && 0 <= ny && ny < 4069 && dist[nx][ny] == -1) {
					dist[nx][ny] = dist[cx][cy] + 1;
					q.push_back({nx, ny});
				}
			}
		}
		return res;
	}
	long long area = 0;
	point cur = point(0, 0);
	vector<point> poly(N);
	for (int i = 0; i < N; i++) {
		poly[i] = cur;
		cur = cur + (dv[D[i]] * L[i]);
	}
	for (int i = 0; i < N; i++) {
		area += poly[i] ^ poly[(i + 1) % N];
	}
	if (area < 0) {
		reverse(D.begin(), D.end());
		for (int i = 0; i < N; i++) {
			if (D[i] <= 3) {
				D[i] += 3;
			}
			else {
				D[i] -= 3;
			}
		}
		reverse(L.begin(), L.end());
		cur = point(0, 0);
		for (int i = 0; i < N; i++) {
			poly[i] = cur;
			cur = cur + (dv[D[i]] * L[i]);
		}
	}
	vector<line> lines;
	vector<int> prv_x;
	for (int i = 0; i < N; i++) {
		point prv = poly[(i + (N - 1)) % N];
		point cur = poly[i];
		point nxt1 = poly[(i + 1) % N];
		point nxt2 = poly[(i + 2) % N];
		if (cur.x < nxt1.x) {
			point p1 = cur;
			point p2 = nxt1;
			int dir = (cur.y == nxt1.y ? 1 : 2);
			if (prv.x < cur.x) {
				if (dir == 1) {
					p1 = p1 + point(1, 0);
				}
				else {
					p1 = p1 + point(1, 1);
				}
			}
			if (prv.x == cur.x && prv.y < cur.y) {
				if (dir == 1) {
					p1 = p1 + point(1, 0);
				}
				else {
					p1 = p1 + point(1, 1);
				}
			}
			if (nxt1.x == nxt2.x && nxt1.y > nxt2.y) {
				if (dir == 1) {
					p2 = p2 + point(-1, 0);
				}
				else {
					p2 = p2 + point(-1, -1);
				}
			}
			if (p1.x <= p2.x) {
				lines.emplace_back(1, dir, p1, p2, lines.size());
				prv_x.push_back(p1.x);
			}
		}
		if (cur.x > nxt1.x) {
			point p1 = nxt1;
			point p2 = cur;
			int dir = (cur.y == nxt1.y ? 1 : 2);
			if (prv.x > cur.x) {
				if (dir == 1) {
					p2 = p2 + point(-1, 0);
				}
				else {
					p2 = p2 + point(-1, -1);
				}
			}
			if (prv.x == cur.x && prv.y > cur.y) {
				if (dir == 1) {
					p2 = p2 + point(-1, 0);
				}
				else {
					p2 = p2 + point(-1, -1);
				}
			}
			if (nxt1.x == nxt2.x && nxt1.y < nxt2.y) {
				if (dir == 1) {
					p1 = p1 + point(1, 0);
				}
				else {
					p1 = p1 + point(1, 1);
				}
			}
			if (p1.x <= p2.x) {
				lines.emplace_back(2, dir, p1, p2, lines.size());
				prv_x.push_back(p1.x);
			}
		}
	}
	vector<pair<int, pair<int, int>>> events;
	for (int i = 0; i < (int)lines.size(); i++) {
		events.push_back(make_pair(lines[i].p1.x - 1, make_pair(2, i)));
		events.push_back(make_pair(lines[i].p1.x, make_pair(0, i)));
		events.push_back(make_pair(lines[i].p2.x, make_pair(1, i)));
	}
	sort(events.begin(), events.end());
	set<line, less<>> S;
	for (auto e: events) {
		int cur_x = e.first;
		int type = e.second.first;
		line L = lines[e.second.second];
		auto it = (type == 0 ? S.insert(L).first : S.lower_bound(L));
		if (type == 1) {
			if (it->type == 1 && next(it) != S.end()) {
				auto bottom = it;
				auto top = next(it);
				if (prv_x[bottom->id] <= cur_x && prv_x[top->id] <= cur_x) {
					comps.emplace_back(*bottom, *top, prv_x[bottom->id], cur_x);
					prv_x[bottom->id] = cur_x + 1;
					prv_x[top->id] = cur_x + 1;
				}
			}
			if (it->type == 2 && it != S.begin()) {
				auto bottom = prev(it);
				auto top = it;
				if (prv_x[bottom->id] <= cur_x && prv_x[top->id] <= cur_x) {
					comps.emplace_back(*bottom, *top, prv_x[bottom->id], cur_x);
					prv_x[bottom->id] = cur_x + 1;
					prv_x[top->id] = cur_x + 1;
				}
			}
			S.erase(it);
		}
		if (type == 2) {
			if (it != S.end() && it != S.begin()) {
				auto bottom = prev(it);
				auto top = it;
				if (bottom->type == 1 && top->type == 2) {
					if (prv_x[bottom->id] <= cur_x && prv_x[top->id] <= cur_x) {
						comps.emplace_back(*bottom, *top, prv_x[bottom->id], cur_x);
						prv_x[bottom->id] = cur_x + 1;
						prv_x[top->id] = cur_x + 1;
					}
				}
			}
		}
	}
	adj.resize(comps.size());
	events.clear();
	for (int i = 0; i < (int)comps.size(); i++) {
		events.push_back(make_pair(comps[i].right_x + 1, make_pair(comps[i].bottom.p2.y, i)));
		events.push_back(make_pair(comps[i].left_x, make_pair(comps[i].bottom.p1.y, -i - 1)));
	}
	sort(events.begin(), events.end());
	int lid = -1;
	int rid = -1;
	int edge_cnt = 0;
	for (auto e: events) {
		if (e.second.second >= 0) {
			lid = e.second.second;
		}
		else {
			rid = -e.second.second - 1;
		}
		if (lid >= 0 && rid >= 0 && comps[lid].right_x + 1 == comps[rid].left_x && max(comps[lid].bottom.p2.y, comps[rid].bottom.p1.y) <= min(comps[lid].top.p2.y, comps[rid].top.p1.y)) {
			adj[lid].push_back(rid);
			adj[rid].push_back(lid);
			edge_cnt++;
		}
	}
	if (B == 0 && (!local || subtask == 4 || subtask == 5)) {
		long long res = 0;
		for (auto C: comps) {
			res += C.calc();
			res %= mod;
		}
		if (res < 0) {
			res += mod;
		}
		res = res * A % mod;
		return res;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 626 ms 67540 KB Output is correct
2 Correct 648 ms 67620 KB Output is correct
3 Correct 637 ms 67564 KB Output is correct
4 Correct 634 ms 67532 KB Output is correct
5 Correct 643 ms 67532 KB Output is correct
6 Correct 616 ms 67676 KB Output is correct
7 Correct 625 ms 67556 KB Output is correct
8 Correct 633 ms 67656 KB Output is correct
9 Correct 616 ms 67660 KB Output is correct
10 Correct 624 ms 67568 KB Output is correct
11 Correct 630 ms 67916 KB Output is correct
12 Correct 626 ms 67572 KB Output is correct
13 Correct 625 ms 67544 KB Output is correct
14 Correct 617 ms 67800 KB Output is correct
15 Correct 612 ms 67652 KB Output is correct
16 Correct 625 ms 67656 KB Output is correct
17 Correct 634 ms 67648 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 6 ms 2332 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 4 ms 1360 KB Output is correct
6 Correct 8 ms 2360 KB Output is correct
7 Correct 27 ms 6572 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 42 ms 17660 KB Output is correct
12 Correct 27 ms 9816 KB Output is correct
13 Correct 20 ms 9560 KB Output is correct
14 Correct 43 ms 12364 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 7 ms 2332 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 4 ms 1360 KB Output is correct
8 Correct 8 ms 2488 KB Output is correct
9 Correct 30 ms 6572 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 38 ms 17744 KB Output is correct
14 Correct 25 ms 9912 KB Output is correct
15 Correct 20 ms 9536 KB Output is correct
16 Correct 44 ms 12456 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 65 ms 18876 KB Output is correct
21 Correct 6 ms 2360 KB Output is correct
22 Correct 3 ms 1280 KB Output is correct
23 Correct 83 ms 23660 KB Output is correct
24 Correct 141 ms 39528 KB Output is correct
25 Correct 151 ms 40088 KB Output is correct
26 Correct 84 ms 20708 KB Output is correct
27 Correct 65 ms 17544 KB Output is correct
28 Correct 43 ms 10244 KB Output is correct
29 Correct 177 ms 70724 KB Output is correct
30 Correct 97 ms 38596 KB Output is correct
31 Correct 90 ms 38576 KB Output is correct
32 Correct 194 ms 64412 KB Output is correct
33 Correct 66 ms 18992 KB Output is correct
34 Correct 24 ms 7636 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 621 ms 67632 KB Output is correct
2 Correct 620 ms 67796 KB Output is correct
3 Correct 614 ms 67656 KB Output is correct
4 Correct 606 ms 67704 KB Output is correct
5 Correct 624 ms 67672 KB Output is correct
6 Correct 616 ms 67696 KB Output is correct
7 Correct 611 ms 67676 KB Output is correct
8 Correct 610 ms 67648 KB Output is correct
9 Correct 615 ms 67560 KB Output is correct
10 Correct 613 ms 67684 KB Output is correct
11 Correct 596 ms 67552 KB Output is correct
12 Correct 638 ms 67572 KB Output is correct
13 Correct 615 ms 67568 KB Output is correct
14 Correct 590 ms 67672 KB Output is correct
15 Correct 591 ms 67648 KB Output is correct
16 Correct 602 ms 67560 KB Output is correct
17 Correct 637 ms 67684 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 7 ms 2424 KB Output is correct
20 Correct 2 ms 828 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 5 ms 1364 KB Output is correct
23 Correct 8 ms 2488 KB Output is correct
24 Correct 30 ms 6616 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 41 ms 17732 KB Output is correct
29 Correct 25 ms 9816 KB Output is correct
30 Correct 19 ms 9636 KB Output is correct
31 Correct 41 ms 12456 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Incorrect 0 ms 212 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 598 ms 67568 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 598 ms 67544 KB Output is correct
7 Correct 625 ms 67524 KB Output is correct
8 Correct 611 ms 67552 KB Output is correct
9 Correct 597 ms 67516 KB Output is correct
10 Correct 612 ms 67532 KB Output is correct
11 Correct 611 ms 67668 KB Output is correct
12 Correct 616 ms 67660 KB Output is correct
13 Correct 617 ms 67560 KB Output is correct
14 Correct 629 ms 67560 KB Output is correct
15 Correct 636 ms 67636 KB Output is correct
16 Correct 640 ms 67660 KB Output is correct
17 Correct 626 ms 67676 KB Output is correct
18 Correct 604 ms 67644 KB Output is correct
19 Correct 625 ms 67648 KB Output is correct
20 Correct 630 ms 67788 KB Output is correct
21 Correct 629 ms 67672 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 7 ms 2404 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 4 ms 1360 KB Output is correct
27 Correct 8 ms 2360 KB Output is correct
28 Correct 24 ms 6492 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 42 ms 17732 KB Output is correct
33 Correct 26 ms 9816 KB Output is correct
34 Correct 21 ms 9652 KB Output is correct
35 Correct 47 ms 12460 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 70 ms 18816 KB Output is correct
40 Correct 7 ms 2360 KB Output is correct
41 Correct 3 ms 1280 KB Output is correct
42 Correct 86 ms 23660 KB Output is correct
43 Correct 144 ms 39496 KB Output is correct
44 Correct 150 ms 40152 KB Output is correct
45 Correct 81 ms 20644 KB Output is correct
46 Correct 67 ms 17464 KB Output is correct
47 Correct 45 ms 10180 KB Output is correct
48 Correct 185 ms 70696 KB Output is correct
49 Correct 95 ms 38588 KB Output is correct
50 Correct 97 ms 38596 KB Output is correct
51 Correct 201 ms 64428 KB Output is correct
52 Correct 68 ms 19088 KB Output is correct
53 Correct 24 ms 7680 KB Output is correct
54 Incorrect 0 ms 212 KB Output isn't correct
55 Halted 0 ms 0 KB -