改变选的顺序, 最后的结果不变。
这种题一般都和前缀和有关, 建个线段树维护前缀和就好了。
#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; } /* */