hdu - 4022 Bombing

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

题目描述:

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.

InputMultiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 10 9, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.
OutputFor each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.Sample Input

3 2
1 2
1 3
2 3
0 1
1 3
0 0

Sample Output

2
1

用两个map来模拟,一个x到y的映射,一个y到x的映射,关于怎么解决删除重复元素的问题,可以在每次删除一个映射的元素的时候,删除掉对应的另外一个映射的元素
#pragma G++ optimize(2)
#include<set>
#include
#include
#include
#include
#include
#include
#include<string>
#include
#include
#include
#include
#include
#include
#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; ii) {
            scanf("%d%d", &x, &y);
            xbase[x].insert(y);
            ybase[y].insert(x);
        }
        for (int i = 0, c, d; ii) {
            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;
}

你可能感兴趣的:(hdu - 4022 Bombing)