forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARRAYSUB.cpp
More file actions
42 lines (34 loc) · 744 Bytes
/
Copy pathARRAYSUB.cpp
File metadata and controls
42 lines (34 loc) · 744 Bytes
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
#include <iostream>
#include <cmath>
using namespace std;
int N, A[1000001];
int M, SparseTable[1000001][10];
void Build()
{
M = log2(N) + 1;
for (int i = 1; i <= N; i++)
SparseTable[i][0] = A[i];
for (int j = 1; j < M; j++)
for (int i = 1; i <= N - pow(2, j) + 1; i++)
SparseTable[i][j] = max(SparseTable[i][j-1], SparseTable[i + (int)pow(2, j-1)][j-1]);
}
int RMQ(int low, int high)
{
int l = high - low + 1;
int k = log2(l);
return max(SparseTable[low][k], SparseTable[low + l - (int)pow(2, k)][k]);
}
int main()
{
ios::sync_with_stdio(false);
int k;
cin>>N;
for (int i = 1; i <= N; i++)
cin>>A[i];
Build();
cin>>k;
for (int k1 = 1; k1 <= N - k + 1; k1++)
cout<<RMQ(k1, k1+k-1)<<' ';
cout<<endl;
return 0;
}