Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <algorithm>
- #include <utility>
- #include <vector>
- using namespace std;
- const int MAXN = 100005;
- const int POP_CODE = 1;
- const int PUSH_CODE = 2;
- struct state {
- int num_pops;
- vector<int> to_push;
- };
- int n;
- state tree[4 * MAXN];
- void print_node(int node) {
- /*
- printf("Node %d has %d pops and vector is of size %d\n", node, tree[node].num_pops, (int)tree[node].to_push.size());
- for (int x : tree[node].to_push) {
- printf("%d ", x);
- }
- printf("\n");
- */
- }
- void merge(int dst, int src1, int src2) {
- // printf("Merging %d\n", dst);
- print_node(src1);
- print_node(src2);
- tree[dst].num_pops = tree[src1].num_pops;
- int res_size = tree[src1].to_push.size() - tree[src2].num_pops;
- // printf("Res size: %d\n", res_size);
- tree[dst].to_push.clear();
- if (res_size <= 0) {
- tree[dst].num_pops -= res_size;
- } else {
- for (int i = 0; i < res_size; ++i) {
- tree[dst].to_push.push_back(tree[src1].to_push[i]);
- }
- }
- for (int x : tree[src2].to_push) {
- tree[dst].to_push.push_back(x);
- }
- print_node(dst);
- }
- void update(int node, int l, int r, int target, int opcode, int val) {
- if (l == r and l == target) {
- if (opcode == POP_CODE) {
- tree[node].num_pops++;
- } else {
- tree[node].to_push.push_back(val);
- }
- } else {
- int mid = (l + r) / 2;
- if (target <= mid) {
- update(2 * node + 1, l, mid, target, opcode, val);
- } else {
- update(2 * node + 2, mid + 1, r, target, opcode, val);
- }
- merge(node, 2 * node + 1, 2 * node + 2);
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- scanf("%d", &n);
- int p, t, x;
- for (int i = 0; i < n; ++i) {
- scanf("%d %d", &p, &t);
- --p;
- if (t == 0) {
- update(0, 0, n - 1, p, POP_CODE, -1);
- } else {
- scanf("%d", &x);
- update(0, 0, n - 1, p, PUSH_CODE, x);
- }
- if (tree[0].to_push.size() == 0) {
- printf("-1\n");
- } else {
- printf("%d\n", tree[0].to_push.back());
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement