+ Segment tree discrete intervals update --cf1179C good title

Definitely a good question

When the problem into the i-th answer to the inquiry is feasible whether a numerical value x

To determine the value of x is feasible, then the problem into a long array> = the number of the value of x is strictly greater than> = the value of x in the array b

Then the line leaves the number of nodes safeguard the legitimate number for a value of x in the array is an array of legal -b number of miles, if this value is a positive number that is feasible

Segment tree maintenance interval maximum value, and then ask the rightmost non-negative leaf subscript

#include<bits/stdc++.h>
#include<vector>
using namespace std;
#define maxn 1000005

int Q,n,m,a[maxn],b[maxn],ans[maxn];
struct Query{int op,i,x;}q[maxn];
vector<int>v;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int lazy[maxn<<2],Max[maxn<<2];
void pushdown(int rt){
    if(lazy[rt]!=0){
        Max[rt<<1]+=lazy[rt];
        Max[rt<<1|1]+=lazy[rt];
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
    }
}
void pushup(int rt){
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void update(int L,int R,int val,int l,int r,int rt){
    if(L<=l && R>=r){
        lazy[rt]+=val;
        Max[rt]+=val;
        return;
    }
    pushdown(rt);
    int m=l+r>>1;
    if(L<=m)update(L,R,val,lson);
    if(R>m)update(L,R,val,rson);
    pushup(rt);
}
int query(int l,int r,int rt){
    if(Max[rt]<=0)return -1;
    if(l==r && Max[rt]>0)return l;
    pushdown(rt);
    int m=l+r>>1;
    if(Max[rt<<1|1]>0)
        return query(rson);
    else if(Max[rt<<1]>0)
        return query(lson);
    else return -1;
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),v.push_back(a[i]);
    for(int i=1;i<=m;i++)scanf("%d",&b[i]),v.push_back(b[i]);
    cin>>Q;
    for(int i=1;i<=Q;i++){
        scanf("%d%d%d",&q[i].op,&q[i].i,&q[i].x);
        v.push_back(q[i].x);
    } 
    
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    int nn=v.size();
    
    for(int i=1;i<=n;i++){
        int pos=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
        update(1,pos,1,1,nn,1);
    }
    for(int i=1;i<=m;i++){
        int pos=lower_bound(v.begin(),v.end(),b[i])-v.begin()+1;
        update(1,pos,-1,1,nn,1);
    }

    for(int i=1;i<=Q;i++){
        int op=q[i].op,p=q[i].i,x=q[i].x;
        if(op==1){//修改a的值 
            int pos=lower_bound(v.begin(),v.end(),a[p])-v.begin()+1;
            update(1,pos,-1,1,nn,1);
            a[p]=x;
            pos=lower_bound(v.begin(),v.end(),a[p])-v.begin()+1;
            update(1,pos,1,1,nn,1);
            ans[i]=query(1,nn,1);
            if(ans[i]>0)
                ans[i]=v[ans[i]-1]; 
        }
        else {
            int pos=lower_bound(v.begin(),v.end(),b[p])-v.begin()+1; 
            update(1,pos,1,1,nn,1);
            b[p]=x;
            pos=lower_bound(v.begin(),v.end(),b[p])-v.begin()+1;
            update(1,pos,-1,1,nn,1);
            ans[i]=query(1,nn,1);
            if(ans[i]>0)
                ans[i]=v[ans[i]-1];
        }
    }
    for(int i=1;i<=Q;i++)
        cout<<ans[i]<<'\n';
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11104433.html