首页 > 代码库 > hdu 4941 STL HASH 模拟

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;
}