hdu 4941 Magical Forest
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4941
Magical Forest
Description
There is a forest can be seen as $N * M$ grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location$(X_i, Y_i)$ and the energy can be provided $C_i$.
However, the forest will make the following change sometimes:
1. Two rows of forest exchange.
2. Two columns of forest exchange.
Fortunately, two rows(columns) can exchange only if both of them contain fruits or none of them contain fruits.
Your superior attach importance to these magical fruit, he needs to know this forest information at any time, and you as his best programmer, you need to write a program in order to ask his answers quick every time.
Input
The input consists of multiple test cases.
The first line has one integer $W.$ Indicates the case number.$(1 \leq W \leq 5)$
For each case, the first line has three integers $N, M, K.$ Indicates that the forest can be seen as maps $N$ rows, $M$ columns, there are $K$ fruits on the map.$(1 \leq N, M \leq 2*10^9,\ 0 \leq K \leq 10^5)$
The next $K$ lines, each line has three integers $X, Y, C,$ indicates that there is a fruit with $C$ energy in $X$ row, $Y$ column. $(0 \leq X \leq N-1,\ 0 \leq Y \leq M-1,\ 1 \leq C \leq 1000)$
The next line has one integer $T.$ $(0 \leq T \leq 10^5)$
The next T lines, each line has three integers $Q, A, B.$
If $Q = 1$ indicates that this is a map of the row switching operation, the $A$ row and $B$ row exchange.
If $Q = 2$ indicates that this is a map of the column switching operation, the $A$ column and $B$ column exchange.
If $Q = 3$ means that it is time to ask your boss for the map, asked about the situation in $(A, B)$.
(Ensure that all given $A, B$ are legal. )
Output
For each case, you should output "Case #C:" first, where C indicates the case number and counts from 1.
In each case, for every time the boss asked, output an integer X, if asked point have fruit, then the output is the energy of the fruit, otherwise the output is 0.
Sample Input
1
3 3 2
1 1 1
2 2 2
5
3 1 1
1 1 2
2 1 2
3 1 1
3 2 2
Sample Output
Case #1:
1
2
1
map映射。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::swap; 13 using std::sort; 14 using std::set; 15 using std::map; 16 using std::pair; 17 using std::vector; 18 using std::multiset; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int Max_N = 100010; 29 typedef unsigned long long ull; 30 struct Node { 31 int x, y, val; 32 Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), val(k) {} 33 inline bool operator<(const Node &a) const { 34 return x == a.x ? y > a.y : x > a.x; 35 } 36 }; 37 set<Node> st; 38 set<Node>::iterator ite; 39 map<int, int> x, y; 40 int main() { 41 #ifdef LOCAL 42 freopen("in.txt", "r", stdin); 43 freopen("out.txt", "w+", stdout); 44 #endif 45 int t, n, m, a, b, v, q, k, p = 1; 46 scanf("%d", &t); 47 while (t--) { 48 st.clear(), x.clear(), y.clear(); 49 scanf("%d %d %d", &n, &m, &k); 50 while (k--) { 51 scanf("%d %d %d", &a, &b, &v); 52 if (x.find(a) == x.end()) x[a] = a; 53 if (y.find(b) == y.end()) y[b] = b; 54 st.insert(Node(a, b, v)); 55 } 56 printf("Case #%d:\n", p++); 57 scanf("%d", &k); 58 while (k--) { 59 scanf("%d %d %d", &q, &a, &b); 60 if (1 == q) { 61 if (x.find(a) == x.end()) x[a] = a; 62 if (x.find(b) == x.end()) x[b] = b; 63 swap(x[a], x[b]); 64 } else if (2 == q) { 65 if (y.find(a) == y.end()) y[a] = a; 66 if (y.find(b) == y.end()) y[b] = b; 67 swap(y[a], y[b]); 68 } else { 69 ite = st.find(Node(x[a], y[b])); 70 printf("%d\n", ite == st.end() ? 0 : ite->val); 71 } 72 } 73 } 74 return 0; 75 }