Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5. #include <algorithm>
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <cassert>
  10.  
  11. using namespace std;
  12.  
  13. #define pb push_back
  14. #define re return
  15.  
  16. const int N = 1e5 + 1;
  17.  
  18. struct node
  19. {
  20. int mx, cnt;
  21. };
  22.  
  23. vector<node> t(4 * N);
  24.  
  25. node chto(node a, node b)
  26. {
  27. if (a.mx == b.mx)
  28. {
  29. node c;
  30. c.mx = a.mx;
  31. c.cnt = a.cnt + b.cnt;
  32.  
  33. return c;
  34. }
  35. else if(a.mx > b.mx)
  36. {
  37. return a;
  38. }
  39. else
  40. {
  41. return b;
  42. }
  43. }
  44.  
  45. void upd(int v, int l, int r, int pos, node x)
  46. {
  47. if (pos < l || pos >= r)
  48. {
  49. return;
  50. }
  51.  
  52. if (l + 1 == r && pos == l)
  53. {
  54. if (t[v].mx == x.mx)
  55. {
  56. t[v].cnt += x.cnt;
  57. }
  58. else
  59. {
  60. t[v] = x;
  61. }
  62. }
  63. else
  64. {
  65. int m = (l + r) / 2;
  66.  
  67. upd(v * 2, l, m, pos, x);
  68. upd(v * 2 + 1, m, r, pos, x);
  69.  
  70. t[v] = chto(t[2 * v], t[2 * v + 1]);
  71. }
  72. }
  73.  
  74. node get(int v, int l, int r, int L, int R)
  75. {
  76. if (l >= R || L >= r)
  77. {
  78. node c;
  79. c.mx = 0;
  80. c.cnt = 0;
  81.  
  82. return c;
  83. }
  84.  
  85. if (l >= L && r <= R)
  86. {
  87. return t[v];
  88. }
  89.  
  90. int m = (l + r) / 2;
  91. return chto(get(v * 2, l, m, L, R), get(v * 2 + 1, m, r, L, R));
  92. }
  93.  
  94. bool comp(pair<int, int> a, pair<int, int> b)
  95. {
  96. return a.first < b.first;
  97. }
  98.  
  99. int main()
  100. {
  101. int n;
  102. cin >> n;
  103.  
  104. vector<pair<int, int>> v(n);
  105.  
  106. for (int i = 0; i < n; ++i)
  107. {
  108. cin >> v[i].first;
  109. v[i].second = i;
  110. }
  111.  
  112. vector<pair<int, int>> v2 = v;
  113.  
  114. sort(v2.begin(), v2.end(), comp);
  115.  
  116. int now = 1;
  117. v[v2[0].second].first = now;
  118.  
  119. for (int i = 1; i < n; ++i)
  120. {
  121. if (v2[i].first == v2[i - 1].first)
  122. {
  123. v[v2[i].second].first = now;
  124. }
  125. else
  126. {
  127. ++now;
  128. v[v2[i].second].first = now;
  129. }
  130. }
  131.  
  132. for (int i = 0; i < n; ++i)
  133. {
  134. node ans = get(1, 0, N, 1, v[i].first);
  135. ans.mx += 1;
  136. ans.cnt = max(ans.cnt, 1);
  137.  
  138. upd(1, 0, N, v[i].first, ans);
  139. }
  140.  
  141. cout << t[1].cnt << endl;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement