Submission #912163

#TimeUsernameProblemLanguageResultExecution timeMemory
912163NonozeGarden (JOI23_garden)C++17
0 / 100
130 ms13132 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define sz(x) (int)(x.size())


int n, m, D;
vector<int> p, q;
vector<int> r, s;
vector<bool> xxx(5005, false), yyy(5005, false);

bool can(int i) {
	return (i>=0 && i<D+5);
}

void build(vector<int> &temp, multiset<int> &yy, set<int> qq) {
	for (auto u=qq.begin(); u!=qq.end(); u++) {
		auto v=++u;
		--u;
		if (v==qq.end()) {
			yy.insert(D-(*u-*qq.begin()+1));
			temp[D-(*u-*qq.begin()+1)]++;
		} else {
			yy.insert(*v-*u-1);
			temp[*v-*u-1]++;
		}
	}
}

int subtask5() {
	vector<pair<int, int>> x;
	set<int> y;
	for (int i=0; i<5005; i++) {
		int u=xxx[i];
		if (u) x.push_back({i, -1}), x.push_back({i+D, -1});
	}
	for (int i=0; i<5005; i++) {
		int u=yyy[i];
		if (u) y.insert(i);
	}
	vector<int> temp(D+5, 0);
	multiset<int> yy;
	build(temp, yy, y);
	int emply=D+4;;
	while (emply>0 && temp[emply]<=0) emply--;
	int ans=D*D;
	for (int i=0; i<m; i++) x.push_back({r[i], i}), x.push_back({r[i]+D, i});
	sort(x.begin(), x.end());
	for (int a=0; a<D; a++) {
		auto lsty=y;
		auto lsttemp=temp;
		auto lstemply=emply;
		int szy=D-emply;
		//cout << szy << endl;
		int empl=upper_bound(x.begin(), x.end(), make_pair(a+D, -2LL))-x.begin();
		while (empl==sz(x) || x[empl].first>=a+D) empl--;
		empl++;
		if (empl==sz(x) || x[empl].first==a+D) empl--;
		for (int i=0; i<m+1; i++) {
			ans=min(ans, (x[empl].first-a+1)*szy);\
			if (x[empl].second==-1) break;
			auto value=s[x[empl].second];
			auto tempy=y.lower_bound(value);
			if (tempy==y.end() || *tempy!=value) {
				if (tempy==y.end()) {
					auto prec=y.end(), next=y.begin();
					prec--;
					int act=D-(*prec-*next+1);
					if (can(act)) temp[act]--;
					if (can(D-(value-*next+1))) temp[D-(value-*next+1)]++;
					if (can(value-*prec-1)) temp[value-*prec-1]++;
					//yy.insert(D-(value-*next+1));
					//yy.insert(value-*prec-1);
				} else if (tempy==y.begin()) {
					auto prec=y.begin(), next=y.end();
					next--;
					int act=D-(*next-*prec+1);
					if (can(act)) temp[act]--;
					if (can(D-(*next-value+1))) temp[D-(*next-value+1)]++;
					if (can(value-*prec-1)) temp[value-*prec-1]++;
					//yy.insert(D-(*next-value+1));
					//yy.insert(value-*prec-1);
				} else {
					auto next=tempy, prec=--tempy;
					int act=*next-*prec-1;
					if (can(act)) temp[act]--;
					if (can(*next-value-1)) temp[*next-value-1]++;
					if (can(value-*prec-1)) temp[value-*prec-1]++;
					//yy.insert(*next-value-1);
					//yy.insert(value-*prec-1);
				}
				y.insert({value, -1});
			}
			while (empl>0 && temp[emply]<=0) emply--;
			szy=D-emply;
			empl--;
		}
		y=lsty;
		temp=lsttemp;
		emply=lstemply;
	}
	return ans;
}

int solve() {
	cin >> n >> m >> D;
	p.resize(n);
	q.resize(n);
	for (int i=0; i<n; i++) {
		cin >> p[i] >> q[i];
		xxx[p[i]]=true;
		yyy[q[i]]=true;
	}
	vector<pair<int, int>> temp;
	for (int i=0; i<m; i++) {
		int a, b; cin >> a >> b;
		if (xxx[a] || yyy[b]) continue;
		temp.push_back({a, b});
	}
	sort(temp.begin(), temp.end());
	m=sz(temp);
	r.clear(), s.clear();
	for (auto u: temp) r.push_back(u.first), s.push_back(u.second);
	return subtask5();
}


signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << solve() << endl;
	return 0;
}
#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...