您的位置:首页 > 其它

Hdu 4941 Magical Forest(map+离散化)

2014-08-12 20:19 447 查看


Magical Forest

Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 273 Accepted Submission(s): 128



Problem Description

There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci.

However, the forest will make the following change sometimes:

1. Two rows of forest exchange.

2. Two columns of forest exchange.

Fortunately, two rows(columns) can exchange only if both of them contain fruits or none of them contain fruits.

Your superior attach importance to these magical fruit, he needs to know this forest information at any time, and you as his best programmer, you need to write a program in order to ask his answers quick every time.



Input

The input consists of multiple test cases.

The first line has one integer W. Indicates the case number.(1<=W<=5)

For each case, the first line has three integers N, M, K. Indicates that the forest can be seen as maps N rows, M columns, there are K fruits on the map.(1<=N, M<=2*10^9, 0<=K<=10^5)

The next K lines, each line has three integers X, Y, C, indicates that there is a fruit with C energy in X row, Y column. (0<=X<=N-1, 0<=Y<=M-1, 1<=C<=1000)

The next line has one integer T. (0<=T<=10^5)

The next T lines, each line has three integers Q, A, B.

If Q = 1 indicates that this is a map of the row switching operation, the A row and B row exchange.

If Q = 2 indicates that this is a map of the column switching operation, the A column and B column exchange.

If Q = 3 means that it is time to ask your boss for the map, asked about the situation in (A, B).

(Ensure that all given A, B are legal. )



Output

For each case, you should output "Case #C:" first, where C indicates the case number and counts from 1.

In each case, for every time the boss asked, output an integer X, if asked point have fruit, then the output is the energy of the fruit, otherwise the output is 0.



Sample Input

1
3 3 2
1 1 1
2 2 2
5
3 1 1
1 1 2
2 1 2
3 1 1
3 2 2




Sample Output

Case #1:
1
2
1

Hint


题意:给一个n*m的格子,有些格子里面有值为c,然后有以下3种操作。

1、交换行a,b。

2、交换列a,b。

3、求出坐标(a,b)的C值。

题解:n,m是个超大的数。本来想用map记录行和列的映射,然后进行交换,最后用个map把行和列的状态压缩成字符串,行!列的形式存储C值。结果没想到超时了。不知道为什么。辗转几次。后来比赛快结束才用离散化行和列为1e5以内。在一些小细节上又修改了许多,遂AC。注意离散时要去重,查询时要进行。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#define LL __int64
#define M 500009
using namespace std;
struct node
{
	int x,y,z;
}f[100010];
int h[100015],l[100015],w,cas,n,m,k,x,y,c;
int a[100010],b[100010];
string x1,y1;
int main()
{
	scanf("%d",&w);
	map<int,int>mp1,mp2;
	map<string,int>mz;
	for (cas=1;cas<=w;cas++)
	{
		scanf("%d%d%d",&n,&m,&k);
	 	printf("Case #%d:\n",cas);
		mp1.clear();
		mp2.clear();
		mz.clear();
		for (int i=1;i<=k;i++)
		{
			scanf("%d%d%d",&a[i],&b[i],&c);
			f[i].x=a[i];
			f[i].y=b[i];
			f[i].z=c;
		}
		sort(a+1,a+1+k);
		sort(b+1,b+1+k);
		a[0]=-1;b[0]=-1;
		int t1=1,t2=1;
		for (int i=1;i<=k;i++)
		{
			if (!mp1[a[i]])
			{
				h[t1]=a[i];
				mp1[a[i]]=t1;
				t1++;
			}
			if (!mp2[b[i]])
			{
				l[t2]=b[i];
				mp2[b[i]]=t2;
				t2++;
			}
		}
		for (int i=1;i<=k;i++)
		{
			int s1=mp1[f[i].x];
			int s2=mp2[f[i].y];
			char cc[100];
			sprintf(cc, "%d", h[s1]);
			x1=cc;
			sprintf(cc, "%d", l[s2]);
			y1=cc;
			x1=x1+"!"+y1;
		//cout<<x1<<" "<<c<<endl;
			mz[x1]=f[i].z;
		}
		scanf("%d",&k);
		while (k--)
		{
			scanf("%d%d%d",&c,&x,&y);
			if (c==1)
			{
				x=mp1[x];
				y=mp1[y];
				int t=h[x];
				h[x]=h[y];
				h[y]=t;
				
			}
			else if (c==2)
			{
				x=mp2[x];
				y=mp2[y];
				int t=l[x];
				l[x]=l[y];
				l[y]=t;
				
			}
			else
			{
				char cc[100];
				x=mp1[x];
				y=mp2[y];
				if (x + y)
				{
					sprintf(cc, "%d", h[x]);
					x1=cc;
			
					sprintf(cc, "%d", l[y]);
					y1=cc;
					x1=x1+"!"+y1;
				//	cout<<x1<<endl;
					printf("%d\n",mz[x1]);
				}
				else puts("0");
			}
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: