hdu 4941 Magical Forest

时间:2021-09-19 12:41:08

题目连接

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映射。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::swap;
using std::sort;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int x, y, val;
Node(int i = , int j = , int k = ) :x(i), y(j), val(k) {}
inline bool operator<(const Node &a) const {
return x == a.x ? y > a.y : x > a.x;
}
};
set<Node> st;
set<Node>::iterator ite;
map<int, int> x, y;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, m, a, b, v, q, k, p = ;
scanf("%d", &t);
while (t--) {
st.clear(), x.clear(), y.clear();
scanf("%d %d %d", &n, &m, &k);
while (k--) {
scanf("%d %d %d", &a, &b, &v);
if (x.find(a) == x.end()) x[a] = a;
if (y.find(b) == y.end()) y[b] = b;
st.insert(Node(a, b, v));
}
printf("Case #%d:\n", p++);
scanf("%d", &k);
while (k--) {
scanf("%d %d %d", &q, &a, &b);
if ( == q) {
if (x.find(a) == x.end()) x[a] = a;
if (x.find(b) == x.end()) x[b] = b;
swap(x[a], x[b]);
} else if ( == q) {
if (y.find(a) == y.end()) y[a] = a;
if (y.find(b) == y.end()) y[b] = b;
swap(y[a], y[b]);
} else {
ite = st.find(Node(x[a], y[b]));
printf("%d\n", ite == st.end() ? : ite->val);
}
}
}
return ;
}