AcWing 1273. 天才的记忆
原题链接
简单
作者:
_empty
,
2020-05-04 19:00:02
,
所有人可见
,
阅读 735
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int lg[N];
int f[N][20];
int a[N];
int n;
int find(int l, int r)
{
int k = lg[r - l + 1];
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
int main()
{
cin >> n;
// init
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) f[i][0] = a[i];
for (int i = 1; i <= n; i++) lg[i] = (int)log2(i);
for (int i = 1; i <= lg[n]; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++)
f[j][i] = max(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
int m;
cin >> m;
while (m--)
{
int l, r;
cin >> l >> r;
cout << find(l, r) << endl;
}
}