P1503 鬼子进村

题目

P1503 鬼子进村

做法

仿佛喝了一桶水

一眼平衡树,找前驱和后继,太暴力了,平衡树只会\(splay\),码量出奇的大

找到\(x\)点往左右扩散的区间,当然线段树也能做

左端点和右端点?明明一个set就能解决的事

My complete code

#include<cstdio>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
typedef long long LL;
const LL maxn=2000000;
inline LL Read(){
    LL x(0),f(1);char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}
LL n,m,top;
LL visit[maxn],sta[maxn];
set<LL> Set;
set<LL> ::iterator it,pre,next;
int main(){
    n=Read(),m=Read();
    Set.insert(0),Set.insert(n+1);
    while(m--){
        char c; scanf(" %c",&c);
        if(c=='D'){
            LL now(Read());
            if(!visit[now]){
                visit[now]=1,Set.insert(now),
                sta[++top]=now;
            }
        }else if(c=='Q'){
            LL now(Read());
            if(visit[now]){
                printf("0\n"); continue;
            }
            Set.insert(now),
            it=Set.find(now);
            pre=it,--pre;
            next=it,++next;
            printf("%lld\n",*next-*pre-1);
            Set.erase(now);
        }else{
            if(top){
                LL now(sta[top--]);
                Set.erase(now),
                visit[now]=0;
            }
        }
    }
}

转载于:https://www.cnblogs.com/y2823774827y/p/10301859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值