跳到正文

题解

AT_abc406_d [ABC406D] Garbage Removal

yy2014约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

AT_abc406_d [ABC406D] Garbage Removal题解

题目传送门

思路:

其实就是先将(Xi,Yi)(X_{i},Y_{i})标记为有垃圾,然后再根据题目要求去删除垃圾

map:

C++中的map容器是STL提供的关联式容器,它以键值对(key-value)的形式存储数据,具有以下特点:

有序性:基于红黑树实现,元素按照键(key)自动排序

唯一性:每个键只能出现一次

高效查找:查找时间复杂度为O(log n)

基本用法示例:

C++
#include <iostream>
#include <map>
using namespace std;

int main() {
    // 创建map
    map<string, int> ageMap;

    // 插入元素
    ageMap["Alice"] = 25;
    ageMap.insert({"Bob", 30});

    // 遍历
    for(auto& pair : ageMap) {
        cout << pair.first << ": " << pair.second << endl;
    }

    // 查找
    if(ageMap.find("Alice") != ageMap.end()) {
        cout << "Found Alice" << endl;
    }

    return 0;
}

map常用于需要快速查找且需要保持元素有序的场景,如字典、配置项存储等。注意键必须是可比较的类型

那为什么要用到map呢?

再来看看题目:

  • 类型 11:以 1 x 的格式给出。求出网格第 xx 行掉落的垃圾数量作为答案。之后,第 xx 行掉落的所有垃圾都被丢弃,并从网格上移除。
  • 类型 22:以 2 y 的格式给出。求出网格第 yy 列掉落的垃圾数量作为答案。之后,第 yy 列掉落的所有垃圾都被丢弃,并从网格上移除。

那这不正好,建两个文明map数组,一个表示行,一个表示列

但是,怎么去删呢?

这不简单,删行就把对应的列删了,删列就把对应的行删了

代码

需注意一下细节的处理

C++
#include <bits/stdc++.h>
using namespace std ;
map <int , int> h[200005] ;
map <int , int> l[200005] ;
int a , b , n , q , x , y ;
int main()
{
	cin >> a >> b >> n ;
	for(int i = 1 ; i <= n ; i ++)
	{
		cin >> x >> y ;
		h[x][y] = 1 ;
		l[y][x] = 1 ;
	}
	cin >> q ;
	while(q --)
	{
		int op ;
		cin >> op ;
		if(op == 1)
		{
			int x ;
			cin >> x ;
			for(auto i : h[x])
			{
				l[i.first].erase(x) ;
			}
			cout << h[x].size() << "\n" ;
			h[x].clear() ;
		}
		if(op == 2)
		{
			int x ;
			cin >> x ;
			for(auto i : l[x])
			{
				h[i.first].erase(x) ;
			}
			cout << l[x].size() << "\n" ;
			l[x].clear() ;
		}
	}
	return 0 ;
}
原文