Submission #956167

# Submission time Handle Problem Language Result Execution time Memory
956167 2024-04-01T08:37:32 Z vjudge1 Road Construction (JOI21_road_construction) C++17
100 / 100
3901 ms 27180 KB
// #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

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 time Memory Grader output
1 Correct 37 ms 5052 KB Output is correct
2 Correct 38 ms 5056 KB Output is correct
3 Correct 34 ms 5308 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Correct 33 ms 4044 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1801 ms 23124 KB Output is correct
2 Correct 1819 ms 23336 KB Output is correct
3 Correct 31 ms 5060 KB Output is correct
4 Correct 1924 ms 22860 KB Output is correct
5 Correct 2045 ms 23108 KB Output is correct
6 Correct 2063 ms 23204 KB Output is correct
7 Correct 1684 ms 22460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3590 ms 19024 KB Output is correct
2 Correct 3663 ms 21152 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 2003 ms 19112 KB Output is correct
5 Correct 1484 ms 21304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3590 ms 19024 KB Output is correct
2 Correct 3663 ms 21152 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 2003 ms 19112 KB Output is correct
5 Correct 1484 ms 21304 KB Output is correct
6 Correct 3655 ms 21288 KB Output is correct
7 Correct 3673 ms 21292 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 3633 ms 21312 KB Output is correct
11 Correct 2062 ms 19140 KB Output is correct
12 Correct 1480 ms 21616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 5052 KB Output is correct
2 Correct 38 ms 5056 KB Output is correct
3 Correct 34 ms 5308 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Correct 33 ms 4044 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 1371 ms 14684 KB Output is correct
8 Correct 1358 ms 14660 KB Output is correct
9 Correct 33 ms 5308 KB Output is correct
10 Correct 1283 ms 14112 KB Output is correct
11 Correct 1229 ms 13892 KB Output is correct
12 Correct 386 ms 10768 KB Output is correct
13 Correct 412 ms 13452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 5052 KB Output is correct
2 Correct 38 ms 5056 KB Output is correct
3 Correct 34 ms 5308 KB Output is correct
4 Correct 35 ms 5312 KB Output is correct
5 Correct 33 ms 4044 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 1801 ms 23124 KB Output is correct
8 Correct 1819 ms 23336 KB Output is correct
9 Correct 31 ms 5060 KB Output is correct
10 Correct 1924 ms 22860 KB Output is correct
11 Correct 2045 ms 23108 KB Output is correct
12 Correct 2063 ms 23204 KB Output is correct
13 Correct 1684 ms 22460 KB Output is correct
14 Correct 3590 ms 19024 KB Output is correct
15 Correct 3663 ms 21152 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 2003 ms 19112 KB Output is correct
18 Correct 1484 ms 21304 KB Output is correct
19 Correct 3655 ms 21288 KB Output is correct
20 Correct 3673 ms 21292 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 3633 ms 21312 KB Output is correct
24 Correct 2062 ms 19140 KB Output is correct
25 Correct 1480 ms 21616 KB Output is correct
26 Correct 1371 ms 14684 KB Output is correct
27 Correct 1358 ms 14660 KB Output is correct
28 Correct 33 ms 5308 KB Output is correct
29 Correct 1283 ms 14112 KB Output is correct
30 Correct 1229 ms 13892 KB Output is correct
31 Correct 386 ms 10768 KB Output is correct
32 Correct 412 ms 13452 KB Output is correct
33 Correct 3901 ms 27180 KB Output is correct
34 Correct 3883 ms 27180 KB Output is correct
35 Correct 3569 ms 26304 KB Output is correct
36 Correct 1288 ms 23124 KB Output is correct
37 Correct 1314 ms 23084 KB Output is correct
38 Correct 1372 ms 25888 KB Output is correct