Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #pragma comment(linker, "/STACK:251700000")
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <string>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <cstdio>
  10. #include <fstream>
  11. #include <unordered_map>
  12. #include <map>
  13. #include <iterator>
  14. #include <iomanip>
  15. #include <stack>
  16. #include <math.h>
  17. #include <bitset>
  18. #include <assert.h>
  19. //( ° ͜ʖ͡°)╭∩╮
  20.  
  21. using namespace std;
  22.  
  23. typedef long long ll;
  24. typedef pair<int, int> pii;
  25. typedef unsigned long long ull;
  26. typedef unsigned int ui;
  27. typedef vector<int> vi;
  28.  
  29. #define TASK "clover"
  30. #define mp make_pair
  31. #define X first
  32. #define Y second
  33. #define all(a) (a).begin(), (a).end()
  34. #define inb push_back
  35. #define y1 lubluAU
  36.  
  37. const ll LINF = 9e18;
  38. const int INF = 2e9;
  39.  
  40. #ifdef _DEBUG
  41. const int M = 2e2 + 5;
  42. #else
  43. const int M = (1 << 18);
  44. #endif
  45.  
  46. struct dd
  47. {
  48. int x, y, cnt, min;
  49. dd * l, *r;
  50. dd() {}
  51. dd(int x)
  52. {
  53. this->x = x;
  54. y = rand() << 15 + rand();
  55. cnt = min = x;
  56. l = r = NULL;
  57. }
  58. };
  59.  
  60. typedef dd * node;
  61.  
  62. int getc(node t)
  63. {
  64. if (t) return t->cnt;
  65. return 0;
  66. }
  67.  
  68. int getm(node t)
  69. {
  70. if (t) return t->min;
  71. return INF;
  72. }
  73.  
  74. void upd(node t)
  75. {
  76. if (!t) return;
  77. t->cnt = 1 + getc(t->l) + getc(t->r);
  78. }
  79.  
  80. void updm(node t)
  81. {
  82. if (!t) return;
  83. t->min = min({ t->x, getm(t->l), getm(t->r) });
  84. }
  85.  
  86. void split(node t, int x, node &l, node &r)
  87. {
  88. if (!t)
  89. {
  90. l = r = NULL;
  91. return;
  92. }
  93. if (x <= getc(t->l))
  94. split(t->l, x, l, t->l), r = t;
  95. else
  96. split(t->r, x - getc(t->l) - 1, t->r, r), l = t;
  97. upd(t);
  98. updm(t);
  99. }
  100.  
  101. void merge(node &t, node l, node r)
  102. {
  103. if (!l || !r) t = l ? l : r;
  104. else if (l->y > r->y)
  105. merge(l->r, l->r, r), t = l;
  106. else
  107. merge(r->l, l, r->l), t = r;
  108. upd(t);
  109. updm(t);
  110. }
  111.  
  112. void ins(node &t, int x, int pos)
  113. {
  114. node a, b, c, d;
  115. split(t, pos, a, b);
  116. split(b, 1, c, d);
  117. c = new dd(x);
  118. merge(b, c, d);
  119. merge(t, a, b);
  120. }
  121.  
  122. void insert(node &t, int x, int l, int r)
  123. {
  124. node a, b, c, d, e, f;
  125. split(t, l, a, b);
  126. split(b, r - l, c, d);
  127. split(d, 1, e, f);
  128. e = new dd(x);
  129. merge(a, a, e);
  130. merge(b, c, f);
  131. merge(t, a, b);
  132. }
  133.  
  134. void got(node t, int &x)
  135. {
  136. if (!t) return;
  137. if (!getm(t->l)) got(t->l, x);
  138. else
  139. {
  140. x += getc(t->l) + 1;
  141. if (t->x) got(t->r, x);
  142. }
  143. }
  144.  
  145. int Find(node &t, int pos)
  146. {
  147. node a, b;
  148. split(t, pos, a, b);
  149. int ans = 0;
  150. got(b, ans);
  151. merge(t, a, b);
  152. return pos + ans - 1;
  153. }
  154.  
  155. vi ans;
  156.  
  157. void print(node t)
  158. {
  159. if (!t) return;
  160. print(t->l);
  161. ans.inb(t->x);
  162. print(t->r);
  163. }
  164.  
  165. int main()
  166. {
  167. #ifdef _DEBUG
  168. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  169. #else
  170. //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  171. //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  172. #endif
  173. /*ios_base::sync_with_stdio(0);
  174. cin.tie(0);
  175. cout.tie(0);*/
  176. int n, m;
  177. scanf("%d%d", &n, &m);
  178. vi l(n);
  179. for (int i = 0; i < n; ++i)
  180. scanf("%d", &l[i]), l[i]--;
  181. node root = NULL;
  182. for (int i = 0; i <= max(2 * n, 2 * m); ++i) merge(root, root, new dd(0));
  183. for (int i = 0; i < n; ++i)
  184. {
  185. int x = Find(root, l[i]);
  186. if (x == l[i])
  187. ins(root, i + 1, l[i]);
  188. else
  189. insert(root, i + 1, l[i], x);
  190. }
  191. ans.clear();
  192. print(root);
  193. while (ans.back() == 0) ans.pop_back();
  194. cout << ans.size() << '\n';
  195. for (int i : ans) cout << i << ' ';
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement