-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmq_self.cpp
75 lines (57 loc) · 1.18 KB
/
rmq_self.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifdef DBG
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int len = 6;
int value[len]={4,6,1,5,7,3};
int sparse[(int)(log(len)/log(2))+1][len];
for (int i = 0; i <= (int)(log(len)/log(2)); ++i)
{
for (int j = 0; j < len; ++j)
{
sparse[i][j] = 0;
}
}
for (int i = 0; i < len; ++i)
{
sparse[0][i] = i;
}
for (int i = 1; (int)(pow(2.0,i)) < len; ++i)//檢查長度
{
for (int j = 0; j+(int)(pow(2.0,i))-1 < len; ++j)//檢查往右有沒有超出範圍
{
int L = sparse[i-1][j];
int R = sparse[i-1][j+(int)(pow(2.0,i-1))];
if (value[L] <= value[R])
{
sparse[i][j] = L;
}
else
{
sparse[i][j] = R;
}
}
}
for (int i = 0; i <= (int)(log(len)/log(2)); ++i)
{
for (int j = 0; j < len; ++j)
{
printf("%d ", sparse[i][j]);
}
printf("\n");
}
int Left,Right;
while(~scanf("%d %d",&Left,&Right))
{
int length = Right - Left + 1;
int lg = (int)(log(length)/log(2));
// printf("length%d\n", length);
// printf("lg%d\n", lg);
printf("%d\n", min( value[sparse[lg][Left]] , value[ sparse[lg][Right-(1<<lg)+1] ]));
}
return 0;
}