hdu 4941map的运用

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

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
(1)这道题n*m的数据范围特别大,所以如果想开个二维数组是行不通的。

 (2)但是k最大只有10的5次方,所以可以考虑建立一个结构体,里面存着每个fruit的坐标值和价值,每个操作都从0到k遍历一次,如果这么做就得TLE。

 (3)所以用map来解决这个问题,不需要考虑数组的大小问题;具体看代码  ps:当询问到一个具体坐标值,查看fruit的价值时,只需要管(x,y)的最后一次变换,之前的都被覆盖;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;
int main()
{
 int T,n,m,k,test=0;
 cin>>T;
 while(T--)
 {
   test++;
   map<pair<int ,int >,int >mp;
   map<int ,int >x;
   map<int ,int >y;
   mp.clear();
   x.clear();
   y.clear();
   scanf("%d%d%d",&n,&m,&k);
   int a,b,c;
   for(int i=0;i<k;i++)
    {
      scanf("%d%d%d",&a,&b,&c);
      mp[make_pair(a,b)]=c;
    }
   printf("Case #%d:\n",test);
   int w,op,l,r;
   scanf("%d",&w);
   while(w--)
   {
      scanf("%d%d%d",&op,&l,&r);
      if(op==1)
      {
        int ll,rr;
        if(!x[l])ll=l;
        if(!x[r])rr=r;
        x[l]=rr;
        x[r]=ll;
      }
      else if(op==2)
      {
        int ll,rr;
        if(!y[l])ll=l;
        if(!y[r])rr=r;
        y[l]=rr;
        y[r]=ll;
      }
      else if(op==3)
      {
        int ll,rr;
        if(x[l])
        ll=x[l];
        else
        ll=l;
        if(y[r])
        rr=y[r];
        else
        rr=r;
        printf("%d\n",mp[make_pair(ll,rr)]);
      }
   }
 }
 return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值