Submission #956167

#TimeUsernameProblemLanguageResultExecution timeMemory
956167vjudge1Road Construction (JOI21_road_construction)C++17
100 / 100
3901 ms27180 KiB
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>

#include <bits/stdc++.h>
#include <chrono>
#include <random>

// @author: Vlapos

namespace operators
{
	template <typename T1, typename T2>
	std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
	{
		in >> x.first >> x.second;
		return in;
	}

	template <typename T1, typename T2>
	std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
	{
		out << x.first << " " << x.second;
		return out;
	}

	template <typename T1>
	std::istream &operator>>(std::istream &in, std::vector<T1> &x)
	{
		for (auto &i : x)
			in >> i;
		return in;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}

	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::multiset<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

// usefull stuff

void boost()
{
	ios_base ::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }

const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};

const ll INF = (1e18) + 500;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

#define int ll

struct segTreeSum
{
	vector<ll> tree;
	int sz;

	void init(int n)
	{
		sz = n;
		tree.resize(2 * sz);
	}

	void build(vector<int> &a)
	{
		init(a.size());
		for (int i = 0; i < a.size(); ++i)
			tree[i + sz] = a[sz];

		for (int i = sz - 1; i > 0; --i)
			tree[i] = tree[i << 1] + tree[(i << 1) + 1];
	}

	int get(int l, int r) // [l, r)
	{
		l += sz;
		r += sz;
		int res = 0;

		while (l < r)
		{
			if (l & 1)
				res += tree[l++];
			if (r & 1)
				res += tree[--r];
			l >>= 1;
			r >>= 1;
		}

		return res;
	}

	void add(int pos, int val)
	{
		pos += sz;
		tree[pos] += val;
		pos >>= 1;
		while (pos)
		{
			tree[pos] = tree[pos << 1] + tree[(pos << 1) + 1];
			pos >>= 1;
		}
	}
};

struct test
{
	int n, k;
	vector<pii> els;
	vector<int> vals, rval;
	segTreeSum tree;

	int check(int x)
	{
		int u = 0;
		ll cnt = 0;
		for (int i = 0; i < n; ++i)
		{
			while (els[u].f <= els[i].f + x and u < n)
				tree.add(rval[u++], 1);
			tree.add(rval[i], -1);
			cnt += tree.get(0, upper_bound(all(vals), els[i].s + x) - vals.begin()) - tree.get(0, lower_bound(all(vals), els[i].s - x) - vals.begin());
		}

		for (int i = 0; i < 2 * vals.size(); ++i)
			tree.tree[i] = 0;
		return cnt;
	}

	void solve(int testcase)
	{
		boost();

		cin >> n >> k;
		for (int i = 0; i < n; ++i)
		{
			int x, y;
			cin >> x >> y;
			els.pb({x + y, x - y});
			vals.pb(x - y);
		}
		vals.pb(-BIG);
		vals.pb(BIG);
		sort(all(vals));
		vals.erase(unique(all(vals)), vals.end());
		sort(all(els));

		rval.resize(n);
		for (int i = 0; i < n; ++i)
			rval[i] = lower_bound(all(vals), els[i].s) - vals.begin();
		tree.init(2 * n);

		int L = -1, R = 4e9 + 10;
		while (R - L > 1)
		{
			int M = (L + R) >> 1;
			if (check(M) < k)
				L = M;
			else
				R = M;
		}

		vector<int> res;
		{
			multiset<pii> cur;
			int u = 0, x = L;
			for (int i = 0; i < n; ++i)
			{
				while (els[u].f <= els[i].f + x and u < n)
				{
					cur.insert({els[u].s, els[u].f});
					++u;
				}
				cur.erase(cur.find({els[i].s, els[i].f}));
				auto it = cur.lower_bound({els[i].s - x, -BIG});
				while (it != cur.end() and abs(it->f - els[i].s) <= x)
				{
					res.pb(max(abs(it->f - els[i].s), abs(it->s - els[i].f)));
					it = next(it);
				}
			}
		}

		sort(all(res));
		for (auto el : res)
			cout << el << '\n';
		for (int i = 0; res.size() + i < k; ++i)
			cout << L + 1 << '\n';
	}
};

main()
{
	boost();
	int q = 1;
	// cin >> q;
	for (int i = 0; i < q; i++)
	{
		test t;
		t.solve(i);
	}
	return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message (stderr)

road_construction.cpp: In member function 'void segTreeSum::build(std::vector<long long int>&)':
road_construction.cpp:117:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |   for (int i = 0; i < a.size(); ++i)
      |                   ~~^~~~~~~~~~
road_construction.cpp: In member function 'long long int test::check(long long int)':
road_construction.cpp:175:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |   for (int i = 0; i < 2 * vals.size(); ++i)
      |                   ~~^~~~~~~~~~~~~~~~~
road_construction.cpp: In member function 'void test::solve(long long int)':
road_construction.cpp:237:34: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
  237 |   for (int i = 0; res.size() + i < k; ++i)
      |                   ~~~~~~~~~~~~~~~^~~
road_construction.cpp: At global scope:
road_construction.cpp:242:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  242 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...