Advertisement
Guest User

Untitled

a guest
Aug 1st, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ar2 = array<int, 2>;
  5. using i64 = long long;
  6. #define sz(v) int(v.size())
  7.  
  8. struct T {
  9. vector<int> t, v;
  10. void init(int n) {
  11. t.resize(n, 0);
  12. v.resize(n, 0);
  13. }
  14. void add(int i, int x) {
  15. v[i] += x;
  16. while (i < sz(t)) {
  17. t[i] += x;
  18. i |= i + 1;
  19. }
  20. }
  21. int sum(int i) {
  22. int x = 0;
  23. while (i >= 0) {
  24. x += t[i];
  25. i &= i + 1, i--;
  26. }
  27. return x;
  28. }
  29. int find(int k) {
  30. int i = 0;
  31. for (int p = 1 << 18; p; p >>= 1) {
  32. if (i + p <= sz(t) && t[i + p - 1] < k) {
  33. i += p;
  34. k -= t[i - 1];
  35. }
  36. }
  37. return i;
  38. }
  39. int next(int i) {
  40. while (!v[i]) {
  41. i++;
  42. }
  43. return i;
  44. }
  45. };
  46.  
  47. struct D {
  48. i64 a; ar2 b;
  49. D operator+(const D &o) const {
  50. return {a + o.a, ar2{b[0] + o.b[0], b[1] + o.b[1]}};
  51. }
  52. void app(int t, i64 v) {
  53. a += v * b[t];
  54. }
  55. };
  56.  
  57. int main() {
  58. ios::sync_with_stdio(0);
  59. cin.tie(0);
  60. int n, q;
  61. cin >> n >> q;
  62. int size[2];
  63. T ds[2];
  64. for (int t = 0; t < 2; t++) {
  65. size[t] = n;
  66. ds[t].init(n + 1);
  67. for (int i = 1; i <= n; i++) {
  68. ds[t].add(i, 1);
  69. }
  70. }
  71. vector<ar2> x(n);
  72. vector<array<D, 2>> d(n);
  73. auto cyc = [&](int i) {
  74. return x[i][0] == 0 && x[i][1] == 0;
  75. };
  76. struct info {
  77. bool c[2][2];
  78. pair<ar2, D> t[2][2];
  79. i64 lz[2];
  80. info() {
  81. lz[0] = lz[1] = 0;
  82. for (int i = 0; i < 2; i++) {
  83. for (int j = 0; j < 2; j++) {
  84. c[i][j] = 0;
  85. t[i][j] = {ar2{0, 0}, D{0, ar2{0, 0}}};
  86. }
  87. }
  88. }
  89. };
  90. vector<info> tr(n * 4);
  91. int who = -1;
  92. auto pul = [&](int k, int l, int r) {
  93. if (r - l + 1 == 1) {
  94. return;
  95. }
  96. if (r - l + 1 <= 3) {
  97. for (int j = 0; j < 2; j++) {
  98. for (int t = 0; t < 2; t++) {
  99. ar2 u = {l + j, t};
  100. D z{0, ar2{0, 0}};
  101. while (u[0] <= r && !cyc(u[0])) {
  102. z = z + d[u[0]][u[1]];
  103. u[0] += x[u[0]][u[1]];
  104. u[1] ^= 1;
  105. }
  106. if (u[0] <= r) {
  107. tr[k].c[j][t] = 1;
  108. } else {
  109. tr[k].c[j][t] = 0;
  110. tr[k].t[j][t] = {ar2{u[0] - r - 1, u[1]}, z};
  111. }
  112. }
  113. }
  114. } else {
  115. for (int j = 0; j < 2; j++) {
  116. for (int t = 0; t < 2; t++) {
  117. if (tr[k * 2].c[j][t]) {
  118. tr[k].c[j][t] = 1;
  119. } else {
  120. auto [j_, t_] = tr[k * 2].t[j][t].first;
  121. tr[k].c[j][t] = tr[k * 2 + 1].c[j_][t_];
  122. if (!tr[k].c[j][t]) {
  123. tr[k].t[j][t].first = tr[k * 2 + 1].t[j_][t_].first;
  124. tr[k].t[j][t].second = tr[k * 2].t[j][t].second + tr[k * 2 + 1].t[j_][t_].second;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. };
  131. auto put = [&](int k, int l, int r, int t, i64 v) {
  132. if (r - l + 1 <= 3) {
  133. for (int i = l; i <= r; i++) {
  134. if (i == who) {
  135. continue;
  136. }
  137. d[i][0].app(t, v);
  138. d[i][1].app(t, v);
  139. }
  140. }
  141. tr[k].lz[t] += v;
  142. for (int a = 0; a < 2; a++) {
  143. for (int b = 0; b < 2; b++) {
  144. tr[k].t[a][b].second.app(t, v);
  145. }
  146. }
  147. };
  148. auto pus = [&](int k, int l, int r) {
  149. for (int t = 0; t < 2; t++) {
  150. if (tr[k].lz[t]) {
  151. int m = (l + r) / 2;
  152. put(k * 2, l, m, t, tr[k].lz[t]);
  153. put(k * 2 + 1, m + 1, r, t, tr[k].lz[t]);
  154. tr[k].lz[t] = 0;
  155. }
  156. }
  157. };
  158. auto bld = [&](auto bld, int k, int l, int r) -> void {
  159. if (r - l + 1 <= 3) {
  160. pul(k, l, r);
  161. } else {
  162. int m = (l + r) / 2;
  163. bld(bld, k * 2, l, m);
  164. bld(bld, k * 2 + 1, m + 1, r);
  165. pul(k, l, r);
  166. }
  167. };
  168. auto upd = [&](auto upd, int k, int l, int r, int i) -> void {
  169. if (r - l + 1 <= 3) {
  170. pul(k, l, r);
  171. } else {
  172. int m = (l + r) / 2;
  173. pus(k, l, r);
  174. i <= m ? upd(upd, k * 2, l, m, i) : upd(upd, k * 2 + 1, m + 1, r, i);
  175. pul(k, l, r);
  176. }
  177. };
  178. auto qry = [&](auto qry, int k, int l, int r, ar2 &u) -> i64 {
  179. assert(u[0] >= l && u[0] <= r);
  180. if (r - l + 1 <= 3) {
  181. i64 z = 0;
  182. while (u[0] <= r && !cyc(u[0])) {
  183. z += d[u[0]][u[1]].a;
  184. u[0] += x[u[0]][u[1]];
  185. u[1] ^= 1;
  186. }
  187. if (u[0] <= r) {
  188. return -1;
  189. } else {
  190. return z;
  191. }
  192. }
  193. if (u[0] == l || u[0] == l + 1) {
  194. if (tr[k].c[u[0] - l][u[1]]) {
  195. return -1;
  196. }
  197. const auto &x = tr[k].t[u[0] - l][u[1]];
  198. u = ar2{r + 1 + x.first[0], x.first[1]};
  199. return x.second.a;
  200. } else {
  201. int m = (l + r) / 2;
  202. pus(k, l, r);
  203. if (u[0] <= m) {
  204. i64 z = qry(qry, k * 2, l, m, u);
  205. if (z == -1) {
  206. return -1;
  207. }
  208. i64 z_ = qry(qry, k * 2 + 1, m + 1, r, u);
  209. if (z_ == -1) {
  210. return -1;
  211. }
  212. return z + z_;
  213. } else {
  214. return qry(qry, k * 2 + 1, m + 1, r, u);
  215. }
  216. }
  217. };
  218. auto add = [&](auto add, int k, int l, int r, int ql, int qr, int t, int v) -> void {
  219. if (r - l + 1 <= 3) {
  220. for (int i = max(ql, l); i <= min(qr, r); i++) {
  221. d[i][0].app(t, v);
  222. d[i][1].app(t, v);
  223. }
  224. pul(k, l, r);
  225. return;
  226. }
  227. if (ql <= l && qr >= r) {
  228. put(k, l, r, t, v);
  229. } else {
  230. int m = (l + r) / 2;
  231. pus(k, l, r);
  232. if (ql <= m) {
  233. add(add, k * 2, l, m, ql, qr, t, v);
  234. }
  235. if (qr > m) {
  236. add(add, k * 2 + 1, m + 1, r, ql, qr, t, v);
  237. }
  238. pul(k, l, r);
  239. }
  240. };
  241. auto calc = [&](int i, bool u = 0) {
  242. if (i <= 0 || i >= n) {
  243. return;
  244. }
  245. who = i;
  246. for (int t = 0; t < 2; t++) {
  247. int j = ds[t].next(i);
  248. if (i == j) {
  249. x[i][t] = ds[t].next(j + 1) - j;
  250. d[i][t] = D{2 * ds[t].sum(j) + 1, ar2{(t == 0) * 2, (t == 1) * 2}};
  251. } else {
  252. x[i][t] = 0;
  253. d[i][t] = D{2 * ds[t].sum(j) - 1, ar2{(t == 0) * 2, (t == 1) * 2}};
  254. }
  255. }
  256. if (u) {
  257. upd(upd, 1, 1, n - 1, i);
  258. }
  259. who = -1;
  260. };
  261. for (int i = 1; i < n; i++) {
  262. calc(i);
  263. }
  264. bld(bld, 1, 1, n - 1);
  265. while (q--) {
  266. int t, x;
  267. cin >> t >> x;
  268. if (t == 1) {
  269. if (x >= n) {
  270. cout << size[1] + 1 << '\n';
  271. } else {
  272. ar2 u{x, 1};
  273. i64 z = qry(qry, 1, 1, n - 1, u);
  274. if (z == -1) {
  275. cout << "-1\n";
  276. } else {
  277. assert(u[0] == n);
  278. z += size[u[1]];
  279. cout << z + 1 << '\n';
  280. }
  281. }
  282. } else if (t == 2) {
  283. assert(x != size[0]);
  284. if (x <= size[0]) {
  285. t = 0;
  286. x = size[0] - x;
  287. } else {
  288. x -= size[0];
  289. assert(x != size[1]);
  290. t = 1;
  291. }
  292. int p = ds[t].find(x);
  293. assert(p >= 1);
  294. ds[t].add(p, -1);
  295. add(add, 1, 1, n - 1, p, n - 1, t, -1);
  296. size[t]--;
  297. calc(p, 1);
  298. calc(p - 1, 1);
  299. } else {
  300. if (x <= size[0]) {
  301. t = 0;
  302. x = size[0] - x + 1;
  303. } else {
  304. x -= size[0];
  305. t = 1;
  306. }
  307. int p = ds[t].find(x);
  308. assert(p >= 2);
  309. ds[t].add(p - 1, 1);
  310. add(add, 1, 1, n - 1, p - 1, n - 1, t, 1);
  311. size[t]++;
  312. calc(p - 1, 1);
  313. calc(p - 2, 1);
  314. }
  315. }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement