/*
离散化之后
将对应的操作改为对下标的操作
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
const int MAXN = 100000 + 10;
int cases, samples, n, m, k, q;
int x, y, w;
int op, a, b, pos;
int X[MAXN], Y[MAXN]; //原来的行列
int anotherX[MAXN], anotherY[MAXN]; //记录对应的行列
int row[MAXN], col[MAXN]; //离散化
int totr, totc;
struct Node //苹果
{
int x, y;
int w;
bool operator < (Node a) const
{
return x < a.x || (x == a.x && y < a.y);
}
bool operator == (Node a) const
{
return x == a.x && y == a.y;
}
}node[MAXN], temp;
template <class T> //二分查找
int finds(T key, T x[], int len)
{
int low = 0, high = len - 1, mid;
while (low <= high)
{
mid = (low + high) >> 1;
if (x[mid] == key) return mid;
if (x[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
void input()
{
scanf("%d", &cases);
while (cases--)
{
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < k; i++)
{
scanf("%d %d %d", &x, &y, &w);
X[i] = node[i].x = x;
Y[i] = node[i].y = y;
node[i].w = w;
}
sort(node, node + k);
sort(X, X + k);
sort(Y, Y + k);
totr = totc = 0;
anotherX[totr] = 0, anotherY[totc] = 0;
row[totr++] = X[0], col[totc++] = Y[0];
for (int i = 1; i < k; i++)
{
if (X[i] != row[totr])
{
anotherX[totr] = totr;
row[totr++] = X[i];
}
if (Y[i] != col[totc])
{
anotherY[totc] = totc;
col[totc++] = Y[i];
}
}
scanf("%d", &q);
printf("Case #%d:\n", ++samples);
while (q--)
{
scanf("%d %d %d", &op, &a, &b);
if (op == 1)
{
x = finds(a, row, totr);
y = finds(b, row, totr);
if (x != -1)
{
swap(anotherX[x], anotherX[y]);
}
}
else if (op == 2)
{
x = finds(a, col, totc);
y = finds(b, col, totc);
if (x != -1)
{
swap(anotherY[x], anotherY[y]);
}
}
else
{
x = finds(a, row, totr);
y = finds(b, col, totc);
if (x != -1 && y != -1)
{
x = row[anotherX[x]];
y = col[anotherY[y]];
temp.x = x, temp.y = y;
pos = finds(temp, node, k);
printf("%d\n", pos == -1 ? 0 : node[pos].w);
}
else
{
printf("0\n");
}
}
}
}
}
int main()
{
input();
return 0;
}
hdu4941
最新推荐文章于 2014-08-13 19:35:12 发布