Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node {
  5. int y, value, cnt;
  6.  
  7. node *l, *r;
  8.  
  9. node(int value): value(value), l(nullptr), r(nullptr), y(rand() * rand()), cnt(1)
  10. {}
  11.  
  12. ~node() {
  13. if(l) delete(l);
  14. if(r) delete(r);
  15. }
  16. };
  17.  
  18. int cnt(node* t) {
  19. return t? t->cnt : 0;
  20. }
  21.  
  22. void upd_cnt(node*& t) {
  23. if(!t) return;
  24. t->cnt = cnt(t->l) + cnt(t->r) + 1;
  25. }
  26.  
  27. pair<node*, node*> split(node*& t, int x) {
  28. if(!t) return {nullptr, nullptr};
  29. int cur_key = cnt(t->l) + 1;
  30. if(cur_key < x) {
  31. auto res = split(t->r, x - cur_key);
  32. t->r = res.first;
  33. upd_cnt(t); upd_cnt(res.second);
  34. return {t, res.second};
  35. } else {
  36. auto res = split(t->l, x);
  37. t->l = res.second;
  38. upd_cnt(t); upd_cnt(res.second);
  39. return {res.first, t};
  40. }
  41. }
  42.  
  43. node* merge(node* l, node* r) {
  44. if(!l || !r) return l? l : r;
  45.  
  46. if(l->y > r->y) {
  47. l->r = merge(l->r, r);
  48. upd_cnt(l);
  49. return l;
  50. } else {
  51. r->l = merge(l, r->l);
  52. upd_cnt(r);
  53. return r;
  54. }
  55. }
  56.  
  57. int exist(node* t, int x, int add = 0) {
  58. if(!t) return false;
  59. int cur_x = add + cnt(t->l);
  60. if(cur_x == x) return t->value;
  61.  
  62. if(cur_x < x) return exist(t->r, x, add + cnt(t->l) + 1);
  63. else return exist(t->l, x, add);
  64. }
  65.  
  66. void insert(node*& t, node*& n, int idx) {
  67. auto res = split(t, idx);
  68. t = merge(merge(res.first, n), res.second);
  69. upd_cnt(t);
  70. }
  71.  
  72. void erase(node*&t, int idx, int add = 0) {
  73. if(!t) return;
  74. int cur_key = add + cnt(t->l);
  75. if(cur_key == idx) {
  76. t = merge(t->l, t->r);
  77. upd_cnt(t);
  78. return;
  79. }
  80.  
  81. if(cur_key < idx) erase(t->r, idx, add + cnt(t->l) + 1);
  82. else erase(t->l, idx, add);
  83. upd_cnt(t);
  84. }
  85.  
  86. struct query {
  87. int val, idx;
  88.  
  89. };
  90.  
  91. int to_start = 0;
  92. int used[1000001];
  93. int ans[1000001];
  94. query q[1000001];
  95.  
  96. int main() {
  97. srand(time(0));
  98. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  99. node* root = nullptr;
  100. int N, m;
  101. cin >> N >> m;
  102. for(int i = 0; i < m; i++) {
  103. cin >> q[i].val >> q[i].idx;
  104. q[i].idx--;
  105. }
  106.  
  107. for(int i = 0; i < N; i++) {
  108. node* n = new node(0);
  109. insert(root, n, 1);
  110. }
  111.  
  112. for(int i = 0; i < m; i++) {
  113. int current = exist(root, q[i].idx);
  114. if(current && current != q[i].val) {
  115. cout << -1;
  116. return 0;
  117. }
  118.  
  119. erase(root, q[i].idx);
  120. node* n = new node(q[i].val);
  121. insert(root, n, 1);
  122. }
  123.  
  124.  
  125. for(int i = m-1; i >= 0; i--) {
  126. node* n = new node(exist(root, 0));
  127. erase(root, 0);
  128. insert(root, n, q[i].idx + 1);
  129. }
  130.  
  131. for(int i = 0; i < N; i++) {
  132. int j = exist(root, i);
  133. if(used[j] == true) {
  134. cout << -1;
  135. return 0;
  136. }
  137. if(j) used[j] = true;
  138. }
  139. int pos = 1;
  140. for(int i = 0; i < N; i++) {
  141. int cur = exist(root, i);
  142. if(!cur) {
  143. while(used[pos]) pos++;
  144. cur = pos++;
  145. }
  146.  
  147. ans[i] = cur;
  148. }
  149.  
  150. for(int i = 0; i < N; i++) cout << ans[i] << " ";
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement