Advertisement
limimage

task E

Feb 9th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 3e5 + 5;
  10.  
  11. struct Treap {
  12. struct Node {
  13. int val;
  14. int sz;
  15. int pri;
  16. Node *l, *r;
  17. Node(int val = 0) :
  18. val(val), sz(1),
  19. pri(rand() ^ (rand() << 16)),
  20. l(nullptr), r(nullptr){};
  21. };
  22.  
  23. using pN = Node*;
  24. using pNN = pair<pN, pN>;
  25.  
  26. Node buff[N];
  27. int st = 0;
  28. pN root = nullptr;
  29.  
  30. int getsz(pN v) {
  31. return v == nullptr ? 0: v -> sz;
  32. }
  33.  
  34. int getval(pN v) {
  35. return v == nullptr ? 0 : v -> val;
  36. }
  37.  
  38. pN newN(int v) {
  39. buff[st] = Node(v);
  40. return &buff[st++];
  41. }
  42.  
  43. void recalc(pN v) {
  44. v -> sz = getsz(v -> l) + getsz(v -> r) + 1;
  45. }
  46.  
  47. pN merge(pN l, pN r) {
  48. if (l == nullptr)
  49. return r;
  50. if (r == nullptr)
  51. return l;
  52. if (l -> pri < r -> pri) {
  53. l -> r = merge(l -> r, r);
  54. recalc(l);
  55. return l;
  56. }
  57. else {
  58. r -> l = merge(l, r -> l);
  59. recalc(r);
  60. return r;
  61. }
  62. }
  63.  
  64. pNN split(pN v, int k) {
  65. if (k == 0)
  66. return {nullptr, v};
  67. if (getsz(v -> l) >= k) {
  68. auto p = split(v -> l, k);
  69. v -> l = p.second;
  70. recalc(v);
  71. return {p.first, v};
  72. }
  73. else {
  74. auto p = split(v -> r, k - getsz(v -> l) - 1);
  75. v -> r = p.first;
  76. recalc(v);
  77. return {v, p.second};
  78. }
  79. }
  80.  
  81. void insert(int pos, int val) {
  82. auto p1 = split(root, pos - 1);
  83. auto v = newN(val);
  84. root = merge(p1.first, merge(v, p1.second));
  85. return;
  86. }
  87.  
  88. void erase(int pos) {
  89. auto p1 = split(root, pos - 1);
  90. auto p2 = split(p1.second, 1);
  91. root = merge(p1.first, p2.second);
  92. return;
  93. }
  94.  
  95. int get_ans(int pos) {
  96. auto p1 = split(root, pos - 1);
  97. auto p2 = split(p1.second, 1);
  98. int ans = getval(p2.first);
  99. root = merge(merge(p2.first, p1.first), p2.second);
  100. return ans;
  101. }
  102. } tr;
  103.  
  104. struct SegTree {
  105.  
  106. vector<int> tr;
  107.  
  108. SegTree(int n) {
  109. tr.resize(n << 2);
  110. }
  111.  
  112. void upd(int pos, int val, int i, int l, int r) {
  113. if (l + 1 == r) {
  114. tr[i] += val;
  115. return;
  116. }
  117. int mid = (l + r) >> 1;
  118. if (pos < mid)
  119. upd(pos, val, (i << 1) + 1, l, mid);
  120. else
  121. upd(pos, val, (i << 1) + 2, mid, r);
  122. tr[i] = (tr[(i << 1) + 1] + tr[(i << 1) + 2]);
  123. }
  124.  
  125. int get(int l, int r, int i, int cl, int cr) {
  126. if (l >= r)
  127. return 0;
  128. if (l == cl && r == cr)
  129. return tr[i];
  130. int mid = (cl + cr) >> 1;
  131. return
  132. get(l, min(mid, r), (i << 1) + 1, cl, mid) +
  133. get(max(l, mid), r, (i << 1) + 2, mid, cr);
  134. }
  135. };
  136.  
  137. int n, m, type;
  138. vector<int> temp[N];
  139. vector<int> vec, dop;
  140.  
  141. void Solve() {
  142. cin >> n >> m >> type;
  143. if (type == 2) {
  144. for (int i = 1; i <= m; i++)
  145. tr.insert(i, i);
  146. for (int i = 0, pos; i < n; i++) {
  147. cin >> pos;
  148. int a = tr.get_ans(pos);
  149. //assert(a > 0);
  150. cout << a << " ";
  151. }
  152. }
  153. else {
  154. for (int i = 0, v; i < n; i++) {
  155. cin >> v;
  156. vec.push_back(v);
  157. }
  158. dop = vec;
  159. reverse(dop.begin(), dop.end());
  160. for (int i = 1; i <= m; i++) {
  161. dop.push_back(i);
  162. }
  163. for (int i = 0; i < dop.size(); i++) {
  164. temp[dop[i]].push_back(i);
  165. }
  166. SegTree t(dop.size());
  167. for (int i = vec.size(); i < dop.size(); i++)
  168. t.upd(i, 1, 0, 0, dop.size());
  169. for (int i: vec) {
  170. int a = t.get(0, temp[i].back() + 1, 0, 0, dop.size());
  171. assert(a > 0);
  172. cout << a << ' ';
  173. t.upd(temp[i].back(), -1, 0, 0, dop.size());
  174. temp[i].pop_back();
  175. t.upd(temp[i].back(), 1, 0, 0, dop.size());
  176. }
  177. }
  178. }
  179.  
  180. int main(){
  181. ios::sync_with_stdio(false);
  182. cin.tie(nullptr);
  183. cout.tie(nullptr);
  184. //auto start = chrono::high_resolution_clock::now();
  185. Solve();
  186. //auto end = chrono::high_resolution_clock::now();
  187. //cout << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  188. return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement