DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
1.7 KiB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #define pb push_back
  9. #define ll long long
  10. #define ub upper_bound
  11. #define lb lower_bound
  12. #define sc(x) scanf("%I64d",&x)
  13. #define pr(x) printf("%I64d\n",x)
  14. using namespace std ;
  15. const int MAXN = 1e9+1 ;
  16. const int N = 1e6+1 ;
  17. int n , m , k , x , y , z ;
  18. int a[N] , d[30][N] , U[N] ;
  19. struct node
  20. {
  21. int x, sz, y ;
  22. node *l, *r ;
  23. node (int X = 0)
  24. {
  25. x = X ;
  26. sz = 1 ;
  27. y = rand () ;
  28. l = r = NULL ;
  29. }
  30. } *t ;
  31. typedef node* pnode ;
  32. int GetSz (pnode t)
  33. {
  34. if (!t)
  35. return 0 ;
  36. return t -> sz ;
  37. }
  38. int GetKey(pnode t)
  39. {
  40. if (!t)
  41. return 0 ;
  42. return GetSz (t -> l) + 1 ;
  43. }
  44. void upd (pnode t)
  45. {
  46. if (!t)
  47. return ;
  48. t -> sz = GetSz (t -> l) + GetSz (t -> r) + 1 ;
  49. }
  50. pnode Merge (pnode a , pnode b)
  51. {
  52. if (!a)
  53. return b ;
  54. if (!b)
  55. return a ;
  56. if (a -> y > b -> y)
  57. {
  58. a -> r = Merge ( a -> r , b );
  59. upd (a) ;
  60. return a ;
  61. }
  62. else
  63. {
  64. b -> l = Merge ( a , b -> l ) ;
  65. upd (b) ;
  66. return b ;
  67. }
  68. }
  69. void Split (pnode t , pnode &a , pnode &b , int x)
  70. {
  71. if (!t)
  72. return void (a = b = NULL);
  73. int key = GetKey (t) ;
  74. if (key <= x)
  75. {
  76. Split ( t -> r , t -> r , b , x - key ) ;
  77. a = t ;
  78. }
  79. else
  80. {
  81. Split ( t -> l , a , t -> l , x ) ;
  82. b = t ;
  83. }
  84. upd (a) ;
  85. upd (b) ;
  86. }
  87. void add (int x)
  88. {
  89. pnode L , R , M ;
  90. L = R = M = NULL ;
  91. Split ( t , L , R , x ) ;
  92. Split ( L , L , M , x - 1 ) ;
  93. M = Merge ( new node ( x ) , M ) ;
  94. L = Merge ( L , M ) ;
  95. t = Merge ( L , R ) ;
  96. }
  97. int main()
  98. {
  99. }