https://www.luogu.com.cn/problem/P5250?%ra=link
洛谷P5250【深基17.例5】木材仓库
我在接的时候报错了
作为小学生,还有很多不懂的地方,可以帮帮我吗?
代码:
#include
#include
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<
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<else {
mp[t] = 1;
it = mp.find(t);
it2 = it;
it++;
if(it == mp.begin()){
cout<first<erase(it);
}else if(it == mp.end()){
cout<<(--it2)->first<erase(it2);
}
else if(t-(--it2)->first>it->first-t){
cout<first<erase(it);
}
else{
cout<first<erase(it2);
}
mp.erase(t);
}
}
}
return 0;
}
问题报错
Process exited after 3.526 seconds with return value 3221226356
#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;
}