AT_abc406_d [ABC406D] Garbage Removal

· · 题解

AT_abc406_d [ABC406D] Garbage Removal题解

题目传送门

思路:

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

map:

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

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

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

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

基本用法示例:

#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呢?

再来看看题目:

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

但是,怎么去删呢?

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

代码

需注意一下细节的处理

#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 ;
}