题意:
有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci。之后又q个询问,分三种;
1)1 a b,将a行和b行交换
2)2 a b,将a列和b列交换
3)3 a b,询问(a,b)位置的果树的能量值。
题解:
由于n和m很大,但树的个数只有1e5,所以可以用map离散化。
而每次交换行或者列的时候只要交换map映射的值即可。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std;
const int maxn = 100010;
int n,m,k,T;
map <LL, int>m1,m2;
map <pair<int,int>,int>m3;
int main()
{
int test;
int u,v,w;
int t1,t2;
scanf("%d",&test);
for(int item = 1; item <= test; item++)
{
m1.clear();
m2.clear();
m3.clear();
scanf("%d %d %d",&n,&m,&k);
t1 = 0;
t2 = 0;
while(k--)
{
scanf("%d %d %d",&u,&v,&w);
if(m1.find(u) == m1.end())
m1[u] = ++t1;
if(m2.find(v) == m2.end())
m2[v] = ++t2;
m3[ make_pair(m1[u],m2[v]) ] = w;
}
scanf("%d",&T);
printf("Case #%d:\n",item);
int q,uu,vv;
while(T--)
{
scanf("%d %d %d",&q,&u,&v);
if(q == 1)
{
uu = m1[u];
vv = m1[v];
m1[u] = vv;
m1[v] = uu;
}
else if(q == 2)
{
uu = m2[u];
vv = m2[v];
m2[u] = vv;
m2[v] = uu;
}
else
{
if(m1.find(u) == m1.end() || m2.find(v) == m2.end())
{
printf("0\n");
continue;
}
uu = m1[u];
vv = m2[v];
printf("%d\n",m3[ make_pair(uu,vv) ]);
}
}
}
return 0;
}
本文介绍了一种使用离散化方法解决大规模矩阵中树能量查询与交换问题的算法。通过利用map进行行和列的映射,有效解决了n*m矩阵上树的位置交换及能量查询,适用于树数量相对较少的情况。
1768

被折叠的 条评论
为什么被折叠?



