HDU 4941 Magical Forest STL

这明明就是给纯C选手的大杀器啊。

题意:给你k坐标,表示 X,Y 有值C,有 3种操作

1) 交换A,B两行

2) 交换A,B两列

3) 询问(A,B)的值

解题思路:map离散化

解题代码:

// File Name: 1007.cpp
// Author: darkdream
// Created Time: 2014年08月12日 星期二 21时05分18秒

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL long long

using namespace std;
map<int,map<int,int> > a;
map<int,int>  hashx , hashy;
const int maxn = 100005;
int T , n , m ,k;
struct node{
   int x, y ,c;
}l[maxn];
int cmp(node t,node tt)
{
     return t.x < tt.x;
}
int cmp1(node t,node tt)
{
    return t.y < tt.y;
}
int main(){
    int T;
    scanf("%d",&T);
    for(int ca = 1; ca <= T; ca ++)
    {
       hashx.clear(),hashy.clear(),a.clear();
       scanf("%d %d %d",&n,&m,&k);
       for(int i = 1;i <= k;i ++)
          scanf("%d %d %d",&l[i].x,&l[i].y,&l[i].c);
       sort(l+1,l+1+k,cmp);
       int mapx,mapy;
       mapx = mapy = 0;
       for(int i = 1;i <= k;i ++)
           if(hashx.find(l[i].x) == hashx.end())
               hashx[l[i].x] = ++mapx;
       sort(l+1,l+1+k,cmp1);
       for(int i =1;i <= k ;i ++)
       {
          if(hashy.find(l[i].y) == hashy.end())
          {
              hashy[l[i].y] = ++ mapy;
          }
          a[hashx[l[i].x]][hashy[l[i].y]] = l[i].c;
       }
       scanf("%d",&m);
       printf("Case #%d:\n",ca);
       for(int i = 1;i <= m;i ++)
       {
          int Q,A,B;
          scanf("%d %d %d",&Q,&A,&B);
          if(Q ==  1){
              if(hashx.find(A) != hashx.end())
              {
                int temp = hashx[A];
                hashx[A] = hashx[B];
                hashx[B] = temp;
              }
          }else if( Q == 2){
              if(hashy.find(A) != hashy.end())
              {
                int temp = hashy[A];
                hashy[A] = hashy[B];
                hashy[B] = temp;
              }
          }else {
             if(hashx.find(A) != hashx.end() && hashy.find(B) != hashy.end())
             {
                printf("%d\n",a[hashx[A]][hashy[B]]);
             }else printf("0\n");
          }
       }
    }

    return 0;
}

HDU 4941 Magical Forest STL,布布扣,bubuko.com

时间: 2024-09-30 05:26:31

HDU 4941 Magical Forest STL的相关文章

HDU 4941 Magical Forest --STL Map应用

题意: 有n*m个格子(n,m <= 2*10^9),有k(k<=10^5)个格子中有值,现在有三种操作,第一种为交换两行,第二种为交换两列,交换时只有两行或两列都有格子有值或都没有格子有值时才能交换,第三种操作是询问现在第A行第B列值为多少. 解法:格子太大,但是k比较小,所以考虑离散一下,把行离散出来,最多10^5行,列用map存下即可. nowR[i] = j 时表示现在的第 i 行是原来的第 j 行, nowC表示列的 R[]表示该行有没有值, CntC[]表示列 然后交换时直交换上面

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

STL : map函数的运用 --- hdu 4941 : Magical Forest

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 343 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场

Magical Forest                                                                       Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is a forest can be seen as N * M grid. In this fore

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

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

hdu 4941 Magical Forest

被虐了一下午..离散化,开一个rr[mnx], cc[mnx],初始化为rr[i] = i, cc[i],然后换的时候就换这两个数组就好了.. 然后就是不断的lower_bound 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #include<string> 6 #include<queue> 7 #includ

HDU 4941 Magical Forest 【离散化】【map】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点,每个点有一个数值,点的xy坐标是0~10^9,点存在于矩阵中.然后给出10^5个操作,1代表交换行,2代表交换列,3代表查询坐标为xy点的数值. 数据量很大........ 所以一直没有思路 后来赛后看了题解是先用离散化然后存在线性map里面. #include <iostream> #include <cstdio> #include <map

HDU 4941 Magical Forest(离散化)

HDU 4941 Magical Forest 题目链接 题意:给定一些点,点有值,现在3种操作交换行,列,询问某个点值 思路:这是签到题,坐标系很大,所以把坐标离散化储存,每次交换的时候只要把相应的行列坐标交换即可,查询就在交换过的上面查就可以了 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; #define