Advertisement
pasholnahuy

Минимум в окне

May 28th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <random>
  4.  
  5.  
  6. using std::pair;
  7. using std::cin;
  8. using std::cout;
  9. using std::vector;
  10. using int64 = int64_t;
  11. using std::max;
  12. using std::min;
  13.  
  14.  
  15. class SparseTable {
  16. public:
  17. int64 log_size;
  18. vector<int64> dp;
  19. vector<vector<int64>> seg;
  20.  
  21. explicit SparseTable(int64 n) {
  22. log_size = IntLog(n);
  23. seg = vector<vector<int64>>(n, vector<int64>(log_size));
  24. dp.assign(n + 1, 0);
  25. }
  26.  
  27. void Build(vector<int64> &val) {
  28. for (int64 i = val.size() - 1; i >= 0; --i) {
  29. seg[i][0] = val[i];
  30. for (int64 j = 1; j < log_size; ++j) {
  31. seg[i][j] = seg[i][j - 1];
  32. if (i + (1 << (j - 1)) < val.size()) {
  33. seg[i][j] = min(seg[i][j], seg[i + (1 << (j - 1))][j - 1]);
  34. }
  35. }
  36. }
  37. for (int64 i = 2; i <= val.size(); ++i) {
  38. dp[i] = dp[i - 1];
  39. if (1 << (dp[i] + 1) <= i) {
  40. ++dp[i];
  41. }
  42. }
  43. }
  44.  
  45. static int64 IntLog(int64 n) {
  46. int64 temp = 1;
  47. int64 ans = 0;
  48. while (temp <= n) {
  49. temp *= 2;
  50. ++ans;
  51. }
  52. return ans;
  53. }
  54.  
  55. int64 GetMin(int64 l, int64 r) {
  56. int64 k = dp[r - l + 1];
  57. return min(seg[l][k], seg[r - (1 << k) + 1][k]);
  58. }
  59. };
  60.  
  61. int main() {
  62. std::ios_base::sync_with_stdio(false);
  63. cin.tie(nullptr);
  64. int64 n, k;
  65. cin >> n >> k;
  66. SparseTable st(n);
  67. vector<int64> vec(n);
  68. for (size_t i = 0; i < n; ++i) {
  69. cin >> vec[i];
  70. }
  71. st.Build(vec);
  72. for (int64 i = 0; i < n - k + 1; ++i) {
  73. cout << st.GetMin(i, i + k - 1) << "\n";
  74. }
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement