1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>

using namespace std;

const int N = 400005, BASE = (1 << 19);

int n, q, type[N];

long long szprot[N], szprotQuery[N];
long long start[N], stop[N];

map<long long, int> firstSzprot;

int smallId[N], smallIdQuery[N];

long long tree[2 * BASE + 5];
int cnt[2 * BASE + 5];

vector<pair<long long, pair<int, int> > > szprots;
vector<int> blocked;
bool isBlocked[2 * BASE + 5];

void insert(int pos, long long w) {
    pos += BASE;
    int added = w > 0 ? 1 : -1;
    while (pos >= 1) {
        tree[pos] += w;
        cnt[pos] += added;
        pos /= 2;
    }
}

pair<long long, int> eatSzprots(int w, int from, int to, int maxTo, long long remaining) {
    if (isBlocked[w]) {
        return {0, 0};
    }
    if (to <= maxTo && tree[w] <= remaining) {
        blocked.push_back(w);
        isBlocked[w] = true;
        return {tree[w], cnt[w]};
    }
    if (from == to && to <= maxTo && tree[w] >= remaining) {
        blocked.push_back(w);
        isBlocked[w] = true;
        return {tree[w], cnt[w]};
    } 
    if (from > maxTo) {
        return {0, 0};
    }
    
    int mid = (from + to) / 2;
    auto pRight = eatSzprots(2 * w + 1, mid + 1, to, maxTo, remaining);
    pair<long long, int> pLeft = {0, 0};
    if (pRight.first < remaining) {
        remaining -= pRight.first;
        pLeft = eatSzprots(2 * w, from, mid, maxTo, remaining);
    }
    tree[w] = (!isBlocked[2 * w]) * tree[2 * w] + (!isBlocked[2 * w + 1]) * tree[2 *  w + 1];
    cnt[w] = (!isBlocked[2 * w]) * cnt[2 * w] + (!isBlocked[2 * w + 1]) * cnt[2 *  w + 1];
    return {pLeft.first + pRight.first, pLeft.second + pRight.second};
}

void unblockNodes() {
    for (int v : blocked) {
        int w = v;
        isBlocked[w] = false;
        w /= 2;
        while (w >= 1) {
            tree[w] = (!isBlocked[2 * w]) * tree[2 * w] + (!isBlocked[2 * w + 1]) * tree[2 *  w + 1];
            cnt[w] = (!isBlocked[2 * w]) * cnt[2 * w] + (!isBlocked[2 * w + 1]) * cnt[2 *  w + 1];
            w /= 2;
        }
    }
    blocked.clear();
}

void query(long long weight, long long stopWeight) {
    bool bad = false;
    int steps = 0;
    while (weight < stopWeight) {
        int low = 0;
        int high = szprots.size() - 1;
        int res = -1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (szprots[mid].first < weight) {
                res = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        if (res == -1) {
            bad = true;
            break;
        }
        long long nextSzprot = res == (int)szprots.size() - 1 ? stopWeight : (szprots[res + 1].first + 1);
        long long nextWeight = min(nextSzprot, stopWeight);
        pair<long long, int> p = eatSzprots(1, 0, BASE - 1, res, nextWeight - weight);
        
        if (p.first < nextWeight - weight) {
            bad = true;
            break;
        }
        
        weight += p.first;
        steps += p.second;
    }
    unblockNodes();
    if (bad) {
        printf("-1\n");
    } else {
        printf("%d\n", steps);
    }
}

int main() {
    
    scanf("%d", &n);
    
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &szprot[i]);
        szprots.push_back({szprot[i], {0, i}});
    }
    
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
        scanf("%d", &type[i]);
        if (type[i] == 1) {
            scanf("%lld %lld", &start[i], &stop[i]);
        } else if (type[i] == 2) {
            scanf("%lld", &szprotQuery[i]);
            szprots.push_back({szprotQuery[i], {1, i}});
        } else {
            scanf("%lld", &szprotQuery[i]);
        }
    }
    
    sort(szprots.begin(), szprots.end());
    
    for (int i = 0; i < szprots.size(); i++) {
        if (i == 0 || szprots[i].first != szprots[i - 1].first) {
            firstSzprot[szprots[i].first] = i;
        }
        if (szprots[i].second.first == 0) {
            smallId[szprots[i].second.second] = i;
        } else {
            smallIdQuery[szprots[i].second.second] = i;
        }
    }
    
    for (int i = 1; i <= n; i++) {
        insert(smallId[i], szprot[i]);
    }
    
    for (int i = 1; i <= q; i++) {
        if (type[i] == 1) {
            query(start[i], stop[i]);
        } else if (type[i] == 2) {
            insert(smallIdQuery[i], szprotQuery[i]);
        } else {
            insert(firstSzprot[szprotQuery[i]], -szprotQuery[i]);
            firstSzprot[szprotQuery[i]]++;
        }
    }
    
    return 0;
}