HDU 4941 magical forest [discretization] [map]

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4941

Here are 10 ^ 5 vertices. Each vertex has a numerical value. The XY coordinate of the vertex is 0 ~ 10 ^ 9, points exist in the matrix. Then, a 10 ^ 5 operation is given. 1 indicates the exchange row, 2 indicates the exchange column, and 3 indicates the value where the query coordinate is the XY point.

A large amount of data...

After the competition, I saw that the problem was solved by first discretization and then linear map.

#include <iostream>#include <cstdio>#include <map>#include <algorithm>using namespace std;struct Node{    int x;    int y;    int c;}l[100000];map <int,  int>  fmap[100010];map <int ,int>hashx,hashy;int T,n,m,k,mapx,mapy,t,cas,lianx[100010],liany[100010],Q,A,B,X,Y;bool cmpx(Node a,Node b){    return a.x<b.x;}bool cmpy(Node a,Node b){    return a.y<b.y;}int main(){    int CAS;    scanf("%d", &CAS);    for(int cas=1;cas<=CAS;cas++)    {      scanf("%d%d%d",&n,&m,&k);      printf("Case #%d:\n",cas);      for(int i=0;i<k;i++) scanf("%d%d%d",&l[i].x,&l[i].y,&l[i].c);      for(int i=0;i<100010;i++) fmap[i].clear();        hashx.clear();  hashy.clear();  mapx=mapy=0;      sort(l,l+k,cmpx);      for(int i=0;i<k;i++) if(!hashx[l[i].x]) hashx[l[i].x]=++mapx;      sort(l,l+k,cmpy);      for(int i=0;i<k;i++)      {        if(!hashy[l[i].y]) hashy[l[i].y]=++mapy;        fmap[hashx[l[i].x]][hashy[l[i].y]]=l[i].c;      }      for(int i=1;i<=mapx;i++)lianx[i]=i;      for(int i=1;i<=mapy;i++)liany[i]=i;      scanf("%d",&t);      for(int i=0;i<t;i++)      {        scanf("%d%d%d",&Q,&A,&B);        if(Q==1)        {          X=hashx[A];Y=hashx[B];          if(X && Y) swap(lianx[X],lianx[Y]);        }        else if(Q==2)        {          X=hashy[A];Y=hashy[B];          if(X && Y) swap(liany[X],liany[Y]);        }        else if(Q==3)        {          X=hashx[A];Y=hashy[B];          if(X && Y)printf("%d\n",fmap[lianx[X]][liany[Y]]);          else printf("0\n");        }      }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.