【Codeforces 1180 E. Serge and Dining Room】线段树

CF1180E
给你 n,m,q
n长度的arr数组 有n道菜的价格
m长度的brr数组 有m个人的钱
q长度的 opt数组,x数组,y数组
如果opt 是 1 把第x个菜价格改成 y
如果opt 是 2 把 第x个人钱改成 y
大家排队买 如果能买就买能买的最贵的
问你一个人他最后等大家买完了再买能买到最贵的是什么

我们这样想 把可行域放在数轴上
如果有一道菜 就把 1 - arr[i] 区间加 1 代表这里有食物
有一个人 代表 1 - brr[i] 区间减 1 代表能买

所以问的就是1 - N最大的maxx[i]等于 0 的下标
离散化一下即可求出来

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<#define dbg3(x1,x2,x3) cout<<#x1<<" = "<#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 300025;
int arr[MAX_N],brr[MAX_N],tmp[MAX_N*3],opt[MAX_N],x[MAX_N],y[MAX_N];
const int N = MAX_N*3;
int Max(int a,int b){return a>b?a:b;}
map<int ,int >mp;
namespace sgt
{
    #define mid ((l+r)>>1)
    int maxx[N<<2],col[N<<2];
    void up(int rt)
    {
        maxx[rt] = Max(maxx[rt<<1],maxx[rt<<1|1]);
    }
    void down(int rt,int l,int r)
    {
        if(col[rt])
        {
            col[rt<<1]+=col[rt];
            col[rt<<1|1]+=col[rt];
            maxx[rt<<1]+=col[rt];
            maxx[rt<<1|1]+=col[rt];
            col[rt] = 0 ;
        }
    }
    void update(int rt,int l,int r,int x,int y,int v)
    {
        if(x<=l&&r<=y)
        {
            col[rt]+=v;
            maxx[rt]+=v;
            return ;
        }
        down(rt,l,r);
        if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
        else if(y<=mid) update(rt<<1,l,mid,x,y,v);
        else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
        up(rt);
    }
    int query(int rt,int l,int r,int x,int y)
    {
        if(maxx[rt]<=0) return -1;
        if(l==r) return l;
        down(rt,l,r);
        int ans = -1;
        if(maxx[rt<<1|1]>0&&mid<y) ans = query(rt<<1|1,mid+1,r,x,y);
        if(ans==-1&&x<=mid) ans = query(rt<<1,l,mid,x,y);
        return ans;
    }
    #undef mid
}


int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m,q,cnt = 0;
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),tmp[++cnt] = arr[i];
    for(int i = 1;i<=m;++i) scanf("%d",&brr[i]),tmp[++cnt] = brr[i];
    scanf("%d",&q);
    for(int i = 1;i<=q;++i)
    {
        scanf("%d%d%d",&opt[i],&x[i],&y[i]);
        tmp[++cnt] = y[i];
    }
    sort(tmp+1,tmp+1+cnt);
    int sz = unique(tmp+1,tmp+1+cnt)-tmp-1;
    for(int i = 1;i<=n;++i) {int t = arr[i];arr[i] = lower_bound(tmp+1,tmp+1+sz,arr[i]) - tmp;mp[arr[i]] = t;}
    for(int i = 1;i<=m;++i) {int t = brr[i];brr[i] = lower_bound(tmp+1,tmp+1+sz,brr[i]) - tmp;mp[brr[i]] = t;}
    for(int i = 1;i<=q;++i) {int t = y[i];y[i] = lower_bound(tmp+1,tmp+1+sz,y[i]) - tmp;mp[y[i]] = t;}
    for(int i = 1;i<=n;++i)
        sgt::update(1,1,N,1,arr[i],1);
    for(int i = 1;i<=m;++i)
        sgt::update(1,1,N,1,brr[i],-1);
    for(int i = 1;i<=q;++i)
    {
        if(opt[i]==1)
        {
            sgt::update(1,1,N,1,arr[x[i]],-1);
            arr[x[i]] = y[i];
            sgt::update(1,1,N,1,arr[x[i]],1);
            if(sgt::query(1,1,N,1,N)==-1) printf("-1\n");
            else printf("%d\n",mp[sgt::query(1,1,N,1,N)]);
        }
        else
        {
            sgt::update(1,1,N,1,brr[x[i]],1);
            brr[x[i]] = y[i];
            sgt::update(1,1,N,1,brr[x[i]],-1);
            if(sgt::query(1,1,N,1,N)==-1) printf("-1\n");
            else printf("%d\n",mp[sgt::query(1,1,N,1,N)]);
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

你可能感兴趣的:(ACM,DS-线段树)