Submission #854701

# Submission time Handle Problem Language Result Execution time Memory
854701 2023-09-28T15:10:01 Z dlalswp25 Circle selection (APIO18_circle_selection) C++17
37 / 100
3000 ms 814412 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

const int K = 3;

int N;
int X[303030];
int Y[303030];
int R[303030];
int XL[303030];
int XR[303030];
vector<pii> UL[303030];
vector<pii> UR[303030];
vector<int> V[303030];
int ans[303030];

struct DSU {
	int n;
	vector<int> l, r, lc, rc;
	int c;

	DSU(int _n = 0) : n(_n), l(_n + 1), r(_n + 1), lc(_n), rc(_n), c(0) {
		l[0] = -1; r[0] = n;
	}

	int left(int x) {
		x = min(x, n - 1);
		return x < 0 ? -1 : l[lc[x]];
	}
	int right(int x) {
		x = max(x, 0);
		return x >= n ? n : r[rc[x]];
	}

	void upd(int x) {
		int lb = left(x), rb = right(x);
		c++;
		if (x - lb <= rb - x) {
			for (int i = lb + 1; i <= x; i++) rc[i] = c;
			r[c] = x;
			for (int i = max(0, lb); i < x; i++) lc[i] = c;
			l[c] = lb; l[lc[x]] = x;
		} else {
			for (int i = x + 1; i <= min(n - 1, rb); i++) rc[i] = c;
			r[c] = rb;
			r[rc[x]] = x;
			for (int i = x; i < rb; i++) lc[i] = c;
			l[c] = x;
		}
	}
};

bool intersect(int i, int j) {
	int dx = X[i] - X[j], dy = Y[i] - Y[j], r = R[i] + R[j];
	return (long long)dx * dx + (long long)dy * dy <= (long long)r * r;
}

void upd_ans(int cur, int cand) {
	int &t = ans[cur];
	if (R[t] > R[cand] || (R[t] == R[cand] && t < cand)) return;
	if (intersect(cur, cand)) t = cand;
}

struct SegTree {
	struct Node {
		vector<pii> v;
		DSU uf;
	};

	int n, base;
	vector<Node> T;

	SegTree(int _n) : n(_n) {
		for (base = 1; base < n; base <<= 1);
		T.resize(base + base);
	}

	void add_line(int p, int q, int y, int i) {
		p += base; q += base;
		p--; q--;
		while (p <= q) {
			if (p & 1) {
				V[i].push_back(T[p].v.size());
				T[p].v.emplace_back(y, i);
				p++;
			}
			if (~q & 1) {
				V[i].push_back(T[q].v.size());
				T[q].v.emplace_back(y, i);
				q--;
			}
			p >>= 1; q >>= 1;
		}
	}

	void add_point(int p, vector<pii> &v) {
		p += base; p--;
		while (p) {
			v.emplace_back(p, T[p].v.size());
			p >>= 1;
		}
	}

	void init() {
		for (int i = 1; i < base + base; i++) T[i].uf = DSU(T[i].v.size());
	}

	void upd(int p, int q, int i) {
		p += base; q += base;
		p--; q--;
		int ptr = 0;
		while (p <= q) {
			if (p & 1) {
				T[p].uf.upd(V[i][ptr++]);
				p++;
			}
			if (~q & 1) {
				T[q].uf.upd(V[i][ptr++]);
				q--;
			}
			p >>= 1; q >>= 1;
		}
	}

	void chk(int cur, vector<pii> &v) {
		for (auto &i : v) {
			int p, s; tie(p, s) = i;
			int t = s;
			for (int j = 0; j < K; j++) {
				t = T[p].uf.right(t);
				if (t >= T[p].v.size()) break;
				upd_ans(cur, T[p].v[t].second);
				t++;
			}
			t = s;
			for (int j = 0; j < K; j++) {
				t = T[p].uf.left(t);
				if (t < 0) break;
				upd_ans(cur, T[p].v[t].second);
				t--;
			}
		}
	}
};

