[HDOJ4022]Bombing(离散化+stl)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4022

  一个图上有n个点,之后m个操作,每次操作一行或者一列。使得这一行或者这一列的点全部消除。每次操作输出每次消除的点的个数。

思路:

  因为数据范围很大,刚开始想的是离散化后维护各行各列的点数,但是发现这样离线的做法只能维护当前状态,更新成了一个难题。后来在思考有没有一个方法,可以在不超过数据范围的情况下,在O(lgn)的限制内维护所有线上的坐标。想来想去,一开始想用map<int, vector<int>>的,但是vector扫描是O(n)的。那就要考虑一个非线性结构,最后想到了红黑树。还是保存重复元素的那个——std::multiset<int>。

  所以这道题就变成了一道水题了。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <fstream>
  8 #include <cassert>
  9 #include <cstdio>
 10 #include <bitset>
 11 #include <vector>
 12 #include <deque>
 13 #include <queue>
 14 #include <stack>
 15 #include <ctime>
 16 #include <set>
 17 #include <map>
 18 #include <cmath>
 19 
 20 using namespace std;
 21 
 22 #define fr first
 23 #define sc second
 24 #define pb(a) push_back(a)
 25 #define Rint(a) scanf("%d", &a)
 26 #define Rll(a) scanf("%I64d", &a)
 27 #define Rs(a) scanf("%s", a)
 28 #define FRead() freopen("in", "r", stdin)
 29 #define FWrite() freopen("out", "w", stdout)
 30 #define Rep(i, n) for(int i = 0; i < (n); i++)
 31 #define For(i, a, n) for(int i = (a); i < (n); i++)
 32 #define Cls(a) memset((a), 0, sizeof(a))
 33 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 34 
 35 const int maxn = 100010;
 36 int n, m;
 37 int x[maxn], y[maxn];
 38 int hx[maxn], hxcnt;
 39 int hy[maxn], hycnt;
 40 int sx[maxn], sy[maxn];
 41 map<int, multiset<int> > xx;
 42 map<int, multiset<int> > yy;
 43 multiset<int>::iterator it;
 44 
 45 inline bool scan_d(int &num) {
 46     char in;bool IsN=false;
 47     in=getchar();
 48     if(in==EOF) return false;
 49     while(in!='-'&&(in<'0'||in>'9')) in=getchar();
 50     if(in=='-'){ IsN=true;num=0;}
 51     else num=in-'0';
 52     while(in=getchar(),in>='0'&&in<='9'){
 53             num*=10,num+=in-'0';
 54     }
 55     if(IsN) num=-num;
 56     return true;
 57 }
 58 
 59 int getid(int* h, int hcnt, int x) {
 60     return lower_bound(h, h+hcnt, x) - h;
 61 }
 62 
 63 int main() {
 64     // FRead();
 65     int c, d;
 66     while(~scanf("%d%d", &n, &m) && n + m) {
 67         Cls(sx); Cls(sy); xx.clear(), yy.clear();
 68         Rep(i, n) {
 69             scan_d(x[i]); scan_d(y[i]);
 70             hx[i] = x[i]; hy[i] = y[i];
 71             xx[x[i]].insert(y[i]);
 72             yy[y[i]].insert(x[i]);
 73         }
 74         sort(hx, hx+n); sort(hy, hy+n);
 75         hxcnt = unique(hx, hx+n) - hx;
 76         hycnt = unique(hy, hy+n) - hy;
 77         Rep(i, n) {
 78             sx[getid(hx, hxcnt, x[i])]++;
 79             sy[getid(hy, hycnt, y[i])]++;
 80         }
 81         Rep(i, m) {
 82             scan_d(c); scan_d(d);
 83             if(c == 0) {
 84                 printf("%d\n", xx[d].size());
 85                 for(it = xx[d].begin(); 
 86                     it != xx[d].end(); it++) {
 87                     yy[*it].erase(d);
 88                 }
 89                 xx[d].clear();
 90                 sx[getid(hx, hxcnt, d)] = 0;
 91             }
 92             else {
 93                 printf("%d\n", yy[d].size());
 94                 for(it = yy[d].begin(); 
 95                     it != yy[d].end(); it++) {
 96                     xx[*it].erase(d);
 97                 }
 98                 sy[getid(hy, hycnt, d)] = 0;
 99                 yy[d].clear();
100             }
101         }
102         printf("\n");
103 
104     }
105     return 0;
106 }

 

posted @ 2016-05-12 19:44  Kirai  阅读(191)  评论(0编辑  收藏  举报