跳到正文

题解

题解:AT_abc406_d [ABC406D] Garbage Removal

Gy456约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月19日
收录
2026年8月20日
更新
2026年8月20日
篇幅
约 2 分钟

版本

  1. v1当前

[ABC406D] Garbage Removal 题解

Problem

h×wh \times w 的网格上有 nn 个垃圾,给定所有垃圾的坐标,两种操作为输出某一行或某一列的垃圾总数并清空这些。

Solution

观察到有插入有删除,只需要维护数量,不难想到 set 大法。开 h+wh+w 个 set,分别维护每行对应的垃圾在第几列和每列对应的垃圾在第几行。

以一操作为例,查询的时候先输出当前集合的大小,然后遍历一遍这行所有垃圾,将这个垃圾对应的列删除掉这行,再把这行清空,反之亦然。

Code

C++
int h, w, q;
std::set<int> se1[maxn], se2[maxn];
inline void solve() {
	Fastio::cin >> h >> w >> n;
	for(int i = 1, x, y; i <= n; i++){
		Fastio::cin >> x >> y;
		se1[x].insert(y);
		se2[y].insert(x);
	}
	Fastio::cin >> q;
	while(q--){
	    int op, x;
	    Fastio::cin >> op >> x;
	    if(op == 1){
	    	int ans = se1[x].size();
			Fastio::cout << ans << '\n';
			for(int y : se1[x]) se2[y].erase(x);
			se1[x].clear();
	    } 
		else{
			int ans = se2[x].size();
			Fastio::cout << ans << '\n';
			for(int y : se2[x]) se1[y].erase(x);
			se2[x].clear();
	    }
  	}
}
原文