Codeforces Round 966 (Div. 3)H(线段树上二分)

Ksyusha and the Loaded Set

题目大意:给一个可增减的序列,若干询问,询问有三种操作,+元素,-元素,?则表示输出对应的最小正整数,使得区间[x,x+k-1]中的任意一个正整数不在此序列中

思路:线段树上二分(动态开点,静态的没试过,群u可以试试)

观察到:每次修改一个元素,只会影响,这个元素在原序列当中,左右俩边的空余区间(即左右俩边没有元素的区间长度)。

 实现:线段树上的叶子节点我们就用来表示空余区间的左端点,然后节点上的值我们就可以存一下每个空余区间对应的长度。 对于每个?询问,我们可以维护当前区间的最大长度,先判断左儿子是否符合,符合就进入左子树继续递归,否则进入右子树,遇到叶子节点返回。

trick:(我们可以先在序列的俩端插入0和一个无穷值,这样做方便后面查找我们删除和修改的元素)用一个set动态维护这个序列,保持他的单调性(方便查找这个元素在序列的哪个位置) 

 代码:

#include<bits/stdc++.h>

using namespace std;
const int N=2e6+20;
struct xdss{
    int l,r,mxlen=0;
}xds[N*4];
int root,idx;
void motify(int &rt,int l,int r,int pos,int val){
    if(!rt) rt=++idx;
    if(l==r){
        xds[rt].mxlen=val;
        return;
    }
    int mid=l+r>>1;
    if(pos<=mid) motify(xds[rt].l,l,mid,pos,val);
    else motify(xds[rt].r,mid+1,r,pos,val);
    int lson=xds[rt].l,rson=xds[rt].r;
    xds[rt].mxlen=max(xds[lson].mxlen,xds[rson].mxlen);
    return;
}
int query(int rt,int l,int r,int klen){
    if(l==r) return l;
    int mid=l+r>>1;
    int lson=xds[rt].l;
    if(lson&&xds[lson].mxlen>=klen) return query(xds[rt].l,l,mid,klen);
    return query(xds[rt].r,mid+1,r,klen);
}
void solve()
{
    int n,m;
    cin>>n;
    set<int>ys;
    for(int i=1;i<=idx;i++) xds[i].l=xds[i].r=xds[i].mxlen=0;
    root=idx=0;
    vector<int>a(n+2);
    for(int i=1;i<=n;i++) cin>>a[i];
    a[n+1]=(int)4e6+10;
    ys.insert(0);
    int pre=0;
    for(int i=1;i<=n+1;i++){
        int l=pre+1,r=a[i]-1;
        pre=a[i];
        ys.insert(a[i]);
        if(l>r) continue;
        int len=r-l+1;
        motify(root,1,N-10,l,len);
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        char op;
        cin>>op;
        if(op=='+')
        {
            int x;
            cin>>x;
            auto u=ys.lower_bound(x);//大于等于x的第一个元素
            auto pr=prev(u);
            int ll=*pr+1,rr=*u-1;
            if(ll<=rr) motify(root,1,N-10,ll,0);
            rr=x-1;;
           if(ll<=rr) motify(root,1,N-10,ll,rr-ll+1);
            ll=x+1,rr=*u-1;
            if(ll<=rr) motify(root,1,N-10,ll,rr-ll+1);
            ys.insert(x);
        }
        else if(op=='-')
        {
            int x;
            cin>>x;
            auto u=ys.lower_bound(x);
            auto pr=prev(u);
            auto at=next(u);
            int ll=*pr+1,rr=x-1;
            if(ll<=rr) motify(root,1,N-10,ll,0);
            ll=x+1,rr=*at-1;
            if(ll<=rr) motify(root,1,N-10,ll,0);
            ll=*pr+1,rr=*at-1;
            if(ll<=rr) motify(root,1,N-10,ll,rr-ll+1);
            ys.erase(x);
        }
        else{

            int k;
            cin>>k;
           cout<<query(root,1,N-10,k)<<' ';
        }
    }
    cout<<'\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int T = 1;
    cin>>T;
    while (T--) { solve(); }
    return 0;
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值