hdu 4941 Magical Forest(Map)

http://acm.hdu.edu.cn/showproblem.php?pid=4941


因为地图的行和列很大,操作次数也很多,直接循环模拟肯定不行。但可以用map映射一下,当交换行和列的时候,直接交换它们的映射值,直接O(1)进行交换。


#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std;

const int maxn = 100010;
int n,m,k,T;
map <LL, int>m1,m2;
map <pair<int,int>,int>m3;

int main()
{
	int test;
	int u,v,w;
	int t1,t2;

	scanf("%d",&test);
	for(int item = 1; item <= test; item++)
	{
		m1.clear();
		m2.clear();
		m3.clear();
		scanf("%d %d %d",&n,&m,&k);
		t1 = 0;
		t2 = 0;
		while(k--)
		{
			scanf("%d %d %d",&u,&v,&w);
			if(m1.find(u) == m1.end())
				m1[u] = ++t1;

			if(m2.find(v) == m2.end())
				m2[v] = ++t2;
			m3[ make_pair(m1[u],m2[v]) ] = w;
		}

		scanf("%d",&T);
		printf("Case #%d:\n",item);
		int q,uu,vv;
		while(T--)
		{
			scanf("%d %d %d",&q,&u,&v);
			if(q == 1)
			{
				uu = m1[u];
				vv = m1[v];
				m1[u] = vv;
				m1[v] = uu;
			}
			else if(q == 2)
			{
				uu = m2[u];
				vv = m2[v];
				m2[u] = vv;
				m2[v] = uu;
			}
			else
			{
				if(m1.find(u) == m1.end() || m2.find(v) == m2.end())
				{
					printf("0\n");
					continue;
				}
				uu = m1[u];
				vv = m2[v];
				printf("%d\n",m3[ make_pair(uu,vv) ]);
			}
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值