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[]表示列
然后交换时直交换上面几个东西即可。
查询时先找到nowR[mp[A]], nowC[B],在对应原来的那一行里面找B即可,普通找不好找,索性预先把每行有的元素存起来,存到一个vector V[10^5]里面,因为要存下,所以行需要离散,然后排个序,以后就可以二分地找了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define N 100007 int nowR[N],R[N],b[N];
map<int,int> mp,CntC,nowC;
vector<pair<int,int> > V[N];
struct Point
{
int x,y,c;
}p[N]; int bsearch(int i,int low,int high,int x)
{
while(low <= high)
{
int mid = (low+high)/;
if(V[i][mid].first == x)
return mid;
else if(V[i][mid].first > x)
high = mid-;
else
low = mid+;
}
return low;
} int main()
{
int n,m,k,i,j,T;
int t,cs = ;
int op,A,B;
scanf("%d",&t);
while(t--)
{
mp.clear(); nowC.clear(); CntC.clear();
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++)
{
V[i].clear();
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].c);
p[i].x++, p[i].y++;
nowC[p[i].y] = p[i].y;
b[i] = p[i].x;
}
sort(b+,b+k+);
int ind = unique(b+,b+k+)-b-;
for(i=;i<=ind;i++)
mp[b[i]] = i;
memset(R,,sizeof(R));
for(i=;i<=k;i++)
{
int toR = mp[p[i].x];
R[toR]++;
int toC = nowC[p[i].y];
CntC[toC]++;
V[toR].push_back(make_pair(p[i].y,p[i].c));
}
for(i=;i<=ind;i++)
{
nowR[i] = i;
sort(V[i].begin(),V[i].end());
}
scanf("%d",&T);
printf("Case #%d:\n",cs++);
while(T--)
{
scanf("%d%d%d",&op,&A,&B);
A++,B++;
if(op == )
{
int toA = mp[A];
int toB = mp[B];
if((R[toA]&&R[toB]) || (!R[toA]&&!R[toB]))
{
swap(nowR[toA],nowR[toB]);
swap(R[toA],R[toB]);
}
}
else if(op == )
{
if((CntC[A]&&CntC[B])||(!CntC[A]&&!CntC[B]))
{
swap(CntC[A],CntC[B]);
swap(nowC[A],nowC[B]);
}
}
else
{
int nA = nowR[mp[A]];
int nB = nowC[B];
int id = bsearch(nA,,V[nA].size()-,nB);
if(id < || id >= V[nA].size() || V[nA][id].first != nB)
puts("");
else
printf("%d\n",V[nA][id].second);
}
}
}
return ;
}
HDU 4941 Magical Forest --STL Map应用的更多相关文章
- 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(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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...
- 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】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...
- 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 ...
- HDU 4585 Shaolin(STL map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
随机推荐
- 认识Python
web框架:Django.Tornado.Flask Twisted:复杂的异步网络框架 指定解释器 #!/usr/bin/env python #!/usr/bin/python print (&q ...
- [译] 你应该升级 MQTT3.1.1 的6个理由
原文 6 facts why it’s worth upgrading to the brand new MQTT 3.1.1version 摘要:新版 MQTT 3.1.1 终于在 2014 年 1 ...
- ASP.NET Core 1.0开发Web API程序
.NET Core版本:1.0.0-rc2Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2开发及运行平台:Windows ...
- 深入浅出java 8 lambda表达式--零基础一分钟入门
lambda从使用上来说,第一感觉直白的理解就是,少了很多不必要的匿名回调类的写法,比如: public static void main(String[] args) { PlatformQuery ...
- jQuery coveringBad 效果对比
Covering-Bad 是一个可拉动大小的元素,覆盖在原有的元素上面,从而两者进行对比. 在线实例 实例演示1 实例演示2 使用方法 <div class="covered&q ...
- Android Service获取当前位置(GPS+基站)
需求详情:1).Service中每隔1秒执行一次定位操作(GPS+基站)2).定位的结果实时显示在界面上(要求得到经度.纬度)技术支持:1).获取经纬度通过GPS+基站获取经纬度,先通过GPS来获取, ...
- Android拍照保存在系统相册不显示的问题
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(& ...
- 学习android学习必备的java基础知识--四大内部类
学习android必备的java基础知识--四大内部类 今天学习android课程,因为我的主专业是JAVA,但是兴趣班却有这其他专业的同学,学习android 需要具备一些java的基础知识,因此就 ...
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"
Spring注入Action使用Json错误:org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: ...
- ReactiveCocoa之UI篇
前言: 上一篇讲ReactiveCocoa是函数响应式编程,并将多种事件响应的方式统一起来,使得不同的事件响应方式高度统一.同时也讲了ReactiveCocoa框架里面常见的几个概念.接下来基于那几个 ...