/* Problem URL: https://olimpiada.ic.unicamp.br/pratique/p2/2015/f2/fila/ */ #include using namespace std; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (size_t i = 0; i < (lim); i++) #define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } struct insertion { int pos; int val; insertion(int pos, int val): pos(pos), val(val) {} // bool operator<(insertion &i) { // if (pos == i.pos) { // return pos == 0 ? ind < i.pos : pos > i.pos; // } // return ind < i.ind; // } }; struct query { int type; int pos; ll val; query() : type(0), pos(0), val(0) {} query(int type, int pos, ll val): type(type), pos(pos), val(val) {} }; vl segtree; vl bit; ll search(int n, int l, int r, int lim, int v) { if (segtree[n] <= v || l >= lim) { return 0; } if (l == r) { return l; } int mid = (l + r) / 2; ll ans = segtree[n * 2 + 1] > v ? search(n * 2 + 1, mid + 1, r, lim, v) : 0; return ans ?: search(n * 2, l, mid, lim, v); } void add(int i) { for (; i < bit.size(); i += (i & -i)) { bit[i]++; } } ll get(int i) { ll ans = 0; for (; i >= 1; i -= (i & -i)) { ans += bit[i]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vl queue(n); cin >> queue; int q; cin >> q; V queries(q); V inserts; rep(i, q) { int type, pos; ll value; cin >> type >> pos >> value; queries[i] = query(type, pos, value); if (type == 0) { inserts.emplace_back(pos, value); } } reverse(queries.begin(), queries.end()); reverse(inserts.begin(), inserts.end()); vl act(n + inserts.size(), -1); bit.assign(n + inserts.size() + 1, 0); for (auto [p, v] : inserts) { } // size_t now = 0; // for (size_t i = 0; i < act.size(); i++) { // if (act[i] == -1) { // act[i] = queue[now]; // now++; // } // } size_t bef = act.size(); while (__builtin_popcount(act.size()) != 1) { act.push_back(-1); } size_t actn = act.size(); cout << act; segtree.resize(act.size() * 2); bit.resize(act.size() + 1); for (size_t i = actn; i < actn * 2; i++) { segtree[i] = act[i - actn]; } for (size_t i = actn - 1; i > 0; i--) { segtree[i] = max(segtree[i * 2], segtree[i * 2 + 1]); } vl ans(queries.size() - inserts.size()); auto itr = ans.rbegin(); for (auto [type, pos, value] : queries) { if (type == 0) { int fix = get(pos + 1); // cout << "Remove fix: " << fix << '\n'; segtree[pos + actn + fix] = -1; add(pos + fix + 1); for (size_t i = (pos + actn + fix) / 2; i >= 1; i /= 2) { segtree[i] = max(segtree[i * 2], segtree[i * 2 + 1]); } continue; } int fix = get(pos); // cout << "Query fix: " << fix << '\n'; int ans = search(1, 1, bef, pos + fix, act[pos + fix - 1] + value); // cout << "Found: " << ans << " with: " << act[pos + fix - 1] << '\n'; *itr = ans ? ans - get(ans) : 0; itr++; } for (auto i : ans) { cout << i << '\n'; } }