-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparse Table (RMQ, O(1)).cpp
66 lines (52 loc) · 1.23 KB
/
Sparse Table (RMQ, O(1)).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
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <math.h>
#define N 1000100
using namespace std;
typedef long long ll;
ll n, k, r, l, v;
ll arr[N], log1[N], power[50];
pair <ll, ll> dp[N][25];
pair <ll, ll> ans;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
log1[1] = 0;
for (int i = 2; i <= n; i++) log1[i] = log1[i / 2] + 1;
power[0] = 1;
for (int i = 1; i <= 40; i++) power[i] = power[i - 1] * 2;
for (ll i = 0; i < n; i++)
dp[i][0] = { arr[i],i };
for (ll i = n; i >= 0; i--) {
for (ll j = 1; j <= 20; j++) {
if (dp[i][j - 1].first > dp[i + power[j - 1]][j - 1].first)
dp[i][j] = dp[i][j - 1];
else
dp[i][j] = dp[i + power[j - 1]][j - 1];
}
}
/*for (int i = 1; i < n; i++) {
for (int j = 0; j < lg[n]; j++) {
cout << dp[i][j].second << " ";
}
cout << "\n";
}*/
cin >> k;
while (k--) {
ans = { 0,0 };
cin >> l >> r;
--r; --l;
v = log1[(r - l + 1)];
if (dp[l][v].first > dp[r - power[v] + 1][v].first)
ans = dp[l][v];
else
ans = dp[r - power[v] + 1][v];
cout << ans.first << " " << ans.second + 1 << "\n";
}
return 0;
}