【树状数组+二分】BZOJ2028 [SHOI2009]会场预约

题面在这里

考虑到每个预约只可能被删除一次

所以可以把与当前冲突的预约一个一个删除

其实就是不断取某位置前面第一个比自己小的端点

这个用树状数组+二分维护就好了(其实也是第K大问题)

然后这样就 O(nlog2n)

示例程序:

#include<cstdio>
#include<vector>
#include<algorithm>
#define pb push_back
#define lowbit(x) ((x)&-(x))
using namespace std;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int red(){
    int res=0,f=1;char ch=nc();
    while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();}
    while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc();
    return res*f;
}
inline char fstchar(){
    char ch=nc();while (ch<'A'||'Z'<ch) ch=nc();
    return ch;
}

const int maxn=200005;
int q,num,BIT[maxn],dl[maxn],dr[maxn],len;
vector<int> a[maxn];
bool vis[maxn];
void ist(int x,int w){
    for (int i=x;i<=100000;i+=lowbit(i)) BIT[i]+=w;
}
int qry(int x){
    int res=0;
    for (int i=x;i;i-=lowbit(i)) res+=BIT[i];
    return res;
}
int find(int x){
    int q=qry(x),l=1,r=x;
    if (!q) return 0;
    while (l<=r){
        int mid=l+r>>1;
        if (qry(mid)<q) l=mid+1;else r=mid-1;
    }
    return l;
}
int work(int l,int r){
    int res=0;
    for (int j=r;;){
        int t=find(j);
        if (!a[t].size()) break;
        while (a[t].size()>0){
            int id=*a[t].rbegin();
            if (dr[id]<l) return res;
            ist(dl[id],-1);
            a[t].pop_back();res++;
        }
    }
    return res;
}
int main(){
    q=red();
    while (q--)
     if (fstchar()=='A'){
        int l=red(),r=red();
        int res=work(l,r);
        num-=res;num++;
        dl[++len]=l;dr[len]=r;
        ist(l,1);a[l].pb(len);
        printf("%d\n",res);
     }else printf("%d\n",num);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值