hdu - 4022 Bombing(map)

题目链接:https://vjudge.net/problem/HDU-4022

  用两个map来模拟,一个x到y的映射,一个y到x的映射,关于怎么解决删除重复元素的问题,可以在每次删除一个映射的元素的时候,删除掉对应的另外一个映射的元素

#pragma G++ optimize(2)
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<stdio.h>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 0x3f3f3f3f3f3f3f;
const int maxn = 1e3 + 10;
map<int, multiset<int> > xbase; //x到y的映射
map<int, multiset<int> > ybase; //y到x的映射
typedef map<int, multiset<int> > ite;
int booms(ite &x, ite &y, int pos) { 
    for (multiset<int>::iterator it = x[pos].begin(); it != x[pos].end(); ++it)
        y[*it].erase(pos); //删除被炸弹炸掉的元素
    int res = x[pos].size(); //炸弹炸掉的基地数量
    x[pos].clear(); //模拟炸弹清空对应的行(列)
    return res;
}
int main(void) {
    //std::ios::sync_with_stdio(false);
    int n, m;
    while(~scanf("%d%d", &n, &m) && (n||m)) {
        for (int i = 0, x, y; i<n; ++i) {
            scanf("%d%d", &x, &y);
            xbase[x].insert(y);
            ybase[y].insert(x);
        }
        for (int i = 0, c, d; i<m; ++i) {
            scanf("%d%d", &c, &d);
            if (!c)
                printf("%d\n", booms(xbase, ybase, d));
            else 
                printf("%d\n", booms(ybase, xbase, d));
        }
        xbase.clear();
        ybase.clear();
        putchar(endl);
    }
    return 0;
}
posted @ 2020-01-18 22:14  shuitiangong  阅读(156)  评论(0编辑  收藏  举报