Advertisement
askarulytarlan

ДЕРЕВО ОТРЕЗКОВ

Mar 25th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1.  
  2.  
  3. 1
  4. 2 3
  5. 4 5 6 7
  6. 8 9 10 11 12 13 14 15
  7. 1 2 3 4 5 -1 -1 -1
  8.  
  9.  
  10. n
  11. a1 a2 a3 ... an
  12. m
  13. l1 r1
  14. l2 r2
  15. .
  16. .
  17. .
  18. ln rn
  19.  
  20.  
  21. #include <iostream>
  22.  
  23. using namespace std;
  24.  
  25. int main() {
  26.  
  27. cin >> n;
  28.  
  29. for (int i = 1; i <= n; i++)
  30. cin >> a[i];
  31.  
  32. sz = 1;
  33.  
  34. while(sz < n)
  35. sz *= 2;
  36.  
  37. for (int i = sz; i <= sz + n - 1; i++)
  38. t[i] = a[i - sz + 1];
  39.  
  40. for (int i = sz + n; i <= sz + sz - 1; i++)
  41. t[i] = -inf;
  42.  
  43. for (int i = sz - 1; i >= 1; i--)
  44. t[i] = max(t[i * 2], t[i * 2 + 1]);
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement