这个题比赛的时候是乱搞的,比赛结束之后学长说是映射+hash才恍然大悟。因此决定好好学一下hash。

题意:

  M*N的格子,里面有一些格子里面有一个值。

  有三种操作:

    1.交换两行的值。

    2.交换两列的值。

    3.询问某个格子的值。

  保证,交换的时候要么两行都有值,要么两行都为空。

思路:

  其实交换两行或者两列的话,相当于把那两行的对应下标交换一下。因此我们可以想到,对行标和列表建立一个索引,交换的时候,我们只需要交换一下索引。

  因为行和列比较多,所以我们不能直接存整个矩阵,即使离散化也不可以,所以这里我们需要用到hash。(Map也可以)。

代码:

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = (int) 1e5+;
const ll HASH = (ll) (1e6)+; struct node {
int x, y, e;
}a[MAXN]; int hash_table[HASH], next[MAXN]; int d_x[MAXN], d_y[MAXN], len_x, len_y;
int xx[MAXN], yy[MAXN];
int n;
ll mx, my; inline void add(int x, int y, int k) {
int id = ((ll)x*n+y)%HASH;
next[k] = hash_table[id];
hash_table[id] = k;
} inline int find(int x, int y) {
int id = ((ll)x*n+y)%HASH;
int p;
for (p = hash_table[id]; p!=-; p = next[p])
if (a[p].x==x&&a[p].y==y)
return a[p].e;
return ;
} void solve() {
memset(hash_table, -, sizeof(hash_table));
scanf("%d%d%d", &mx, &my, &n);
for (int i = ; i < n; i++) {
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].e);
d_x[i] = a[i].x;
d_y[i] = a[i].y;
xx[i] = yy[i] = i;
}
sort(d_x, d_x+n);
len_x = unique(d_x, d_x+n) - d_x;
sort(d_y, d_y+n);
len_y = unique(d_y, d_y+n) - d_y;
for (int i = ; i < n; i++) {
a[i].x = lower_bound(d_x, d_x+len_x, a[i].x)-d_x;
a[i].y = lower_bound(d_y, d_y+len_y, a[i].y)-d_y;
add(a[i].x, a[i].y, i);
} int Q, o, x, y;
int rx, ry;
scanf("%d", &Q);
while (Q--) {
scanf("%d%d%d", &o, &x, &y);
if (o==) {
rx = lower_bound(d_x, d_x+len_x, x) - d_x;
ry = lower_bound(d_x, d_x+len_x, y) - d_x;
if (x==d_x[rx]&&y==d_x[ry]) swap(xx[rx], xx[ry]);
} else if (o==) {
rx = lower_bound(d_y, d_y+len_y, x) - d_y;
ry = lower_bound(d_y, d_y+len_y, y) - d_y;
if (x==d_y[rx]&&y==d_y[ry]) swap(yy[rx], yy[ry]);
} else {
rx = lower_bound(d_x, d_x+len_x, x) - d_x;
ry = lower_bound(d_y, d_y+len_y, y) - d_y;
if (x==d_x[rx]&&y==d_y[ry]) printf("%d\n", find(xx[rx], yy[ry]));
else puts("-1");
}
}
} int main() {
#ifdef Phantom01
freopen("HDU4941.in", "r", stdin);
freopen("my4941.txt", "w", stdout);
#endif //Phantom01 int T;
scanf("%d", &T);
for (int i = ; i <= T; i++) {
printf("Case #%d:\n", i);
solve();
} return ;
}

HDU 4941 Magical Forest (Hash)的更多相关文章

  1. hdu 4941 Magical Forest

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...

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

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  3. hdu 4941 Magical Forest (map容器)

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  4. HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...

  6. HDU 4941 Magical Forest(2014 Multi-University Training Contest 7)

    思路:将行列离散化,那么就可以用vector 存下10W个点 ,对于交换操作 只需要将行列独立分开标记就行   . r[i] 表示第 i 行存的是 原先的哪行         c[j] 表示 第 j ...

  7. HDU 4941 Magical Forest --STL Map应用

    题意: 有n*m个格子(n,m <= 2*10^9),有k(k<=10^5)个格子中有值,现在有三种操作,第一种为交换两行,第二种为交换两列,交换时只有两行或两列都有格子有值或都没有格子有 ...

  8. 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 ...

  9. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

随机推荐

  1. BootStrap学习(一)——BootStrap入门

    1.环境搭建 中文官网下载地址:http://www.bootcss.com/ 右击选中的WEB项目,点击导入,选择文件系统,然后下一步,选择BootStrap文件目录路径,如下: 完成后,WEB项目 ...

  2. 【原创】Apache集群报Service Temporarily Unavailable的解决

    Apache的集群突然时不时的报出以下错误: Service Temporarily Unavailable The server is temporarily unable to service y ...

  3. eclipse oxygen离线安装activiti

    我用的最新版本的eclipse oxygen,结果通过help-->install new software-->add 一直报找不到,后来网上查找的离线安装,终于成功了,在这里和大家分享 ...

  4. HDU 1171 Big Event in HDU【01背包】

    题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路---@_@ 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容 ...

  5. php——get与post方法(转)

    file_get_contents版本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php /**  * 发送p ...

  6. 贰、js的基础(三)数组

    JS中数组的操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...

  7. [HDU5685]2016"百度之星" - 资格赛 Problem A

    题目大意:给你一个字符串,和一些问题,每个问题问你[l,r]子串的哈希值是多少. 哈希值计算方法为:$H(s)=\prod _{i=1} ^{i\leq len(s)}(s_i-28)(mod\ 99 ...

  8. codevs 1288 埃及分数 (迭代加深搜索)

    题目大意:给你一个分数$a/b$,把它拆解成$\sum_{i=1}^{n}1/ai$的形式,必须保证$ai$互不相同的情况下,尽量保证n最小,其次保证分母最大的分数的分母最小 什么鬼玄学题!!! 因为 ...

  9. 学习参考《父与子的编程之旅python【第二版】》高清中文版PDF+高清英文版PDF+源代码

    对于初步接触编程语言的朋友,推荐看一看<父与子的编程之旅第2版>,对于完全编程零基础的很友好! 图文并茂,过多的文字堆垒很容易让人产生厌倦情绪,也更容易让人产生放弃的想法.使用了大量插图, ...

  10. linux 系统相关命令

    说明:此篇以 Debian ( ubuntu16.04 ) 命令为例 1. tab键默认是不能自动补全命令 apt install bash-completion // 安装完成之后重启系统 2. 虚 ...