水题竟然没有很快A出来= =
首先对所有点的行,列离散化。
然后维护一个row[i]表示第i行现在所表示的行,同理列也是。那么第i行和第j行交换只要row[i]和row[j]交换一下就可以了。
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 100007;
map<pair<int, int>, int> maps;
struct Point {
int x, y, c;
};
struct Point point[maxn];
int row_num[maxn], col_num[maxn], num_row, num_col, row_turn[maxn], col_turn[maxn];
int n, m, k, q;
int main() {
// freopen("in.txt", "r", stdin);
int test_case;
scanf("%d", &test_case);
int ncase = 1;
while (test_case--) {
scanf("%d%d%d", &n, &m, &k);
printf("Case #%d:\n", ncase);
ncase++;
num_row = num_col = 0;
maps.clear();
for (int i = 1; i <= k; i++) {
scanf("%d%d%d", &point[i].x, &point[i].y, &point[i].c);
row_num[num_row++] = point[i].x;
col_num[num_col++] = point[i].y;
}
sort(row_num, row_num + num_row);
sort(col_num, col_num + num_col);
int row_len = unique(row_num, row_num + num_row) - row_num;
int col_len = unique(col_num, col_num + num_col) - col_num;
for (int i = 1; i <= row_len; i++) {
row_turn[i] = i;
}
for (int i = 1; i <= col_len; i++) {
col_turn[i] = i;
}
for (int i = 1; i <= k; i++) {
int cnt_row, cnt_col;
cnt_row = lower_bound(row_num, row_num + row_len, point[i].x) - row_num + 1;
cnt_col = lower_bound(col_num, col_num + col_len, point[i].y) - col_num + 1;
maps[make_pair(cnt_row, cnt_col)] = point[i].c;
}
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a == 3) {
int pre = b;
b = lower_bound(row_num, row_num + row_len, b) - row_num;
if (row_num[b] != pre) {
printf("0\n");
continue;
}
b++;
b = row_turn[b];
c = lower_bound(col_num, col_num + col_len, c) - col_num + 1;
c = col_turn[c];
if (maps.count(make_pair(b, c))) {
printf("%d\n", maps[make_pair(b, c)]);
}
else {
printf("0\n");
}
}
else if (a == 1) {
int pre = b;
b = lower_bound(row_num, row_num + row_len, b) - row_num;
if (row_num[b] != pre) {
continue;
}
b++;
c = lower_bound(row_num, row_num + row_len, c) - row_num + 1;
swap(row_turn[c], row_turn[b]);
}
else if (a == 2) {
int pre = b;
b = lower_bound(col_num, col_num + col_len, b) - col_num;
if (col_num[b] != pre) {
continue;
}
b++;
c = lower_bound(col_num, col_num + col_len, c) - col_num + 1;
swap(col_turn[b], col_turn[c]);
}
}
}
return 0;
}