제출 #965281

#제출 시각아이디문제언어결과실행 시간메모리
965281Gromp15Garden (JOI23_garden)C++17
30 / 100
3044 ms16976 KiB
#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int INF = 1e9;
void test_case() {
	int n, m, D;
	cin >> n >> m >> D;
	vector<ar<int, 2>> a(n), b(m);
	for (auto &x : a) cin >> x[0] >> x[1];
	for (auto &x : b) cin >> x[0] >> x[1];
	vector<vector<ar<int, 3>>> each(D);
	for (int i = 0; i < n; i++) {
		each[a[i][0]].push_back({a[i][1], i, 0});
	}
	for (int i = 0; i < m; i++) {
		each[b[i][0]].push_back({-1, i + n, 1});
	}
	int ans2 = 1e9;
	for (int i = 0; i < D; i++) {
		auto calc = [&](int top) {
			int l = i, r = top;
			// use [l, r] y
			vector<int> got(n + m);
			int need = n + m;
			for (int i = 0; i < m; i++) {
				// b[1] is y
				if ((l <= b[i][1] && b[i][1] <= r) || (l <= b[i][1] + D && b[i][1] + D <= r)) {
					got[i + n]++;
					need--;
				}
			}
			int left = 0, bst = INF;
			for (int j = 0; j < 2 * D; j++) {
				for (auto [y, pos, tp] : each[j % D]) {
					if (tp == 1) {
						if (got[pos]++ == 0) need--;
					}
					else {
						// [l,r ] need to enclose y
						if ((l <= y && y <= r) || (l <= y+D && y+D <= r)) {
							if (got[pos]++ == 0) need--;
						}
					}
				}
				bool ok = need == 0;
				int last_good = left;
				while (need == 0) {
					last_good = left;
					for (auto [y, pos, tp] : each[left % D]) {
						if (tp == 1) {
							if (--got[pos] == 0) need++;
						}
						else {
							// [l,r ] need to enclose y
							if ((l <= y && y <= r) || (l <= y+D && y+D <= r)) {
								if (--got[pos] == 0) need++;
							}
						}
					}
					left++;
				}
				if (ok) ckmin(bst, j - last_good + 1);
			}
			return bst == INF ? -1 : bst;
		};
		for (int j = i; j < i + D; j++) {
			int res = calc(j);
			//cout << "I: " << i << " " << j << " " << res << '\n';
			if (~res) ckmin(ans2, (j - i + 1) * res);
		}
	}
	cout << ans2 << '\n';



}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
//    cin >> t;
    while (t--) test_case();
}

#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...