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
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.
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. )
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.
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
1
2
1
No two fruits at the same location.
Mean:
有一片N*M的森林,里面种着一些果实,现在有三种操作:1.交换第i行和第j行的果实;2.交换第i列和第j列的果实;3.查询第i行第j列有多少果实。
analyse:
由于地图的范围达到了2*1e9,我们不可能用二维数组,必须离散化。
我们用三个map函数,一个存行坐标,一个存列坐标,另一个存果实的数目。在变换的时候,只需交换行坐标的map或者列坐标的map,存果实的map函数是一直不变的,这样就大大减少了时间的消耗。
Time complexity:O(k)
Source code:
//Memory Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
map<int,int>row;
map<int,int>col;
map<int,map<int,int> >val;
int main()
{
#ifndef ONLINE_JUDGE
freopen("cin.txt","r",stdin);
#endif
int T,kase=1;
cin>>T;
while(T--)
{
row.clear();
col.clear();
val.clear();
int x,y,k,c;
int rrow=1,ccol=1;
scanf("%d %d %d",&x,&y,&k);
while(k--)
{
scanf("%d %d %d",&x,&y,&c);
if(!row[x])
row[x]=rrow++;
if(!col[y])
col[y]=ccol++;
x=row[x],y=col[y];
val[x][y]=c;
}
printf("Case #%d:\n",kase++);
int Q,tmp;
cin>>Q;
while(Q--)
{
scanf("%d %d %d",&c,&x,&y);
if(c==1)
{
tmp=row[x];
row[x]=row[y];
row[y]=tmp;
}
else if(c==2)
{
tmp=col[x];
col[x]=col[y];
col[y]=tmp;
}
else
printf("%d\n",val[row[x]][col[y]]);
}
}
return 0;
}
STL : map函数的运用 --- hdu 4941 : Magical Forest的更多相关文章
- hdu 4941 Magical Forest
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...
- hdu 4941 Magical Forest (map容器)
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 4941 Magical Forest --STL Map应用
题意: 有n*m个格子(n,m <= 2*10^9),有k(k<=10^5)个格子中有值,现在有三种操作,第一种为交换两行,第二种为交换两列,交换时只有两行或两列都有格子有值或都没有格子有 ...
- HDU 4941 Magical Forest 【离散化】【map】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...
- HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...
- hdu 4941 Magical Forest ( 双重map )
题目链接 题意: 有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci.之后又q个询问,分三种; 1)1 a b,将a行和b行交换 2)2 a b,将a列和b列交换 3)3 ...
- HDU 4941 Magical Forest (Hash)
这个题比赛的时候是乱搞的,比赛结束之后学长说是映射+hash才恍然大悟.因此决定好好学一下hash. 题意: M*N的格子,里面有一些格子里面有一个值. 有三种操作: 1.交换两行的值. 2.交换两列 ...
- HDU 4941 Magical Forest(2014 Multi-University Training Contest 7)
思路:将行列离散化,那么就可以用vector 存下10W个点 ,对于交换操作 只需要将行列独立分开标记就行 . r[i] 表示第 i 行存的是 原先的哪行 c[j] 表示 第 j ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
随机推荐
- Backbone源码解析(二):Model(模型)模块
Model(模型)模块在bk框架中的作用主要是存储处理数据,它对外和对内都有很多操作数据的接口和方法.它与视图(Views)模块精密联系着,通过set函数改变数据结构从而改变视图界面的变化.下面我们来 ...
- tsd-提升IDE对JavaScript智能感知的能力
在编写前端JavaScript代码时,最痛苦的莫过于代码的智能感知(Intelli Sense). 追其根源,是因为JavaScript是一门弱类型的动态语言.对于弱类型的动态语言来说,智能感知就是I ...
- HTML5 history API实践
一.history API知识点总结 在HTML4中,我们已经可以使用window.history对象来控制历史记录的跳转,可以使用的方法包括: history.forward();//在历史记录中前 ...
- 实战使用Axure设计App,使用WebStorm开发(6) – 迈向后端
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- .NET面试基础知识
1. 什么是Asp.Net? 答:Asp.Net是一种基于.NET平台下的动态web开发技术,它使用的是codebehind(代码后置技术),可以将前台呈现和后台代码进行有效的分离. 2. ...
- 谨慎DateTime.Now在EF的query中的使用
执行如下代码: var query = from tr in _carrierRepository select new BaseCarrier { CarrierCode = tr.CarrierC ...
- Redis学习笔记~Redis主从服务器,读写分离
回到目录 Redis这个Nosql的存储系统一般会被部署到linux系统中,我们可以把它当成是一个数据服务器,对于并发理大时,我们会使用多台服务器充当Redis服务器,这时,各个Redis之间也是分布 ...
- WebApi系列~QQ互联的引入(QConnectSDK)
回到目录 感谢与改进 首先要感谢张善友老兄为大家封装的这个DLL,它将QQ官方的相关API都集成到了这个里面,这对于开发人员来说,是个福音,有人会说,为什么QQ官方没有提供.net版的SDK呢,在这里 ...
- 360路由器c301最新固件支持万能中继
360路由器c301现在已经停产了.出厂的固件是360_301_0.7.3.8.这个版本不支持中继.5G信号.本文主要介绍如何刷最新固件以及设置万能中继. 本文为作者原创,发表在博客园:http:// ...
- 360路由器刷openwrt后设置wifi中继
上一篇文章(360路由器刷openwrt.不死uboot.双系统 .wifi中继 - 飞鸿影~ - 博客园)讲了如何在360路由器C301上安装openwrt以及安装双系统.这篇文章讲如何设置无线中继 ...