Codeforces 1179C Serge and Dining Room 线段树

Serge and Dining Room

改变选的顺序, 最后的结果不变。

这种题一般都和前缀和有关, 建个线段树维护前缀和就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 7340033;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

int n, m, a[N], b[N];

const int maxVal = 1000000;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct segmentTree {
    int lazy[N << 2], mx[N << 2];
    inline void pull(int rt) {
        mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
    }
    inline void push(int rt) {
        if(lazy[rt]) {
            lazy[rt << 1] += lazy[rt];
            lazy[rt << 1 | 1] += lazy[rt];
            mx[rt << 1] += lazy[rt];
            mx[rt << 1 | 1] += lazy[rt];
            lazy[rt] = 0;
        }
    }
    void update(int L, int R, int val, int l = 1, int r = maxVal, int rt = 1) {
        if(R < l || r < L || R < L) return;
        if(L <= l && r <= R) {
            mx[rt] += val;
            lazy[rt] += val;
            return;
        }
        push(rt);
        int mid = l + r >> 1;
        update(L, R, val, lson);
        update(L, R, val, rson);
        pull(rt);
    }
    int query(int l = 1, int r = maxVal, int rt = 1) {
        if(mx[rt] <= 0) return -1;
        if(l == r) return l;
        push(rt);
        int mid = l + r >> 1;
        if(mx[rt << 1 | 1] > 0) return query(rson);
        else return query(lson);
    }
} Tree;


int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= m; i++) scanf("%d", &b[i]);
    for(int i = 1; i <= n; i++) {
        Tree.update(1, a[i], 1);
    }
    for(int i = 1; i <= m; i++) {
        Tree.update(1, b[i], -1);
    }
    int q; scanf("%d", &q);
    while(q--) {
        int op, p, x;
        scanf("%d%d%d", &op, &p, &x);
        if(op == 1) {
            Tree.update(1, a[p], -1);
            a[p] = x;
            Tree.update(1, a[p], 1);
        } else {
            Tree.update(1, b[p], 1);
            b[p] = x;
            Tree.update(1, b[p], -1);
        }
        printf("%d\n", Tree.query());
    }
    return 0;
}

/*
*/

 

转载于:https://www.cnblogs.com/CJLHY/p/11102793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值