int main() {
	scanf("%d", &N);
	vector<int> v;
	for (int i = 1; i <= N; i++) {
		scanf("%d%d%d", &X[i], &Y[i], &R[i]);
		XL[i] = X[i] - R[i];
		XR[i] = X[i] + R[i];
		v.push_back(XL[i]); v.push_back(XR[i]);
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	for (int i = 1; i <= N; i++) {
		XL[i] = lower_bound(v.begin(), v.end(), XL[i]) - v.begin() + 1;
		XR[i] = lower_bound(v.begin(), v.end(), XR[i]) - v.begin() + 1;
	}

	int M = v.size();
	SegTree seg(M);

	vector<int> ord(N);
	iota(ord.begin(), ord.end(), 1);

	sort(ord.begin(), ord.end(), [&](int a, int b) { return Y[a] < Y[b]; });
	for (int i : ord) {
		seg.add_line(XL[i], XR[i], Y[i], i);
		seg.add_point(XL[i], UL[i]);
		seg.add_point(XR[i], UR[i]);
	}
	seg.init();

	sort(ord.begin(), ord.end(), [&](int a, int b) {
		if (R[a] == R[b]) return a < b;
		return R[a] > R[b];
	});
	for (int i : ord) {
		ans[i] = i;
		seg.chk(i, UL[i]);
		seg.chk(i, UR[i]);
		if (ans[i] == i) seg.upd(XL[i], XR[i], i);
	}

	for (int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
	return 0;
}

Compilation message

circle_selection.cpp: In member function 'void SegTree::chk(int, std::vector<std::pair<int, int> >&)':
circle_selection.cpp:133:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     if (t >= T[p].v.size()) break;
      |         ~~^~~~~~~~~~~~~~~~
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:189:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  189 |  for (int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
      |  ^~~
circle_selection.cpp:189:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  189 |  for (int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
      |                                                      ^~~~
circle_selection.cpp:149:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
circle_selection.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |   scanf("%d%d%d", &X[i], &Y[i], &R[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26968 KB Output is correct
2 Correct 5 ms 26972 KB Output is correct
3 Correct 5 ms 26968 KB Output is correct
4 Correct 5 ms 26972 KB Output is correct
5 Correct 5 ms 26972 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27112 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 5 ms 27224 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27272 KB Output is correct
12 Correct 5 ms 27228 KB Output is correct
13 Correct 5 ms 27120 KB Output is correct
14 Correct 5 ms 27228 KB Output is correct
15 Correct 6 ms 27228 KB Output is correct
16 Correct 8 ms 28452 KB Output is correct
17 Correct 9 ms 28660 KB Output is correct
18 Correct 10 ms 28508 KB Output is correct
19 Correct 23 ms 37496 KB Output is correct
20 Correct 23 ms 37468 KB Output is correct
21 Correct 23 ms 37552 KB Output is correct
22 Correct 20 ms 36184 KB Output is correct
23 Correct 25 ms 36696 KB Output is correct
24 Correct 21 ms 36188 KB Output is correct
25 Correct 19 ms 35676 KB Output is correct
26 Correct 20 ms 35932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3062 ms 814412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 27224 KB Output is correct
2 Correct 797 ms 218768 KB Output is correct
3 Correct 2881 ms 705008 KB Output is correct
4 Correct 2943 ms 704844 KB Output is correct
5 Execution timed out 3063 ms 712600 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2328 ms 697316 KB Output is correct
2 Correct 1471 ms 663524 KB Output is correct
3 Execution timed out 3072 ms 769728 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26968 KB Output is correct
2 Correct 5 ms 26972 KB Output is correct
3 Correct 5 ms 26968 KB Output is correct
4 Correct 5 ms 26972 KB Output is correct
5 Correct 5 ms 26972 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27112 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 5 ms 27224 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27272 KB Output is correct
12 Correct 5 ms 27228 KB Output is correct
13 Correct 5 ms 27120 KB Output is correct
14 Correct 5 ms 27228 KB Output is correct
15 Correct 6 ms 27228 KB Output is correct
16 Correct 8 ms 28452 KB Output is correct
17 Correct 9 ms 28660 KB Output is correct
18 Correct 10 ms 28508 KB Output is correct
19 Correct 23 ms 37496 KB Output is correct
20 Correct 23 ms 37468 KB Output is correct
21 Correct 23 ms 37552 KB Output is correct
22 Correct 20 ms 36184 KB Output is correct
23 Correct 25 ms 36696 KB Output is correct
24 Correct 21 ms 36188 KB Output is correct
25 Correct 19 ms 35676 KB Output is correct
26 Correct 20 ms 35932 KB Output is correct
27 Correct 49 ms 48220 KB Output is correct
28 Correct 51 ms 48212 KB Output is correct
29 Correct 50 ms 48212 KB Output is correct
30 Correct 43 ms 45920 KB Output is correct
31 Correct 40 ms 45392 KB Output is correct
32 Correct 36 ms 45336 KB Output is correct
33 Correct 752 ms 249784 KB Output is correct
34 Correct 810 ms 250524 KB Output is correct
35 Correct 851 ms 250556 KB Output is correct
36 Correct 599 ms 211428 KB Output is correct
37 Correct 609 ms 212164 KB Output is correct
38 Correct 666 ms 213960 KB Output is correct
39 Correct 276 ms 67520 KB Output is correct
40 Correct 252 ms 67520 KB Output is correct
41 Correct 284 ms 67524 KB Output is correct
42 Correct 333 ms 73100 KB Output is correct
43 Correct 400 ms 206208 KB Output is correct
44 Correct 387 ms 204748 KB Output is correct
45 Correct 401 ms 206024 KB Output is correct
46 Correct 387 ms 205764 KB Output is correct
47 Correct 385 ms 204276 KB Output is correct
48 Correct 392 ms 205944 KB Output is correct
49 Correct 396 ms 203452 KB Output is correct
50 Correct 390 ms 204996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26968 KB Output is correct
2 Correct 5 ms 26972 KB Output is correct
3 Correct 5 ms 26968 KB Output is correct
4 Correct 5 ms 26972 KB Output is correct
5 Correct 5 ms 26972 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27112 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 5 ms 27224 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27272 KB Output is correct
12 Correct 5 ms 27228 KB Output is correct
13 Correct 5 ms 27120 KB Output is correct
14 Correct 5 ms 27228 KB Output is correct
15 Correct 6 ms 27228 KB Output is correct
16 Correct 8 ms 28452 KB Output is correct
17 Correct 9 ms 28660 KB Output is correct
18 Correct 10 ms 28508 KB Output is correct
19 Correct 23 ms 37496 KB Output is correct
20 Correct 23 ms 37468 KB Output is correct
21 Correct 23 ms 37552 KB Output is correct
22 Correct 20 ms 36184 KB Output is correct
23 Correct 25 ms 36696 KB Output is correct
24 Correct 21 ms 36188 KB Output is correct
25 Correct 19 ms 35676 KB Output is correct
26 Correct 20 ms 35932 KB Output is correct
27 Execution timed out 3062 ms 814412 KB Time limit exceeded
28 Halted 0 ms 0 KB -