hdu 4941 STL HASH 模拟

http://acm.hdu.edu.cn/showproblem.php?pid=4941

比赛的时候现学的map的find...以前都是用下标做的,但是map用下标查询的话,如果查询的元素不存在,会插入一个新的元素。

贴一个map查找元素找到和找不到的模板

                map<pair<int,int>,int>::iterator it=poshash.find(tmppos);//pair<int,int>poshash;
                int pp;
                if(it == poshash.end())////
                    printf("0\n");
                else
                {
                    pp=it->second;
                    printf("%d\n",nodes[pp].c);
                }

还有map第一个元素,似乎不能是一个结构体的point,看错误信息貌似是因为我没有重载<和>,然后改为pair。1A

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdin)

const int MAXN = 1e5+1000;

struct Node{
    int x,y;
    int c;
}nodes[MAXN];

int n,m,k;
map<int,int>xhash;
map<int,int>yhash;
map<pair<int,int>,int>poshash;
map<int,bool>xvis;
map<int,bool>yvis;

bool cmp(const Node a, const Node b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    else
        return a.y<b.y;
}

void build()
{
    int xx,yy;
    pair<int,int>tmp;
    for(int i=0;i<k;i++)
    {
        xvis[nodes[i].x]=1;
        yvis[nodes[i].y]=1;
        tmp.first=xhash[nodes[i].x]=nodes[i].x;
        tmp.second=yhash[nodes[i].y]=nodes[i].y;
        poshash[tmp]=i;
    }
}

void swapx(int a,int b)
{
    int tmp=xhash[a];
    xhash[a]=xhash[b];
    xhash[b]=tmp;
}

void swapy(int a,int b)
{
    int tmp=yhash[a];
    yhash[a]=yhash[b];
    yhash[b]=tmp;
}

int main()
{
    int ncase;
    int t,q,a,b,r,c,tmp;
    pair<int,int>tmppos;
    scanf("%d",&ncase);
    for(int ic=1;ic<=ncase;ic++)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<k;i++)
            scanf("%d%d%d",&nodes[i].x,&nodes[i].y,&nodes[i].c);
        sort(nodes,nodes+k,cmp);
        build();
        scanf("%d",&t);
        printf("Case #%d:\n",ic);
        while(t--)
        {
            scanf("%d%d%d",&q,&a,&b);
            if(q == 1)
            {
                if((xvis[a]&&xvis[b]) || (!xvis[a]&&!xvis[b]))
                    swapx(a,b);
            }
            if(q == 2)
            {
                if( (yvis[a]&&yvis[b]) || (!yvis[a]&&!yvis[b]))
                    swapy(a,b);
            }
            if(q == 3)
            {
                tmppos.first=xhash[a];
                tmppos.second=yhash[b];
                map<pair<int,int>,int>::iterator it=poshash.find(tmppos);
                int pp;
                if(it == poshash.end())////
                    printf("0\n");
                else
                {
                    pp=it->second;
                    printf("%d\n",nodes[pp].c);
                }
                    //pp=poshash.find(tmppos)
            }
        }

    }
    return 0;
}

hdu 4941 STL HASH 模拟

时间: 2024-12-11 05:44:50

hdu 4941 STL HASH 模拟的相关文章

hdu 4941 stl的map&lt;node,int&gt;用法

#include<iostream> #include<cstdio> #include<cstring> #include<map> using namespace std; typedef struct node{ int x,y; bool operator<(const node &b)const { if(x==b.x) return y<b.y; else return x<b.x; } }node; int main(

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

HDU 4941 Magical Forest STL

这明明就是给纯C选手的大杀器啊. 题意:给你k坐标,表示 X,Y 有值C,有 3种操作 1) 交换A,B两行 2) 交换A,B两列 3) 询问(A,B)的值 解题思路:map离散化 解题代码: // File Name: 1007.cpp // Author: darkdream // Created Time: 2014年08月12日 星期二 21时05分18秒 #include<vector> #include<list> #include<map> #includ

HDU 4941 Magical Forest _(:зゝ∠)_ 模拟题

模拟大法保平安_(:зゝ∠)_ #include <cstdio> #include <map> #include <set> #include <algorithm> using namespace std; const int N = 1; struct node{ int x, y, val; node(int a=0,int b=0,int c=0):x(a),y(b),val(c){} bool operator<(const node&am

hdu 4941 Magical Forest(hash映射)

题目链接:hdu 4941 Magical Forest 题目大意:给定N,M和K,表示在一个N*M的棋盘上有K个棋子,给出K个棋子的位置和值,然后是Q次操作,对应的是: 1 a b :交换a和b两行 2 a b : 交换a和b两列 3 a b :查询a b这个位置上棋子的值,没有棋子的话输出0 解题思路:一开始X[i]=i,X[j]=j,如果需要交换i和j,那么就令X[i]=j,X[j]=i即可,因为N和M很大,所以用map映射. #include <cstdio> #include <

STL : map函数的运用 --- hdu 4941 : Magical Forest

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 343 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场

Magical Forest                                                                       Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is a forest can be seen as N * M grid. In this fore

HDU 4941

Magical Forest Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, th

hdu 4941 Magical Forest(Map)

http://acm.hdu.edu.cn/showproblem.php?pid=4941 因为地图的行和列很大,操作次数也很多,直接循环模拟肯定不行.但可以用map映射一下,当交换行和列的时候,直接交换它们的映射值,直接O(1)进行交换. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack