HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941
解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第一种是行交换操作,就是把矩阵的两行进行交换,另一种是列交换操作,注意两种操作都要求行或列至少要有一个水果,第三种操作是查找,询问第A行B列的水果的能量值,如果查询的位置没有水果,则输出0.
因为n和m都很大,达到了2*10^9,但水果最多一共只有10^5个,我的做法是直接用结构体存了之后排序,然后map[i] = j,表示当前的i行相当于原地图中的j行,这样进行行交换操作的时候只要交换一下这个就可以了,原地图上的模样不变,列交换同理。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define maxn 100005
struct node
{
int x,y,c;
}mat[maxn];
map<int,int> hang,lie;
bool cmp(node a,node b)
{
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
int query(int k,int x,int y)
{
int l = ,r = k,d = x,m;
while(l < r)
{
m = (l + r) >> ;
if(d <= mat[m].x) r = m;
else l = m + ;
}
int s1 = l;
if(mat[s1].x != x) return ; //没找到一个符合的x,说明没有这一行
l = ,r = k,d = x + ;
while(l < r)
{
m = (l + r) >> ;
if(d <= mat[m].x) r = m;
else l = m + ;
}
int s2 = l;
if(mat[s2].x != x) s2--;
l = s1,r = s2;
while(l < r)
{
m = (l + r) / ;
if(y <= mat[m].y) r = m;
else l = m + ;
}
if(mat[l].y != y) return ; //没找到符合的y,说明这一行的这一列没有
return mat[l].c;
}
int main()
{
int T,n,m,k,q,kase = ;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
hang.clear();
lie.clear();
for(int i = ;i <= k;++i)
{
scanf("%d%d%d",&mat[i].x,&mat[i].y,&mat[i].c);
hang[mat[i].x] = mat[i].x;
lie[mat[i].y] = mat[i].y;
}
sort(mat+,mat+k+,cmp);
printf("Case #%d:\n",kase++);
scanf("%d",&q);
int op,a,b;
while(q--)
{
scanf("%d%d%d",&op,&a,&b);
if(op == )
{
swap(hang[a],hang[b]);
}
else if(op == )
{
swap(lie[a],lie[b]);
}
else if(op == )
{
a = hang[a];
b = lie[b];
printf("%d\n",query(k,a,b));
}
}
}
return ;
}
/*
5
4 4 4dlsdfflsfls
1 1 1
2 4 4
3 2 2
4 4 3
*/
再附上一种短代码的完全用map实现的,其实关键就是用pair<int,int>作为键。
#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
int T,n,m,k,x,y,c,q,kase;
map<int,int> hang,lie;
map<pair<int,int>,int> mp;
int main()
{
scanf("%d",&T);
kase = ;
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
hang.clear(),lie.clear(),mp.clear();
while(k--)
{
scanf("%d%d%d",&x,&y,&c);
hang[x] = x,lie[y] = y;
mp[pair<int,int> (x,y)] = c;
}
printf("Case #%d:\n",kase++);
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&c,&x,&y);
if(c == ) swap(hang[x],hang[y]);
else if(c == ) swap(lie[x],lie[y]);
else
{
x = hang[x],y = lie[y];
printf("%d\n",mp[pair<int,int> (x,y)]);
}
}
}
return ;
}
HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007的更多相关文章
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 解题报告:有n台机器用来完成m个任务,每个任务有一个难度值和一个需要完成的时间,每台机器有一个可 ...
- 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 ...
- STL : map函数的运用 --- hdu 4941 : Magical Forest
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 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 5371 (2015多校联合训练赛第七场1003)Hotaru's problem(manacher+二分/枚举)
pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<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 --STL Map应用
题意: 有n*m个格子(n,m <= 2*10^9),有k(k<=10^5)个格子中有值,现在有三种操作,第一种为交换两行,第二种为交换两列,交换时只有两行或两列都有格子有值或都没有格子有 ...
随机推荐
- C++ Primer Plus读书笔记
第五章 循环和关系表达式 1. 2.类别别名: (1) #define FLOAT_POINTER float * FLOAT_POINTER pa, pb; 预处理器置换将该声明转换成 flo ...
- centos 6 安装
centos 6 安装步骤 说明: 1.install or upgrade an existing system安装或升级现有系统 2.install system with basic vide ...
- 基于WS-BPEL2.0的服务组合研究
http://tech.it168.com/soadocument/2008-01-03/200801031332376.shtml WS-BPEL是为组合Web服务而制定的一项规范.它的前身是由IB ...
- C中的字符串实例
1.#include <stdio.h>#include <assert.h>size_t strlen(const char* s){ return ( assert( ...
- 9月9日HTML上午表单元素2(框架、样式表)
五.框架 1.frameset是双标签框架集,如果使用框架集,当前页面不能有body. frameset属性:①cols代表左右拆分.cols=“300,*”表示左边框架宽300,右边宽剩余的宽度.* ...
- CPU host-passthrough技术与应用
为了保证虚拟机在不同宿主机之间迁移的时候的兼容性,Libvirt对CPU提炼出标准的几种类型,在/usr/share/libvirt/cpu_map.xml中可以查到.cpu_map.xml不仅是CP ...
- 一种nodejs的MVC框架
mvc会针对请求进行分发,分发一般有controller(针对模块),action(针对模块中的方法),args(请求的参数). 1.先对http请求的url进行设置,解析url中的各种参数: //c ...
- 用淘宝ip地址库查ip
这是一个通过调用淘宝ip地址库实现ip地址查询的功能类 using System; using System.Collections.Generic; using System.Linq; using ...
- 用IIS配置反向代理
http://my.oschina.net/tanyixiu/blog/123832 目标服务器:targetServer 配置反向代理的服务器:reveseProxServer 1.确定最终访问的网 ...
- php:Header
转自鸟哥的博客: http://www.laruence.com/2007/12/16/308.html PHP header()the function declaration: void head ...