哈希1AC四RE求救

P3613 【深基15.例2】寄包柜

QDHSLGYYJK @ 2020-11-16 17:55:56

#include<cstdio> 
#define ll long long
#define Mod 100003
using namespace std;
struct node
{
    int d;
    int k;
    node *nxt;
};
node *Insert(node *head,ll pos,int k);
node *Delete(node *head,ll pos);
node *Find(node *head,ll pos);
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    node *head[100007]={};
    for (int i=1;i<=q;++i)
    {
        int b,x,y;
        scanf("%d%d%d",&b,&x,&y);
        ll pos=(ll)x*n+y;
        if (b==1)
        {
            int k;
            scanf("%d",&k);
            if (k)
                head[pos%Mod]=Insert(head[pos%Mod],pos,k);
            else
                head[pos%Mod]=Delete(head[pos%Mod],pos);
        }
        else
            printf("%d\n",Find(head[pos%Mod],pos)->k);
    }
    return 0;
}
node *Insert(node *head,ll pos,int k)
{
    int d=pos/Mod;
    node *p=head,*prev=NULL,*newp=new(node);
    newp->d=d;
    newp->k=k;
    while (p!=NULL){
        if (p->d>d)
            break;
        prev=p;
        p=p->nxt;
    }
    newp->nxt=p;
    if (prev==NULL)
        head=newp;
    else
        prev->nxt=newp;
    return head;
}
node *Delete(node *head,ll pos)
{
    int d=pos/Mod;
    node *p=head,*prev=NULL;
    while (p!=NULL){
        if (p->d==d)
            break;
        prev=p;
        p=p->nxt;
    }
    if (prev==NULL)
        head=head->nxt;
    else
        prev->nxt=p->nxt;
    delete p;
}
node *Find(node *head,ll pos){
    int d=pos/Mod;
    node *p=head,*prev=NULL;
    while (p!=NULL){
        if (p->d==d)
            break;
        prev=p;
        p=p->nxt;
    }
    return p;
}

大概是哈希(其实我也不懂哈希具体是什么)。


|