跳到正文

题解

题解:AT_abc449_f [ABC449F] Grid Clipping

JonnynbZLY约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年4月16日
更新
2026年4月16日
篇幅
约 6 分钟

版本

  1. v1当前

官方题解:原网址 原题面

F - Grid Clipping Editorial by en_translator

Consider the pairs (r0,c0)(r_0, c_0) such that:

  • 1≤r0≤H−h+11 \leq r_0 \leq H - h + 1
  • 1≤c0≤W−w+11 \leq c_0 \leq W - w + 1
  • There exists (i,j)(i, j) with 0≤i<H,0≤j<W0 \leq i < H, 0 \leq j < W such that cell (r0+i,c0+j)(r_0 + i, c_0 + j) is painted black.

Let XX be the number of such pairs (r0,c0)(r_0, c_0). Then the sought answer is (H−h+1)(W−w+1)−X(H - h + 1)(W - w + 1) - X. From now on, we will consider how to find this XX.

When there is a cell (Rk,Ck)(R_k, C_k) painted black, cells (r0,c0)∈[Rk−h+1,Rk]×[Ck−w+1,Ck](r_0, c_0) \in [R_k - h + 1, R_k] \times [C_k - w + 1, C_k] satisfy the condition due to that black cell. Thus, it suffices to find the size of the union of these rectangular regions.

There are various ways to find the size of this union; here we will introduce a sweep line algorithm using a multiset>

When we sweep a scan-line extending in the r-axis direction, black cells appear within (Ck−w+1,Ck](C_k - w + 1, C_k] at r=Rk−h+1r = R_k - h + 1, and disappear at r=Rk+1r = R_k + 1. Thus, the size can be computed fast by maintaining the segments of black cells within the current sweep line in a multiset or a map, and accumulating the delta formed by those segments.

The problem can be answered correctly by appropriately implementing the algorithm. The complexity is O(Nlog⁡N)O(N \log N).

Sample code (C++)

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	long H, W, h, w, n;
	cin >> H >> W >> h >> w >> n;
	vector<tuple<long, long, bool>> g;
	g.reserve(2 * n);
	for (int i = 0; i < n; i++) {
		long r, c;
		cin >> r >> c;
		r--, c--;
		g.push_back({r - h + 1, c - w + 1, true});
		g.push_back({r + 1, c - w + 1, false});
	}
	sort(g.begin(), g.end());
	multiset<long> s;
	s.insert(-w);
	s.insert(W - w + 1);
	long ans = (H - h + 1) * (W - w + 1);
	auto gap = [&](long l, long r) { return max(0L, r - l - w); };
	const long Y = W - w + 1;
	const long X = H - h + 1;
	long good = Y;
	auto add = [&](long x) {
		auto it = s.lower_bound(x);
		long r = *it, l = *prev(it);
		good += gap(l, x) + gap(x, r) - gap(l, r);
		s.insert(x);
	};
	auto del = [&](long x) {
		auto it = s.find(x);
		long r = *next(it), l = *prev(it);
		good -= gap(l, x) + gap(x, r) - gap(l, r);
		s.erase(it);
	};
	long pre = 0;
	int i = 0;
	while (i < 2 * n) {
		const long x = get<0>(g[i]);
		const long nx = min(x, X);
		if (pre < nx) {
			ans -= (nx - pre) * (Y - good);
			pre = nx;
		}
		while (i < 2 * n && get<0>(g[i]) == x) {
			auto [r, c, in] = g[i];
			if (in) add(c);
			else del(c);
			i++;
		}
	}
	if (pre < X) ans -= (X - pre) * (Y - good);
	cout << ans << endl;
}
原文