[HDOJ4022]Bombing(離散化+stl)

來源:互聯網
上載者:User

標籤:

題目連結: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 }

 

[HDOJ4022]Bombing(離散化+stl)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.