Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- #include <random>
- using std::pair;
- using std::cin;
- using std::cout;
- using std::vector;
- using int64 = int64_t;
- using std::max;
- using std::min;
- class SparseTable {
- public:
- int64 log_size;
- vector<int64> dp;
- vector<vector<int64>> seg;
- explicit SparseTable(int64 n) {
- log_size = IntLog(n);
- seg = vector<vector<int64>>(n, vector<int64>(log_size));
- dp.assign(n + 1, 0);
- }
- void Build(vector<int64> &val) {
- for (int64 i = val.size() - 1; i >= 0; --i) {
- seg[i][0] = val[i];
- for (int64 j = 1; j < log_size; ++j) {
- seg[i][j] = seg[i][j - 1];
- if (i + (1 << (j - 1)) < val.size()) {
- seg[i][j] = min(seg[i][j], seg[i + (1 << (j - 1))][j - 1]);
- }
- }
- }
- for (int64 i = 2; i <= val.size(); ++i) {
- dp[i] = dp[i - 1];
- if (1 << (dp[i] + 1) <= i) {
- ++dp[i];
- }
- }
- }
- static int64 IntLog(int64 n) {
- int64 temp = 1;
- int64 ans = 0;
- while (temp <= n) {
- temp *= 2;
- ++ans;
- }
- return ans;
- }
- int64 GetMin(int64 l, int64 r) {
- int64 k = dp[r - l + 1];
- return min(seg[l][k], seg[r - (1 << k) + 1][k]);
- }
- };
- int main() {
- std::ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int64 n, k;
- cin >> n >> k;
- SparseTable st(n);
- vector<int64> vec(n);
- for (size_t i = 0; i < n; ++i) {
- cin >> vec[i];
- }
- st.Build(vec);
- for (int64 i = 0; i < n - k + 1; ++i) {
- cout << st.GetMin(i, i + k - 1) << "\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment