Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <list>
  10. #include <deque>
  11. #include <memory>
  12. #include <utility>
  13. #include <queue>
  14. #include <math.h>
  15. #include <ctime>
  16. #include <random>
  17. #include <functional>
  18.  
  19. using namespace std;
  20.  
  21. const int n = 100050;
  22. const int bs = 316;
  23.  
  24. struct vertex {
  25. int left0;
  26. int left1;
  27. int qq;
  28. int sum;
  29. };
  30.  
  31. vector<int> v(n, 0);
  32. vector<vertex> block(n / bs + 2);
  33.  
  34. void updates(int x) {
  35. block[x].left0 = 1e9;
  36. block[x].left1 = 1e9;
  37. block[x].sum = 0;
  38. for (int i = x * bs; i < (x + 1) * bs; i++) {
  39. if (v[i]) {
  40. block[x].sum++;
  41. block[x].left1 = min(block[x].left1, i);
  42. }
  43. if (!v[i]) {
  44. block[x].left0 = min(block[x].left0, i);
  45. }
  46. }
  47. }
  48.  
  49. void update(int x) {
  50. if (block[x].qq >= 0) {
  51. for (int i = x * bs; i < (x + 1) * bs; i++)
  52. v[i] = block[x].qq;
  53. if (block[x].qq)
  54. block[x].sum = bs;
  55.  
  56. block[x].qq = -1;
  57. }
  58. }
  59.  
  60.  
  61. int ff0(int l) {
  62. int ans = 1e9;
  63. update(l / bs);
  64. for (int i = l; i < ((l / bs) + 1) * bs; i++) {
  65. if (!v[i]) {
  66. ans = i;
  67. break;
  68. }
  69. }
  70. if (ans != 1e9)
  71. return ans;
  72. for (int i = (l / bs) + 1; i < block.size(); i++) {
  73. if (block[i].left0 != 1e9) {
  74. ans = block[i].left0;
  75. return ans;
  76. }
  77. }
  78. }
  79.  
  80. int ff1(int l) {
  81. int ans = 1e9;
  82. update(l / bs);
  83. for (int i = l; i < ((l / bs) + 1) * bs; i++) {
  84. if (v[i]) {
  85. ans = i;
  86. break;
  87. }
  88. }
  89. if (ans != 1e9)
  90. return ans;
  91. for (int i = (l / bs) + 1; i < block.size(); i++) {
  92. if (block[i].left1 != 1e9) {
  93. ans = block[i].left1;
  94. return ans;
  95. }
  96. }
  97. }
  98.  
  99. void set_smth(int L, int R, int q) {
  100. update(L / bs);
  101. for (int i = L; i <= min(((L / bs) + 1) * bs - 1, R); i++) {
  102. v[i] = q;
  103. }
  104. updates(L / bs);
  105.  
  106. update(R / bs);
  107. for (int i = max(L, (R / bs) * bs); i <= R; i++) {
  108. v[i] = q;
  109. }
  110. updates(R / bs);
  111. for (int i = (L / bs) + 1; i < R / bs; i++) {
  112. if (!q) { // 0
  113. block[i].qq = q;
  114. block[i].left1 = 1e9;
  115. block[i].left0 = i * bs;
  116. block[i].sum = 0;
  117. }
  118. else {
  119. block[i].qq = q;
  120. block[i].left1 = i * bs;
  121. block[i].left0 = 1e9;
  122. block[i].sum = bs;
  123. }
  124. }
  125. }
  126.  
  127.  
  128. int get_sum() {
  129. int ans = 0;
  130. for (auto f: block) {
  131. ans += f.sum;
  132. }
  133. return ans;
  134. }
  135.  
  136. int main() {
  137. ios_base::sync_with_stdio(0);
  138. cin.tie(0);
  139. for (int i = 0; i < block.size(); i++) {
  140. block[i].left0 = i * bs;
  141. block[i].left1 = 1e9;
  142. }
  143. int qqq;
  144. cin >> qqq;
  145. for (int zzz = 0; zzz < qqq;zzz++) {
  146. string s;
  147. int x;
  148. cin >> s >> x;
  149. if (s == "add") {
  150. int u = ff0(x);
  151. set_smth(x, u, 0);
  152. set_smth(u, u, 1);
  153. cout << get_sum() << '\n';
  154. }
  155. else {
  156. int u = ff1(x);
  157. set_smth(x, u, 1);
  158. set_smth(u, u, 0);
  159. cout << get_sum() << '\n';
  160. }
  161. }
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement