拉杆给油不要慌 2022-11-11 14:06 采纳率: 50%
浏览 34

洛谷P5250【深基17.例5】木材仓库

https://www.luogu.com.cn/problem/P5250?%ra=link

洛谷P5250【深基17.例5】木材仓库
我在接的时候报错了
作为小学生,还有很多不懂的地方,可以帮帮我吗?
代码:

#include<bits/stdc++.h>
#include<map>
using namespace std;
int n,op,t;
map<int,int> mp;
map<int,int>::iterator it,it2;
int main(){
    /*freopen(".in","r",stdin);
    freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision()<<x<<endl;
    十年OI一场空,不开long long见祖宗
    暴力出奇迹,打表过样例
    */
    cin>>n;
    for(int i = 1 ; i<=n ; i++){
        cin>>op>>t;
        if(op == 1){
            if(mp.count(t)) cout<<"Already Exist\n";
            else mp[t] = 1;
        }else if(op == 2){
            if(mp.empty()) cout<<"Empty\n";
            else if(mp.count(t)){
                mp.erase(t);     
                cout<<t<<endl;
            }else{
                mp[t] = 1;
                it = mp.find(t);
                it2 = it;
                it++;
                if(it == mp.begin()){
                    cout<<it->first<<endl;
                    mp.erase(it); 
                }else if(it == mp.end()){
                    cout<<(--it2)->first<<endl;
                    mp.erase(it2);
                }
                else if(t-(--it2)->first>it->first-t){
                    cout<<it->first<<endl;
                    mp.erase(it);
                }
                else{
                    cout<<it2->first<<endl;
                    mp.erase(it2);
                }
                mp.erase(t); 
            }
        }
    }
    return 0;
}

问题报错
Process exited after 3.526 seconds with return value 3221226356

  • 写回答

1条回答 默认 最新

  • honestman_ 2022-11-11 14:31
    关注
    
    #include<bits/stdc++.h>
    using namespace std;
     
    struct Node
    {
        int val,key;
        int l,r;
        int size;
    };
    Node k[100001];
    int n,root,Index;
     
    inline void update(int x)//计算子树大小 
    {
        k[x].size=k[k[x].l].size+k[k[x].r].size+1;
    }
     
    inline int newnode(int cnt)//新建结点 
    {
        ++Index;
        k[Index].val=cnt;
        k[Index].key=rand();
        k[Index].size=1;
        return Index;
    }
     
    void spilt(int now,int cnt,int &x,int &y)//分裂 
    {
        if(!now) x=y=0;
        else
        {
            if(k[now].val<=cnt)
            {
                x=now;
                spilt(k[now].r,cnt,k[now].r,y);
            }
            else
            {
                y=now;
                spilt(k[now].l,cnt,x,k[now].l);
            }
            update(now);
        }
    }
     
    int merge(int x,int y)//合并 
    {
        if(!x || !y) return x+y;
        else
        {
            if(k[x].key>k[y].key)
            {
                k[x].r=merge(k[x].r,y);
                update(x);
                return x;
            }
            else
            {
                k[y].l=merge(x,k[y].l);
                update(y);
                return y;
            }
        }
    }
     
    void Insert(int x)//插入 
    {
        int a,b;
        spilt(root,k[x].val,a,b);
        root=merge(merge(a,x),b);
    }
     
    void Delete(int x)//删除 
    {
        int a,b,c;
        spilt(root,x,a,c);
        spilt(a,x-1,a,b);
        b=merge(k[b].l,k[b].r);
        root=merge(merge(a,b),c);
    }
     
    void pre(int x,int &y)//x的前驱
    {
        int a,b,now;
        spilt(root,x,a,b);
        now=a;
        while(k[now].r)
            now=k[now].r;
        y=now;
        root=merge(a,b);
    }
     
    void nxt(int x,int &y)//x的后继 
    {
        int a,b,now;
        spilt(root,x-1,a,b);
        now=b;
        while(k[now].l)
            now=k[now].l;
        y=now;
        root=merge(a,b);
    }
     
    int main()
    {
        int Alive=0;
        scanf("%d",&n);
        for(register int i=1;i<=n;++i)
        {
            int typ,a;
            scanf("%d%d",&typ,&a);
            if(typ==1)//进货操作 
            {
                int b;
                pre(a,b);
                if(a==k[b].val) printf("Already Exist\n");//如果树中已经有这个数了 
                else//如果树中还没有这个数 
                {
                    Insert(newnode(a));//那么插入这个数 
                    ++Alive;//树中结点数 +1 
                }
            }
            if(typ==2)//出货操作 
            {
                int b,c;
                if(!Alive) printf("Empty\n");//如果树为空 
                else//如果树不为空 
                {
                    int Ans;//要删除的数 
                    pre(a,b);
                    nxt(a,c);
                    if(!b || !c)//如果一个数没有前驱或者没有后继 
                    {
                        if(!b) Ans=k[c].val;//如果没有前驱,删除后继 
                        else Ans=k[b].val;//如果没有后继,删除前驱 
                    }
                    else//如果一个数前驱和后继都有了 
                    {
                        if(a-k[b].val<=k[c].val-a) Ans=k[b].val;
                        else Ans=k[c].val;//比较前驱和后继与这个数的距离 
                    }
                    printf("%d\n",Ans);
                    Delete(Ans);//删除这个数 
                    --Alive;//树中结点数 -1  
                }
            }
        }
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月11日

悬赏问题

  • ¥15 微软硬件驱动认证账号申请
  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 如何在cst中建立这种螺旋扇叶结构
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?
  • ¥15 请问GPT语言模型怎么训练?
  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角