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 ...
随机推荐
- java Map及Map.Entry详解
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...
- EF6(CodeFirst)+MySql开发脱坑指南
废话 话说当年,在一个春光明媚的晌午,邂逅了迷人的丁香姑娘,从此拜倒在了她的石榴裙下,至今不能自拔,这位丁香姑娘就是ORM思想. 所谓ORM思想,我的理解就是根据一定的规则,把程序中的对象和数据库中的 ...
- 论checkbox和radio的样式美化问题
如果你下定决心要改变现有的默认的checkbox和radio的样式,那么我目前有两种办法: 1.自己动手写一个,也就是自己写代码实现将input的checkbox和radio默认的样式隐藏掉,使用绝对 ...
- 欢迎访问我的快站fbengine.kuaizhan.com
欢迎访问我的快站 fbengine.kuaizhan.com
- 细说.NET中的多线程 (六 使用MemoryBarrier,Volatile进行同步)
上一节介绍了使用信号量进行同步,本节主要介绍一些非阻塞同步的方法.本节主要介绍MemoryBarrier,volatile,Interlocked. MemoryBarriers 本文简单的介绍一下这 ...
- Effective c#学习笔记(1)
条款1 使用属性代替可访问的数据成员 属性是个啥,谁在用? 1. C#的属性在被访问的时候看起来好像是数据成员,但其实是方法. 2. 在.NET框架中,一般使用属性表达公有数据成员. ...
- 锋利的JQuery —— 事件和动画
大图猛戳
- Atitit. Api 设计 原则 ---归一化
Atitit. Api 设计 原则 ---归一化 1.1. 叫做归一化1 1.2. 归一化的实例:一切对象都可以序列化/toString 通过接口实现1 1.3. 泛文件概念.2 1.4. 游戏行业 ...
- Atitit 图像处理—图像形态学(膨胀与腐蚀)
Atitit 图像处理-图像形态学(膨胀与腐蚀) 1.1. 膨胀与腐蚀1 1.2. 图像处理之二值膨胀及应用2 1.3. 测试原理,可以给一个5*5pic,测试膨胀算法5 1.4. Photoshop ...
- asp.net mvc 使用ajax请求 控制器 (PartialViewResult)分部的action,得到一个分部视图(PartialView)的HTML,进行渲染
在asp.net mvc 使用ajax请求获取数据的时候,我们一般是返回json或者xml,然后解析这些数据进行渲染,这样会比较麻烦,可以请求一个 分部action,返回一个分部视图 直接可以渲染,不 ...