bhupixb

LIS using Segment Tree

Jan 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // I can't tell you what it really is,
  2. // I can only tell you what it feels like.
  3. #include "bits/stdc++.h"
  4.  
  5. using namespace std;
  6.  
  7. #define int long long
  8. #define F first
  9. #define S second
  10. #define rep(i,a,n) for (int i = a; i <= n; ++i)
  11. #define fo(i, n) for (int i = 1; i <= n; ++i)
  12. #define all(v) v.begin(), v.end()
  13. #define pb push_back
  14. #define P pair < int, int >
  15. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  16. #define reno(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  17. void err(istream_iterator<string> it) {}
  18.  
  19. template<typename T, typename... Args>
  20. void err(istream_iterator<string> it, T a, Args... args) {
  21. cout << *it << " = " << a << ' ';
  22. err(++it, args...);
  23. }
  24.  
  25. const int N = 1e6 + 5;
  26. int tree[N<<2];
  27. pair < int, int > p[N];
  28. void upd(int l, int r, int pos, int val, int x) {
  29. if(r < pos || pos < l)
  30. return;
  31. if(l == r and pos == r){
  32. // cout << l << ' ' << val << '\n';
  33. tree[x] = val;
  34. return;
  35. }
  36. int mid = l + r >> 1;
  37. upd(l, mid, pos, val, x << 1);
  38. upd(mid + 1, r, pos, val, x << 1 | 1);
  39. tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
  40. }
  41. int query(int l, int r, int ql, int qr, int x) {
  42. if(qr < l || r < ql || qr < ql)
  43. return 0;
  44. if(ql <= l and r <= qr)
  45. return tree[x];
  46. int mid = l + r >> 1;
  47. int a = query(l, mid, ql, qr, x << 1);
  48. int b = query(mid + 1, r, ql, qr, x << 1 | 1);
  49. return max(a, b);
  50. }
  51. signed main(){
  52. fast;
  53. int n, temp;
  54. cin >> n;
  55.  
  56. fo(i, n) {
  57. cin >> temp;
  58. p[i].F = temp;
  59. p[i].S = i;
  60. }
  61.  
  62. sort(p + 1, p + 1 + n, []
  63. (P x, P y) {
  64. // For Strictly Increasing
  65. if(x.F == y.F)
  66. return x.S > y.S;
  67. return x.F < y.F;
  68. }
  69. );
  70.  
  71. fo(i, n) {
  72. // cout << p[i].F << ' ' << p[i].S << ' ';
  73. int let = query(1, n, 1, p[i].S - 1, 1);
  74. //cout << let << '\n';
  75. upd(1, n, p[i].S, let + 1, 1);
  76.  
  77. }
  78. cout << tree[1];
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment