Submission #912456

#TimeUsernameProblemLanguageResultExecution timeMemory
912456NonozeGarden (JOI23_garden)C++17
6 / 100
259 ms4824 KiB
#include <bits/stdc++.h>
using namespace std;

#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<2*D+5);
}

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

int subtask5() {
	vector<vector<int>> x(2*D+5);
	vector<vector<int>> correspond(D+5);
	set<int> y;
	for (int i=0; i<5005; i++) {
		int u=yyy[i];
		if (u) y.insert(i);
	}
	vector<int> yy(2*D+5, 0);
	build(yy, y);
	int emply=2*D+4;
	while (emply>0 && yy[emply]<=0) emply--;
	int ans=D*D;
	for (int i=0; i<m; i++) {
		x[r[i]].push_back(s[i]), x[r[i]+D].push_back(s[i]);
		if (!yyy[s[i]]) correspond[s[i]].push_back(r[i]), correspond[s[i]].push_back(r[i]+D);
	}

	for (auto &u: x) sort(u.begin(), u.end());
	for (auto &u: correspond) sort(u.begin(), u.end());

	vector<int> emplacement(D+5, 0);
	auto basey=y;
	auto baseyy=yy;
	auto baseemply=emply;
	for (int a=0; a<D; a++) {
		int szy=D-emply;
		int empl=a+D-1;
		vector<pair<int, int>> liste;
		for (int i=0; i<D; i++) {
			if (xxx[i]) {
				int val=i;
				if (i<a) val+=D;
				liste.push_back({val, -1});
			}
			while (emplacement[i]+1<sz(correspond[i]) && correspond[i][emplacement[i]+1]<a+D) emplacement[i]++;
			if (emplacement[i]>=sz(correspond[i]) || correspond[i][emplacement[i]]<a) continue;
			liste.push_back({correspond[i][emplacement[i]], i});
		}
		sort(liste.rbegin(), liste.rend());
		for (int emplliste=0; emplliste<sz(liste); emplliste++) {
			ans=min(ans, (liste[emplliste].first-a+1)*szy);
			if (xxx[liste[emplliste].first%D]) break;
			int value=liste[emplliste].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)) yy[act]--;
					if (can(D-(value-*next+1))) yy[D-(value-*next+1)]++;
					if (can(value-*prec-1)) yy[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)) yy[act]--;
					if (can(D-(*next-value+1))) yy[D-(*next-value+1)]++;
					if (can(value-*prec-1)) yy[value-*prec-1]++;
				} else {
					auto next=tempy, prec=--tempy;
					int act=*next-*prec-1;
					if (can(act)) yy[act]--;
					if (can(*next-value-1)) yy[*next-value-1]++;
					if (can(value-*prec-1)) yy[value-*prec-1]++;
				}
				y.insert(value);
			}
			while (emply>0 && yy[emply]<=0) emply--;
			szy=D-emply;
		}
		//y=basey;
		//yy=baseyy;
		emply=baseemply;
	}
	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;
	map<pair<int, int>, bool> mp;
	for (int i=0; i<m; i++) {
		int a, b; cin >> a >> b;
		if (xxx[a] || yyy[b] || mp.count({a, b})) continue;
		temp.push_back({a, b});
		mp[{a, b}]=true;
	}
	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;
}

Compilation message (stderr)

garden.cpp: In function 'int subtask5()':
garden.cpp:55:7: warning: unused variable 'empl' [-Wunused-variable]
   55 |   int empl=a+D-1;
      |       ^~~~
#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...