-
Notifications
You must be signed in to change notification settings - Fork 0
/
1306.cpp
46 lines (34 loc) · 990 Bytes
/
1306.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <algorithm>
#define N 1000005
using namespace std;
int n, m, arr[N], st[N * 3];
int init(int left, int right, int index)
{
if (left == right)
return st[index] = arr[left];
int mid = (left + right) / 2;
return st[index] = max(init(left, mid, index * 2), init(mid + 1, right, index * 2 + 1));
}
int getval(int head, int tail, int left, int right, int index)
{
if (right < head || tail < left)
return 0;
if (head <= left && right <= tail)
return st[index];
int mid = (left + right) / 2;
return max(getval(head, tail, left, mid, index * 2), getval(head, tail, mid + 1, right, index * 2 + 1));
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i;
cin >> n >> m;
for (i = 1; i <= n; i++)
cin >> arr[i];
init(1, n, 1);
for (i = m; i <= n - m + 1; i++)
cout << getval(i - m + 1, i + m - 1, 1, n, 1) << " ";
return 0;
}