forked from andreimargeloiu/Competitive-programming-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMQ.cpp
More file actions
30 lines (26 loc) · 562 Bytes
/
Copy pathRMQ.cpp
File metadata and controls
30 lines (26 loc) · 562 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
# include <fstream>
# include <algorithm>
# define NR 100005
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int i,j,n,m,x,y,k;
int a[20][NR], lg[NR];
int main ()
{
f>>n>>m;
for (i=2; i<=n; ++i)
lg[i]=lg[i/2]+1;
for (i=1; i<=n; ++i)
f>>a[0][i];
for (i=1; i<=lg[n]; ++i)
for (j=1; j<=n-(1<<(i-1)); ++j)
a[i][j]=min(a[i-1][j], a[i-1][j+(1<<(i-1))]);
for (i=1; i<=m; ++i)
{
f>>x>>y;
k=lg[y-x+1];
g<<min(a[k][x], a[k][y-(1<<k)+1])<<"\n";
}
return 0;
}