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;
}


你可能感兴趣的:(map)