Advertisement
juaniisuar

Merge tree (SPOJ MKTHNUM)

Oct 19th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #define INF 1e9
  5. #define For(i,x,n) for(int i=x; i<n; i++)
  6. using namespace std;
  7.  
  8. struct ura{
  9. int x, y;
  10. };
  11.  
  12. const int maxN = 1e5;
  13. const int maxMagic = (1<<17);
  14. int N, M, magic;
  15. int input[maxN+10];
  16. ura minmaxst[maxMagic * 2];
  17. vector<int> ST[maxMagic * 2];
  18.  
  19. vector<int> MERGE(vector<int> A, vector<int> B){
  20. if(A.empty()) return B;
  21. if(B.empty()) return A;
  22.  
  23. vector<int> ans;
  24. for(int i=0, j=0;;){
  25. if(i==A.size()){
  26. while(j<B.size())
  27. ans.push_back(B[j++]);
  28. break;
  29. }
  30. if(j==B.size()){
  31. while(i<A.size())
  32. ans.push_back(A[i++]);
  33. break;
  34. }
  35. if(A[i]<B[j]) ans.push_back(A[i++]);
  36. else ans.push_back(B[j++]);
  37. }
  38. return ans;
  39. }
  40.  
  41. void build(int index, int s, int e){
  42. if(s>e) return;
  43. if(s==e){
  44. ST[index].push_back(input[s]);
  45. return;
  46. }
  47.  
  48. build(index*2, s, (s+e)/2);
  49. build(index*2+1, 1+(s+e)/2, e);
  50. ST[index] = MERGE(ST[index*2], ST[index*2+1]);
  51. }
  52.  
  53. void buildminmax(int index, int s, int e){
  54. if(s>e) return;
  55. if(s==e){
  56. minmaxst[index] = {input[s], input[s]};
  57. return;
  58. }
  59.  
  60. buildminmax(index*2, s, (s+e)/2);
  61. buildminmax(index*2+1, 1+(s+e)/2, e);
  62. minmaxst[index] = { min(minmaxst[index*2].x, minmaxst[index*2+1].x),
  63. max(minmaxst[index*2].y, minmaxst[index*2+1].y) };
  64. }
  65.  
  66. ura queryminmax(int index, int s, int e, int qs, int qe){
  67. if(s>e || s>qe || e<qs) return {INF, -INF};
  68. if(s>=qs && e<=qe) return minmaxst[index];
  69.  
  70. ura a = queryminmax(index*2, s, (s+e)/2, qs, qe);
  71. ura b = queryminmax(index*2+1, 1+(s+e)/2, e, qs, qe);
  72. return { min(a.x, b.x), max(a.y, b.y) };
  73. }
  74.  
  75. int query(int index, int s, int e, int qs, int qe, int v){
  76. if(s>e || s>qe || e<qs) return 0;
  77. if(s>=qs && e<=qe)
  78. return lower_bound(ST[index].begin(), ST[index].end(), v) - ST[index].begin();
  79.  
  80. return query(index*2, s, (s+e)/2, qs, qe, v) + query(index*2+1, 1+(s+e)/2, e, qs, qe, v);
  81. }
  82.  
  83. int main()
  84. {
  85. freopen("tests.in", "r", stdin);
  86. cin >> N >> M;
  87. For(i,0,N)
  88. cin >> input[i];
  89. for(magic=1; magic<N; magic<<=1);
  90. build(1,0,N-1);
  91. buildminmax(1,0,N-1);
  92.  
  93. int a, b, k, l, r, e, z;
  94. ura aux;
  95. For(i,0,M){
  96. cin >> a >> b >> k;
  97. /// binary
  98. aux = queryminmax(1, 0, N-1, a-1, b-1);
  99. l = aux.x;
  100. r = aux.y;
  101. while(l<=r){
  102. e = (l+r)/2;
  103. z = query(1, 0, N-1, a-1, b-1, e);
  104. if( z > k-1 ) r=e-1;
  105. else if( z < k-1 ) l=e+1;
  106. else {
  107. cout << e << endl;
  108. break;
  109. }
  110. }
  111. }
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement