Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int MAXN = 100005;
  11. const int POP_CODE = 1;
  12. const int PUSH_CODE = 2;
  13.  
  14. struct state {
  15.     int num_pops;
  16.     vector<int> to_push;
  17. };
  18.  
  19. int n;
  20. state tree[4 * MAXN];
  21.  
  22. void print_node(int node) {
  23.     /*
  24.     printf("Node %d has %d pops and vector is of size %d\n", node, tree[node].num_pops, (int)tree[node].to_push.size());
  25.     for (int x : tree[node].to_push) {
  26.         printf("%d ", x);
  27.     }
  28.     printf("\n");
  29.     */
  30. }
  31.  
  32. void merge(int dst, int src1, int src2) {
  33.     // printf("Merging %d\n", dst);
  34.     print_node(src1);
  35.     print_node(src2);
  36.     tree[dst].num_pops = tree[src1].num_pops;
  37.  
  38.     int res_size = tree[src1].to_push.size() - tree[src2].num_pops;
  39.     // printf("Res size: %d\n", res_size);
  40.  
  41.     tree[dst].to_push.clear();
  42.     if (res_size <= 0) {
  43.         tree[dst].num_pops -= res_size;
  44.     } else {
  45.         for (int i = 0; i < res_size; ++i) {
  46.             tree[dst].to_push.push_back(tree[src1].to_push[i]);
  47.         }
  48.     }
  49.  
  50.     for (int x : tree[src2].to_push) {
  51.         tree[dst].to_push.push_back(x);
  52.     }
  53.  
  54.     print_node(dst);
  55. }
  56.  
  57. void update(int node, int l, int r, int target, int opcode, int val) {
  58.     if (l == r and l == target) {
  59.         if (opcode == POP_CODE) {
  60.             tree[node].num_pops++;
  61.         } else {
  62.             tree[node].to_push.push_back(val);
  63.         }
  64.     } else {
  65.         int mid = (l + r) / 2;
  66.         if (target <= mid) {
  67.             update(2 * node + 1, l, mid, target, opcode, val);
  68.         } else {
  69.             update(2 * node + 2, mid + 1, r, target, opcode, val);
  70.         }
  71.  
  72.         merge(node, 2 * node + 1, 2 * node + 2);
  73.     }
  74. }
  75.  
  76. int main() {
  77.     ios_base::sync_with_stdio(false);
  78.  
  79.     scanf("%d", &n);
  80.  
  81.     int p, t, x;
  82.     for (int i = 0; i < n; ++i) {
  83.         scanf("%d %d", &p, &t);
  84.         --p;
  85.         if (t == 0) {
  86.             update(0, 0, n - 1, p, POP_CODE, -1);
  87.         } else {
  88.             scanf("%d", &x);
  89.             update(0, 0, n - 1, p, PUSH_CODE, x);
  90.         }
  91.  
  92.         if (tree[0].to_push.size() == 0) {
  93.             printf("-1\n");
  94.         } else {
  95.             printf("%d\n", tree[0].to_push.back());
  96.         }
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement