hdu4941

/*
离散化之后
将对应的操作改为对下标的操作
*/
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值