Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Ksyusha and the Loaded Set

#include <bits/stdc++.h>
using namespace std;

int mx = 4 * (int)1e6 + 4;

void solve() {
    // dp[v] is the longest missing streak starting at value v.
    vector<int> dp(mx, mx);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        dp[x] = 0;
    }
    // Reconstruct the entire DP table.
    for (int i = mx - 2; i >= 0; i--) {
        if (dp[i] != 0) {
            dp[i] = 1 + dp[i + 1];
        }
    }

    int m;
    cin >> m;
    while (m--) {
        char type;
        cin >> type;
        if (type == '+') {
            int x;
            cin >> x;

            // Reconstruct the DP array to the left.
            for (int i = 0; i < x; i++) {
                int dist = x - i;
                if (dp[i] > dist) {
                    dp[i] -= dp[x];
                }
            }
            dp[x] = 0;
        }
        if (type == '-') {
            int x;
            cin >> x;
            dp[x] = 1 + dp[x + 1];

            // Reconstruct the DP array to the left.
            for (int i = 0; i < x; i++) {
                int dist = x - i;
                if (dp[i] == dist) {
                    dp[i] += dp[x];
                }
            }
        }
        if (type == '?') {
            int k;
            cin >> k;
            for (int i = 1; i < mx; i++) {
                if (dp[i] >= k) {
                    cout << i << " ";
                    break;
                }
            }
        }
    }
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}