HDU 4941 Magical Forest

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

這個題比賽的時候是亂搞的,比賽結束之後學長說是映射+hash才恍然大悟。因此決定好好學一下hash。

題意:

  M*N的格子,裡面有一些格子裡面有一個值。

  有三種操作:

    1.交換兩行的值。

    2.交換兩列的值。

    3.詢問某個格子的值。

  保證,交換的時候要麼兩行都有值,要麼兩行都為空白。

 

 思路:

  其實交換兩行或者兩列的話,相當於把那兩行的對應下標交換一下。因此我們可以想到,對行標和列表建立一個索引,交換的時候,我們只需要交換一下索引。

  因為行和列比較多,所以我們不能直接存整個矩陣,即使離散化也不可以,所以這裡我們需要用到hash。(Map也可以)。

 

代碼:

  

  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cstdlib>  5 #include <cmath>  6 #include <algorithm>  7 #include <string>  8 #include <queue>  9 #include <stack> 10 #include <vector> 11 #include <map> 12 #include <set> 13 #include <functional> 14 #include <time.h> 15  16 using namespace std; 17  18 typedef __int64 ll; 19  20 const int INF = 1<<30; 21 const int MAXN = (int) 1e5+50; 22 const ll HASH = (ll) (1e6)+3; 23  24 struct node { 25     int x, y, e; 26 }a[MAXN]; 27  28 int hash_table[HASH], next[MAXN]; 29  30 int d_x[MAXN], d_y[MAXN], len_x, len_y; 31 int xx[MAXN], yy[MAXN]; 32 int n; 33 ll mx, my; 34  35 inline void add(int x, int y, int k) { 36     int id = ((ll)x*n+y)%HASH; 37     next[k] = hash_table[id]; 38     hash_table[id] = k; 39 } 40  41 inline int find(int x, int y) { 42     int id = ((ll)x*n+y)%HASH; 43     int p; 44     for (p = hash_table[id]; p!=-1; p = next[p]) 45         if (a[p].x==x&&a[p].y==y) 46             return a[p].e; 47     return 0; 48 } 49  50 void solve() { 51     memset(hash_table, -1, sizeof(hash_table)); 52     scanf("%d%d%d", &mx, &my, &n); 53     for (int i = 0; i < n; i++) { 54         scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].e); 55         d_x[i] = a[i].x; 56         d_y[i] = a[i].y; 57         xx[i] = yy[i] = i; 58     } 59     sort(d_x, d_x+n); 60     len_x = unique(d_x, d_x+n) - d_x; 61     sort(d_y, d_y+n); 62     len_y = unique(d_y, d_y+n) - d_y; 63     for (int i = 0; i < n; i++) { 64         a[i].x = lower_bound(d_x, d_x+len_x, a[i].x)-d_x; 65         a[i].y = lower_bound(d_y, d_y+len_y, a[i].y)-d_y; 66         add(a[i].x, a[i].y, i); 67     } 68  69     int Q, o, x, y; 70     int rx, ry; 71     scanf("%d", &Q); 72     while (Q--) { 73         scanf("%d%d%d", &o, &x, &y); 74         if (o==1) { 75             rx = lower_bound(d_x, d_x+len_x, x) - d_x; 76             ry = lower_bound(d_x, d_x+len_x, y) - d_x; 77             if (x==d_x[rx]&&y==d_x[ry]) swap(xx[rx], xx[ry]); 78         } else if (o==2) { 79             rx = lower_bound(d_y, d_y+len_y, x) - d_y; 80             ry = lower_bound(d_y, d_y+len_y, y) - d_y; 81             if (x==d_y[rx]&&y==d_y[ry]) swap(yy[rx], yy[ry]); 82         } else { 83             rx = lower_bound(d_x, d_x+len_x, x) - d_x; 84             ry = lower_bound(d_y, d_y+len_y, y) - d_y; 85             if (x==d_x[rx]&&y==d_y[ry]) printf("%d\n", find(xx[rx], yy[ry])); 86             else puts("-1"); 87         } 88     } 89 } 90  91 int main() { 92     #ifdef Phantom01 93         freopen("HDU4941.in", "r", stdin); 94         freopen("my4941.txt", "w", stdout); 95     #endif //Phantom01 96  97     int T; 98     scanf("%d", &T); 99     for (int i = 1; i <= T; i++) {100         printf("Case #%d:\n", i);101         solve();102     }103 104     return 0;105 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.