Fzu2022车站

题目链接
foj有点老了,不资瓷万能头,不能用unordered_map,用map会爆内存
线段树维护左边界,右边界,和答案。

#include<map>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define ok (l==r)
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)>>1
#define lson ls,l,m
#define rson rs,m+1,r
typedef long long LL;
using namespace std;
const int maxn=500005;
int pos[500050];
struct node{
    int is;
    int lmx,rmx;
    int ans;
}tree[maxn*3];
void clamp(int &a){
    a=a<=0?0:1;
}
void pushup(int rt){
    tree[rt].lmx=tree[rt].rmx=tree[rt].ans=0;
    if(tree[ls].ans){
        tree[rt].ans=tree[ls].ans;
        if(tree[rs].ans){
            tree[rt].ans=min(tree[rt].ans,tree[rs].ans);
        }
    }else if(tree[rs].ans){
        tree[rt].ans=tree[rs].ans;
    }
    if(tree[ls].lmx){
        tree[rt].lmx=tree[ls].lmx;
        tree[rt].rmx=tree[ls].rmx;
    }
    if(tree[rs].rmx){
        tree[rt].rmx=tree[rs].rmx;
        if(!tree[ls].lmx)tree[rt].lmx=tree[rs].lmx;
    }
    if(!tree[rt].ans)tree[rt].ans=tree[rt].rmx-tree[rt].lmx;
    else if(tree[ls].rmx&&tree[rs].lmx){
        tree[rt].ans=min(tree[rt].ans,tree[rs].lmx-tree[ls].rmx);
    }
}
void update(int rt,int l,int r,int po,int add){
    //printf("%d %d %d\n",l,r,po);
    if(ok){
        tree[rt].is+=add;
        clamp(tree[rt].is);
        if(!tree[rt].is)tree[rt].lmx=tree[rt].rmx=tree[rt].ans=0;
        else tree[rt].lmx=tree[rt].rmx=pos[l],tree[rt].ans=0;
        return ;
    }
    int m=mid;
    if(po<=m)update(lson,po,add);
    else update(rson,po,add);
    pushup(rt);
}
struct OP{
    int type;
    int num;
}op[500050];
char str[5];
int cnt;
void init(){
    memset(tree,0,sizeof tree);
    cnt=0;
}

int main(){
    int t;
    while(~scanf("%d",&t)){
        init();
        for(int i=1;i<=t;i++){
            scanf("%s",str);
            if(str[0]=='a'){
                op[i].type=1;
                scanf("%d",&op[i].num);
                pos[++cnt]=op[i].num;
            }
            if(str[0]=='d'){
                op[i].type=-1;
                scanf("%d",&op[i].num);
                pos[++cnt]=op[i].num;
            }
            if(str[0]=='m'){
                op[i].type=0;
            }
        }
        sort(pos+1,pos+1+cnt);
        cnt=unique(pos+1,pos+1+cnt)-pos-1;
        for(int i=1;i<=t;i++){
            if(!op[i].type)printf("%d\n",tree[1].ans);
            else {
                int k=lower_bound(pos+1,pos+cnt+1,op[i].num)-pos;
                update(1,1,cnt,k,op[i].type);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值