HDU 4941 Magical Forest 【離散化】【map】,hdu4941

來源:互聯網
上載者:User

HDU 4941 Magical Forest 【離散化】【map】,hdu4941

題目連結: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 100010struct 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:\n",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\n",p_w[linkx[hx[A]]][linky[hy[B]]]);          }     }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.