Submission #736211

# Submission time Handle Problem Language Result Execution time Memory
736211 2023-05-05T10:12:53 Z QwertyPi Road Construction (JOI21_road_construction) C++14
0 / 100
10000 ms 221972 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2.5e5 + 11;
const int INF = (1LL << 60);
const int SINF = (1LL << 32);

struct point{
	int a, b;
};

vector<point> P;

int cx;
set<int> sx;
map<int, int> mx;

int d(point x, point y){
	return max(abs(x.a - y.a), abs(x.b - y.b));
}

struct BIT2{
	vector<int> Sp[N];
	vector<int> Bi[N];
	void build(const vector<point>& P){
		for(auto p : P){
			int x = mx[p.a], y = p.b;
			for(int i = x; i < N; i += i & -i){
				Sp[i].push_back(y);
			}
		}
		for(int i = 0; i < N; i++){
			Sp[i].push_back(-INF);
			Sp[i].push_back(INF);
			sort(Sp[i].begin(), Sp[i].end());
			Sp[i].resize(unique(Sp[i].begin(), Sp[i].end()) - Sp[i].begin());
		}
		for(int i = 0; i < N; i++){
			Bi[i].resize(Sp[i].size());
		}
	}
	
	void add(int a, int b, int v){
		int x = (mx.lower_bound(a))->second;
		for(int i = x; i < N; i += i & -i){
			int y = lower_bound(Sp[i].begin(), Sp[i].end(), b) - Sp[i].begin();
			for(int j = y; j < Sp[i].size(); j += j & -j){
				Bi[i][j] += v;
			}
		}
	}
	
	int qry(int a, int b){
		int res = 0;
		int x = (--mx.upper_bound(a))->second;
		for(int i = x; i; i -= i & -i){
			int y = (--upper_bound(Sp[i].begin(), Sp[i].end(), b)) - Sp[i].begin();
			for(int j = y; j; j -= j & -j){
				res += Bi[i][j];
			}
		}
		return res;
	}
	
	int search(int a, int b, int r){
		return qry(a + r, b + r)
			-  qry(a - r - 1, b + r)
			-  qry(a + r, b - r - 1)
			+  qry(a - r - 1, b - r - 1);
	}
} bit2;

int u[N];

unordered_map<int, int> stored_result;

const int A1 = 13819, P1 = 1e9 + 7, A2 = 42589, P2 = 998244353; 

int norm(int x, int p){
	return (x % p + p) % p;
}

int h(int x, int y, int z){
	return // ((norm(x % P1 * A1 % P1 * A1 % P1 + y * A1 + z, P1)) << 32)
		+  (norm(x % P2 * A2 % P2 * A2 % P2 + y * A2 + z, P2));
}

int query(int a, int b, int r){
	int _h = h(a, b, r);
	if(stored_result.count(_h)){
		return stored_result[_h];
	}
	return stored_result[_h] = bit2.search(a, b, r);
}

int32_t main(){
	int n, k; cin >> n >> k;
	for(int i = 0; i < n; i++){
		int x, y; cin >> x >> y;
		P.push_back({x + y, x - y});
	}
	
	
	for(int i = 0; i < n; i++){
		sx.insert(P[i].a);
	}
	
	mx[-INF] = cx++; for(auto i : sx) mx[i] = cx++; mx[INF] = cx++;
	
	bit2.build(P);
	for(int i = 0; i < n; i++){
		bit2.add(P[i].a, P[i].b, 1);
	}
	
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	
	fill(u, u + N, 1);
	
	auto find_next = [&](int i){
		int l = 0, r = SINF;
		while(l != r){
			int mid = (l + r) / 2;
			int s = query(P[i].a, P[i].b, mid);
			if(s > u[i]){
				r = mid;
			}else{
				l = mid + 1;
			}
		}
		pq.push({l, i});
	};
	
	for(int i = 0; i < n; i++) find_next(i);
	
	while(k){
		auto [a1, i1] = pq.top(); pq.pop();
		u[i1]++; find_next(i1);
		auto [a2, i2] = pq.top(); pq.pop();
		u[i2]++; find_next(i2);
		assert(a1 == a2);
		cout << a1 << endl;
		k--;
	}
}

Compilation message

road_construction.cpp: In member function 'void BIT2::add(long long int, long long int, long long int)':
road_construction.cpp:48: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]
   48 |    for(int j = y; j < Sp[i].size(); j += j & -j){
      |                   ~~^~~~~~~~~~~~~~
road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:137:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  137 |   auto [a1, i1] = pq.top(); pq.pop();
      |        ^
road_construction.cpp:139:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  139 |   auto [a2, i2] = pq.top(); pq.pop();
      |        ^
# Verdict Execution time Memory Grader output
1 Runtime error 106 ms 66012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10110 ms 221972 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10088 ms 197160 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 10088 ms 197160 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 106 ms 66012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 106 ms 66012 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -