[ABC406D] Garbage Removal 题解
Problem
的网格上有 个垃圾,给定所有垃圾的坐标,两种操作为输出某一行或某一列的垃圾总数并清空这些。
Solution
观察到有插入有删除,只需要维护数量,不难想到 set 大法。开 个 set,分别维护每行对应的垃圾在第几列和每列对应的垃圾在第几行。
以一操作为例,查询的时候先输出当前集合的大小,然后遍历一遍这行所有垃圾,将这个垃圾对应的列删除掉这行,再把这行清空,反之亦然。
Code
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();
}
}
}