Advertisement
limimage

zalupka

Sep 4th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 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 = 1e5+5;
  10.  
  11. struct V
  12. {
  13. int val;
  14. V* l;
  15. V* r;
  16. V(int v = 0, V* a = nullptr, V* b = nullptr) :
  17. val(v), l(a), r(b){};
  18. };
  19. using pv = V*;
  20.  
  21. V buf[40*N];
  22. map<int, stack<int>> mp;
  23. pv ver[N], root = nullptr;
  24. int s = 0, n, val, ans, arr[N];
  25. string q;
  26.  
  27. pv newV(int v = 0, pv l = nullptr, pv r = nullptr)
  28. {
  29. buf[s] = V(v, l, r);
  30. return &buf[s++];
  31. }
  32.  
  33. int getans(pv v)
  34. {
  35. return v == nullptr ? 0 : v->val;
  36. }
  37.  
  38. pv upd(int pos, int val, pv v = root, int l = 0, int r = n)
  39. {
  40. if (l+1 == r)
  41. return newV(val);
  42. int m = (l+r)>>1;
  43. pv tl = v == nullptr ? nullptr : v->l,
  44. tr = v == nullptr ? nullptr : v->r;
  45. if (pos < m)
  46. tl = upd(pos, val, tl, l, m);
  47. else tr = upd(pos, val, tr, m, r);
  48. return newV(getans(tl) + getans(tr), tl, tr);
  49. }
  50.  
  51. int get (int val, pv v, int l = 0, int r = n)
  52. {
  53. if (l+1 == r)
  54. return l + val - getans(v);
  55. int m = (l+r)>>1;
  56. pv tl = v == nullptr ? nullptr : v->l,
  57. tr = v == nullptr ? nullptr : v->r;
  58. if (getans(tl) >= val)
  59. return get(val, tl, l, m);
  60. else
  61. return get(val - getans(tl), tr, m, r);
  62. }
  63.  
  64. void Solve()
  65. {
  66. cin >> n;
  67. for (int i = 0; i < n; i++)
  68. {
  69. cin >> arr[i];
  70. mp[arr[i]].push(i);
  71. }
  72. for (int i = n - 1; i >= 0; i--)
  73. {
  74. upd(i, 1);
  75. if (i != mp[arr[i]].top())
  76. upd(mp[arr[i]].top(), 0), mp[arr[i]].pop();
  77. ver[i] = root;
  78. }
  79. for (int i = 1; i <= n; i++)
  80. {
  81. val = get(i, ver[1]);
  82. ans = 1;
  83. while(val < n)
  84. {
  85. val = get(i, ver[val+1]);
  86. ans++;
  87. }
  88. cout << ans << " ";
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. ios::sync_with_stdio(false);
  95. cin.tie(nullptr);
  96. cout.tie(nullptr);
  97. Solve();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement