re求调
查看原帖
re求调
444651
ASSASSIN2099楼主2024/3/7 21:51
#include <bits/stdc++.h>
using namespace std;
#define M 191111
int re()
{
    int x = 0, w = 1;
    char y;
    y = getchar();
    while (y < '0' || y > '9')
    {
        if (y == '-')
            w = -1;
        y = getchar();
    }
    while (y >= '0' && y <= '9')
    {
        x = x * 10 + y - '0';
        y = getchar();
    }
    return x * w;
}
int e;
int ans=0;
int n,m;
int vis[M];
int c[M];
int f[M];
typedef struct tree{
    int val;
    tree* next;
    tree(int x):
    val(x),next(NULL){};
};
class box{
    public:
    tree* build(int x);
    void delt(tree* head,int x);
    int ask(tree* head,int x);
    void add(tree* head,int x,int y);
};
tree* box::build(int x)
{
    tree* head=new tree(1);
    return head;
}
int box::ask(tree* head,int x)
{
    tree* p=head;
    while(p)
    {
        if(p->val==x)return p->next->val;
        if(!p->next)break;
        p=p->next;
    }return 0;
}
void box::add(tree* head,int x,int y)
{
    tree* p=head;
    while(p)
    {
        if(p->val==x)
        {
            tree* nw=new tree(y);
            if(p->next)nw->next=p->next;
            p->next=nw; 
            return ;
        }
        if(!p->next)break;
        p=p->next;
    }
}
void box::delt(tree* head,int x)
{
    tree* p=head;
    while(p)
    {
        if(p->val==x)
        {
            if(p->next->next)p->next=p->next->next;
            else p->next=NULL;
            return;
        }
        if(!p->next)break;
        p=p->next;
    }
}
signed main()
{
    n=re();
    box fir;
    tree* nw=fir.build(1);
    for(int i=1;i<=n;i++)
    {
        int k=re();
        if(k==1)
        {
            int x=re(),y=re();
            fir.add(nw,x,y);
        }
        else if(k==2)
        {
            int x=re();
            cout<<fir.ask(nw,x)<<endl;
        }
        else if(k==3)
        {
            int x=re();
            fir.delt(nw,x);
        }
    }
    system("pause");
    return 0;
}
2024/3/7 21:51
加载中...