From af5bf736f8120bf99fb6a062c3f4e88a58437afe Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 28 Oct 2022 09:23:22 +0800 Subject: [PATCH] =?UTF-8?q?P3865=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91ST?= =?UTF-8?q?=20=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/91841060 --- Luogu/P3865/P3865.cpp | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Luogu/P3865/P3865.cpp b/Luogu/P3865/P3865.cpp index ab074957..be19608b 100644 --- a/Luogu/P3865/P3865.cpp +++ b/Luogu/P3865/P3865.cpp @@ -1,24 +1,39 @@ -#include +#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -int n, m, l, r, k, t, f[100005][25]; +const int N = 1e5 + 5; + +int n, m, k, t, f[N][25]; int main() { - scanf("%d%d", &n, &m); + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + for (int i = 1; i <= n; i++) { - scanf("%d", &f[i][0]); + cin >> f[i][0]; } - t = log(n) / log(2) + 1; - for (int i = 1; i < t; i++) { + + t = std::__lg(n); + for (int i = 1; i <= t; i++) { for (int j = 1; j <= n - (1 << i) + 1; j++) { - f[j][i] = max(f[j][i - 1], f[j + (1 << i - 1)][i - 1]); + f[j][i] = std::max(f[j][i - 1], f[j + (1 << i - 1)][i - 1]); } } - for (int i = 1; i <= m; i++) { - scanf("%d%d", &l, &r); - k = log(r - l + 1) / log(2); - printf("%d\n", max(f[l][k], f[r - (1 << k) + 1][k])); + + for (int i = 1, l, r; i <= m; i++) { + cin >> l >> r; + + k = std::__lg(r - l + 1); + + cout << std::max(f[l][k], f[r - (1 << k) + 1][k]) << endl; } + return 0; }