답안 #246691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246691 2020-07-09T21:41:14 Z xiryss Drzava (COCI15_drzava) C++17
160 / 160
914 ms 28840 KB
//#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
/*#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/
//#pragma comment(linker, "/STACK:367077216")
#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <set>
#include <map>
#include <deque>
#include <string>
#include <cstdint>
#include <cmath>
#include <queue>
#include <cassert>
#include <random>
#include <bitset>
#include <iomanip>
#include <numeric>
#include <time.h>//////////////
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
//#define endl '\n'
#define mp make_pair
#define pbc push_back
#define pob pop_back()
#define empb emplace_back
#define queuel queue<long long>
#define sqr(a) ((a) * (a))
#define sqrl(a) (ll(a)*ll(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pin(p) cin >> p.first >> p.second;
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define rev(v) reverse(v.begin(), v.end());
#define sout(s, c) for (auto i : s) cout << i << c;
#define pout(p) cout << p.first << " " << p.second;
#define er(v, l, r) erase(v.begin() + l, v.begin() + r);
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
#define vout(v, c) for (int i = 0; i < v.size(); ++i) cout << v[i] << c;
#define pushi(v, a) for (int i = 0; i < a.size(); ++i) v.push_back(a[i]);
#define fastio() ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL))
#define dab(v) for(auto i:v)cout<<i<<' ';
#define sp system("pause")
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
using namespace std;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
mt19937 rnd(time(0) + 228 + 'k' + 'e' + 'k' + 'e' + 'r' + 'o' + 'f' + 'e' + 'y');
const ld EPS = 1e-5;//(cos,-sin), (sin, cos)
bool checkprime(int x)
{
	for (int i = 2; i * i <= x; ++i) if (x % i == 0) return 0;
	return 1;
}
const ld PI = acos(-1);
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const ll INF = 1e18;
int mod = MOD7;
const int inf = 1e9;
const int MAXN = 5e4 + 2;
int p[MAXN];
int sz[MAXN];
vector<int> have[MAXN];
pair<pair<int, int>, int> a[MAXN];

inline void make(int v)
{
	p[v] = v, sz[v] = 1;
	have[v] = { a[v - 1].second };
}
inline int fin(int v)
{
	return (v == p[v] ? v : p[v] = fin(p[v]));
}
bool flg = 0;
int n, k;

inline void unite(int a, int b)
{
	a = fin(a);
	b = fin(b);
	if (a == b) return;
	if (sz[a] < sz[b]) swap(a, b);
	sz[a] += sz[b];
	if (sz[a] >= k) flg = 1;
	p[b] = a;
	for (int j : have[b]) have[a].pbc(j);
	
}
int f[MAXN];
inline void add(int i, int x)
{
	for (; i < MAXN; i |= (i + 1)) f[i] += x;
}
inline int get(int r)
{
	int ans = 0;
	for (; r >= 0; r = (r & (r + 1)) - 1) ans += f[r];
	return ans;
}

inline int get(int l, int r)
{
	return get(r) - get(l - 1);
}
inline double dist(pair<int, int> a, pair<int, int> b)
{
	return sqrt(sqrl(a.first - b.first) + sqrl(a.second - b.second));
}
struct query
{
	int r, i, x, y;
	bool type;
	query(int a, int b, int c, int d, bool e)
	{
		tie(r, i, x, y, type) = { a,b,c,d,e };
	}
	query()
	{
		r = i = x = y = type = 0;
	}
};
vector<pair<int, int>> tree[4 * MAXN];
void build(int v, int l, int r, vector<pair<int, int>>& kek)
{
	if (l == r - 1)
	{
		tree[v] = { kek[l] };
		return;
	}
	build(2 * v + 1, l, (l + r) / 2, kek);
	build(2 * v + 2, (l + r) / 2, r, kek);
	merge(all(tree[2 * v + 1]), all(tree[2 * v + 2]), back_inserter(tree[v]));
}
vector<int> joint;
void get(int v, int l, int r, int ql, int qr, int x, int y)
{
	if (l >= qr || ql >= r) return;
	if (l >= ql && r <= qr)
	{
		int xa = lower_bound(all(tree[v]), mp(x, -inf)) - tree[v].begin();
		for (int j = xa; j < tree[v].size() && tree[v][j].first <= y; ++j) joint.pbc(tree[v][j].second);
		return;
	}

	get(2 * v + 1, l, (l + r) / 2, ql, qr, x, y);
	get(2 * v + 2, (l + r) / 2, r, ql, qr, x, y);
}
bool cmp(query a, query b)
{
	return a.r < b.r;
}
int xs[MAXN];
int ys[MAXN];
int ta[MAXN];
int answers[MAXN];
query handle[MAXN * 2];
int hsz = 0;
vector<int> available[MAXN];
vector<query> keks[MAXN];
bool check(int dst)
{
	hsz = 0;
	//	if (n >= 1e4 && cnt >= 20) assert(0);
	fill(f, f + MAXN, 0);
	fill(answers, answers + MAXN, 0);
	for (int i = 0; i < n; ++i)
	{
		int xm = a[i].first.first - dst, xmm = a[i].first.first + dst;
		int ym = a[i].first.second - dst, ymm = a[i].first.second + dst;
		int xfs = lower_bound(xs, xs + n, xm) - xs;
		int xss = upper_bound(xs, xs + n, xmm) - xs - 1;
		int yfs = lower_bound(ys, ys + n, ym) - ys;
		int yss = upper_bound(ys, ys + n, ymm) - ys - 1;
		if (xfs - 1 >= 0)
			handle[hsz++] = query(xfs - 1, i, yfs, yss, 0);
		handle[hsz++] = query(xss, i, yfs, yss, 1);
	}
	stable_sort(handle, handle + hsz, cmp);
	int lft = 0;
	for (int i = 0; i < n; ++i)
	{
		int nowy = lower_bound(ys, ys + n, a[ta[i]].first.second) - ys;
		add(nowy, 1);
		while (lft < hsz && handle[lft].r < i) ++lft;
		while (lft < hsz && handle[lft].r == i)
		{
			int now = get(handle[lft].x, handle[lft].y);
			if (handle[lft].type == 0) answers[handle[lft].i] -= now;
			else answers[handle[lft].i] += now;
			if (answers[handle[lft].i] >= 8 * k + 3) return 1;
			++lft;
		}
	}
	return 0;
}
char used[MAXN];
unsigned dp[31];
inline bool check2(double dst)
{
	flg = 0;
	for (int i = 1; i <= n; ++i) make(i);
	for (int i = 0; i < n; ++i)
	{
		for (int j : available[i])
		{
			if (dist(a[i].first, a[j].first) <= dst)
			{
				unite(i + 1, j + 1);
				if (flg) return 1;
			}
		}
	}
	fill(used, used + MAXN, 0);
	for (int i = 0; i < n; ++i)
	{
		if (used[fin(i + 1)]) continue;
		used[fin(i + 1)] = 1;
		int x = fin(i + 1);
		fill(dp, dp + 31, 0);
		dp[0] = 1;
		for (int j = 0; j < have[x].size(); ++j)
		{
			for (int w = 0; w <= k; ++w)
			{
				if (!((dp[j] >>w)&1) ) continue;
				dp[j + 1]|= (1<<w);
				int tw = w + have[x][j];
				if (tw == k) return 1;
				if (tw >= k) tw -= k;
				dp[j + 1] |= (1<<tw);
			}
		}
	}
	return 0;
}
signed main()
{
	fastio();
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second);
		a[i].second %= k;
	}
	iota(ta, ta + n, 0);
	sort(ta, ta + n, [&](int x, int y) {return a[x].first.first < a[y].first.first; });
	double kekw = 2e9;
	for (int cnt =0 ;cnt< 4;++cnt)
	{
		int i = cnt;
		for (int j = 0; j < n; ++j)
		{
			if (i == j) continue;
			if ((a[i].second + a[j].second) % k == 0)kekw = min(kekw, dist(a[i].first, a[j].first));
		}
	}
	for (int i = 0; i < n; ++i) xs[i] = a[ta[i]].first.first;
	for (int i = 0; i < n; ++i) ys[i] = a[ta[i]].first.second;
	sort(ys, ys + n);
	vector<pair<int, int>> now;
	for (int i : ta)
	{
		int nowy = lower_bound(ys, ys + n, (ld)a[i].first.second) - ys;

		now.pbc({ nowy, i });
	}
	int l = 0, r = min((int)1.5e8, (int)kekw +1);
	while (r - l > 1)
	{
		int m = (l + r) / 2;
		if (check(m))r = m;
		else  l = m;
	}
	build(0, 0, n, now);
	//if (n >= 1e4)assert(0);

	ld dst = r;
	for (int i = 0; i < n; ++i)
	{
		ld xm = a[i].first.first - dst, xmm = a[i].first.first + dst;
		ld ym = a[i].first.second - dst, ymm = a[i].first.second + dst;
		int xfs = lower_bound(xs, xs + n, ld(xm)) - xs;
		int xss = upper_bound(xs, xs + n, xmm) - xs - 1;
		int yfs = lower_bound(ys, ys + n, ld(ym)) - ys;
		int yss = upper_bound(ys, ys + n, ymm) - ys - 1;
		joint.clear();

		get(0, 0, n, xfs, xss + 1, yfs, yss);
	//	if (joint.size() >= 10 * k + 3) assert(0);

		for (int j : joint)
		{
			if (dist(a[i].first, a[j].first) <= dst)
			{
				available[i].pbc(j);
			}
		}
	}
	ld lll = 0;
	ld rr = r + 0.5;
	int am = 13 + (int)(log(rr) / log(2));
	if (n <= 1e3) am += 40;
	for (int cnt = 0; cnt < am; ++cnt)
	{
		//if (abs(r - l) < 1e-4) break;
		ld m = (lll + rr) / 2;
		if (check2(m)) rr = m;
		else lll = m;
	}
	cout << fixed << setprecision(3) << rr;
	sp;
}

Compilation message

drzava.cpp: In function 'void get(int, int, int, int, int, int, int)':
drzava.cpp:166:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = xa; j < tree[v].size() && tree[v][j].first <= y; ++j) joint.pbc(tree[v][j].second);
                    ~~^~~~~~~~~~~~~~~~
drzava.cpp: In function 'bool check2(double)':
drzava.cpp:246:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < have[x].size(); ++j)
                   ~~^~~~~~~~~~~~~~~~
drzava.cpp: In function 'int main()':
drzava.cpp:264:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~
drzava.cpp:267:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drzava.cpp:63:18: warning: ignoring return value of 'int system(const char*)', declared with attribute warn_unused_result [-Wunused-result]
 #define sp system("pause")
            ~~~~~~^~~~~~~~~
drzava.cpp:336:2: note: in expansion of macro 'sp'
  sp;
  ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 14 ms 11516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 14 ms 11516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 11516 KB Output is correct
2 Correct 24 ms 11644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 21 ms 11644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 35 ms 11644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 28 ms 11568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 11516 KB Output is correct
2 Correct 25 ms 11644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 25 ms 11548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 11516 KB Output is correct
2 Correct 326 ms 18040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 777 ms 26236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 914 ms 28840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 788 ms 25836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 11516 KB Output is correct
2 Correct 632 ms 25924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 742 ms 26064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 555 ms 26060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 487 ms 26108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 489 ms 25932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11516 KB Output is correct
2 Correct 704 ms 28240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11516 KB Output is correct
2 Correct 542 ms 25928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 11516 KB Output is correct
2 Correct 700 ms 26144 KB Output is correct