1 条题解

  • 0
    @ 2025-8-24 21:25:49

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar 66666a
    **

    搬运于2025-08-24 21:25:48,当前版本为作者最后更新于2017-08-17 16:29:22,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    这题用线段树,平衡树都能过

    但是其实我们可以用STL中的set

    设一个set记录当前被炸的房子编号,并且维护编号从小到大,当我们查询x时,找到在s中比x大的第一个和比x小的最后一个,位置之差减1即为答案。

    代码出奇的短

    #include<bits/stdc++.h>
    #define M 50010
    using namespace std;
    int q[M],tail,n,m;
    set<int> s;
    set<int>:: iterator it;
    int main()
    {
        scanf("%d%d",&n,&m);
        s.insert(0);
        s.insert(n+1);
        for(int i=1;i<=m;i++)
        {
            char c;
        cin>>c;//用scanf会把空格读进来
            if(c=='D')
            {
                int x;                      // 加入x
                scanf("%d",&x);
                s.insert(x);
                q[++tail]=x;
            }
            if(c=='Q')
            {
                int x;                       //查询
                scanf("%d",&x);
                it=s.lower_bound(x);
                if(*it==x)
                {
                    printf("0\n");
                    continue;
                }
                int ans=*it-*(--it);
                printf("%d\n",ans-1);
            }
            if(c=='R')                    //删除
            {
                it=s.find(q[tail--]);
                s.erase(it);
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    496
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者