zoukankan      html  css  js  c++  java
  • HDU 4941 Magical Forest 【离散化】【map】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941

    题目大意:给你10^5个点。每一个点有一个数值。点的xy坐标是0~10^9。点存在于矩阵中。然后给出10^5个操作。1代表交换行。2代表交换列,3代表查询坐标为xy点的数值。

    数据量非常大........ 所以一直没有思路

    后来赛后看了题解是先用离散化然后存在线性map里面。

    用hx,hy来存放离散化后的点的坐标,用linkx,linky来存放点离散化之后的点的坐标的行与列。

    还是对于STL里面的基础运用掌握不牢。

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<map>
    using namespace std;
    #define maxn 100010
    
    struct node {
         int u,v,w;
    } point[maxn];
    
    //其基本的原理是用一个线性的map存放。
    //map里面的<int,int>是map一个特征。最后其所占的空间还是maxn这么多
    map<int,int> p_w[maxn];
    map<int,int> hx,hy;
    
    bool  cmpx(node A,node B)
    {
         return A.u<B.u;
    }
    
    
    bool cmpy(node A,node B)
    {
         return A.v<B.v;
    }
    
    int main ()
    {
         int W;
         scanf("%d",&W);
         for(int w=1; w<=W; w++) {
              ///初始化
              for(int i=0; i<maxn; i++) p_w[i].clear();
              hx.clear();
              hy.clear();
    
              int N,M,K;
              scanf("%d%d%d",&N,&M,&K);
              for(int i=0; i<K; i++)
                   scanf("%d%d%d",&point[i].u,&point[i].v,&point[i].w);
    
              int tx=1;
              sort(point,point+K,cmpx);
              for(int i=0; i<K; i++) if(!hx[point[i].u]) hx[point[i].u]=tx++;
    
              int ty=1;
              sort(point,point+K,cmpy);
              for(int i=0; i<K; i++) {
                   if(!hy[point[i].v]) hy[point[i].v]=ty++;
                   p_w[hx[point[i].u]][hy[point[i].v]]=point[i].w;
              }
    
              int linkx[maxn];
              int linky[maxn];
              for(int i=0; i<maxn; i++) linkx[i]=i,linky[i]=i;
    
              printf("Case #%d:
    ",w);
    
              int T;
              int Q,A,B;
              scanf("%d",&T);
              while(T--) {
                   scanf("%d%d%d",&Q,&A,&B);
    
                   if(Q==1) {
                        int tem=linkx[hx[A]];
                        linkx[hx[A]]=linkx[hx[B]];
                        linkx[hx[B]]=tem;
                   }
                   if(Q==2) {
                        int tem=linky[hy[A]];
                        linky[hy[A]]=linky[hy[B]];
                        linky[hy[B]]=tem;
                   }
                   if(Q==3)
                        printf("%d
    ",p_w[linkx[hx[A]]][linky[hy[B]]]);
              }
         }
    }
    


  • 相关阅读:
    这个夏天,感动我的歌,感动我的你
    设计中最困难的部分在于决定要设计什么 设计原本择录
    Sql效能优化总结(续) sql语句优化篇
    sql效能优化总结
    使用AStyle进行代码格式化
    迭代模型 转
    软件项目开发系列开篇杂谈
    Sql效能优化总结(续) 架构调整篇
    throw和throw ex的区别
    面向过程&面向对象 UML&RUP
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5354555.html
Copyright © 2011-2022 走看看