HDU 4941 Magical Forest STL

这明明就是给纯C选手的大杀器啊。

题意:给你k坐标,表示 X,Y 有值C,有 3种操作

1) 交换A,B两行

2) 交换A,B两列

3) 询问(A,B)的值

解题思路:map离散化

解题代码:

// File Name: 1007.cpp
// Author: darkdream
// Created Time: 2014年08月12日 星期二 21时05分18秒

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL long long

using namespace std;
map<int,map<int,int> > a;
map<int,int>  hashx , hashy;
const int maxn = 100005; 
int T , n , m ,k; 
struct node{
   int x, y ,c; 
}l[maxn];
int cmp(node t,node tt)
{
     return t.x < tt.x;
}
int cmp1(node t,node tt)
{
    return t.y < tt.y;
}
int main(){
    int T; 
    scanf("%d",&T);
    for(int ca = 1; ca <= T; ca ++)
    {
       hashx.clear(),hashy.clear(),a.clear();
       scanf("%d %d %d",&n,&m,&k);
       for(int i = 1;i <= k;i ++)
          scanf("%d %d %d",&l[i].x,&l[i].y,&l[i].c);
       sort(l+1,l+1+k,cmp);
       int mapx,mapy;
       mapx = mapy = 0; 
       for(int i = 1;i <= k;i ++)
           if(hashx.find(l[i].x) == hashx.end())
               hashx[l[i].x] = ++mapx;
       sort(l+1,l+1+k,cmp1);
       for(int i =1;i <= k ;i ++)
       {
          if(hashy.find(l[i].y) == hashy.end())
          {
              hashy[l[i].y] = ++ mapy;  
          }
          a[hashx[l[i].x]][hashy[l[i].y]] = l[i].c;
       }
       scanf("%d",&m);
       printf("Case #%d:
",ca);
       for(int i = 1;i <= m;i ++)
       {
          int Q,A,B;
          scanf("%d %d %d",&Q,&A,&B);
          if(Q ==  1){
              if(hashx.find(A) != hashx.end())
              {
                int temp = hashx[A];
                hashx[A] = hashx[B];
                hashx[B] = temp;
              }
          }else if( Q == 2){
              if(hashy.find(A) != hashy.end())
              {
                int temp = hashy[A];
                hashy[A] = hashy[B];
                hashy[B] = temp;
              }
          }else {
             if(hashx.find(A) != hashx.end() && hashy.find(B) != hashy.end())
             {
                printf("%d
",a[hashx[A]][hashy[B]]);
             }else printf("0
");
          }
       }
    }
    
    return 0;
}
View Code

 后来发现了其实不用离散化,改进后的代码如下:

 1 // File Name: 1007.cpp
 2 // Author: darkdream
 3 // Created Time: 2014年08月12日 星期二 21时05分18秒
 4 
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 
26 using namespace std;
27 map<int,map<int,int> > a;
28 map<int,int>  hashx , hashy;
29 const int maxn = 100005; 
30 int T , n , m ,k; 
31 struct node{
32    int x, y ,c; 
33 }l[maxn];
34 int main(){
35     int T; 
36     scanf("%d",&T);
37     for(int ca = 1; ca <= T; ca ++)
38     {
39        hashx.clear(),hashy.clear(),a.clear();
40        scanf("%d %d %d",&n,&m,&k);
41        for(int i = 1;i <= k;i ++){
42           scanf("%d %d %d",&l[i].x,&l[i].y,&l[i].c);
43        hashx[l[i].x] = l[i].x; 
44        hashy[l[i].y] = l[i].y;
45        a[l[i].x][l[i].y] = l[i].c;
46     }
47        scanf("%d",&m);
48        printf("Case #%d:
",ca);
49        for(int i = 1;i <= m;i ++)
50        {
51           int Q,A,B;
52           scanf("%d %d %d",&Q,&A,&B);
53           if(Q ==  1){
54               if(hashx.find(A) != hashx.end())
55               {
56                 int temp = hashx[A];
57                 hashx[A] = hashx[B];
58                 hashx[B] = temp;
59               }
60           }else if( Q == 2){
61               if(hashy.find(A) != hashy.end())
62               {
63                 int temp = hashy[A];
64                 hashy[A] = hashy[B];
65                 hashy[B] = temp;
66               }
67           }else {
68              if(hashx.find(A) != hashx.end() && hashy.find(B) != hashy.end())
69              {
70                 printf("%d
",a[hashx[A]][hashy[B]]);
71              }else printf("0
");
72           }
73        }
74     }
75     
76     return 0;
77 }
View Code
没有梦想,何谈远方
原文地址:https://www.cnblogs.com/zyue/p/3908607.html