mirror of
https://github.com/wczffl-503/OI-Codes.git
synced 2025-07-08 02:13:51 +08:00
upload some new code
This commit is contained in:
51
FZOI/区间最大值.cpp
Normal file
51
FZOI/区间最大值.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define int long long
|
||||
#define endl "\n"
|
||||
#define IMX LONG_LONG_MAX
|
||||
#define IMN LONG_LONG_MIN
|
||||
using namespace std;
|
||||
|
||||
/* toothless. #17 */
|
||||
|
||||
const int N = 1e7 + 10;
|
||||
const int M = 2e3 + 5;
|
||||
|
||||
int n, m;
|
||||
int a[100050], dpmax[100050][100];
|
||||
int LOG[500005];
|
||||
void build()
|
||||
{
|
||||
LOG[0] = -1;
|
||||
for (int i = 1; i <= 500005; i++)
|
||||
LOG[i] = LOG[i >> 1] + 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
dpmax[i][0] = a[i];
|
||||
}
|
||||
int p = log2(n);
|
||||
for (int k = 1; k <= p; k++) {
|
||||
for (int s = 1; s + (1 << k) <= n + 1; s++) {
|
||||
dpmax[s][k] = max(dpmax[s][k - 1], dpmax[s + (1 << (k - 1))][k - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
int query(int l, int r)
|
||||
{
|
||||
int k = LOG[r - l + 1];
|
||||
int x = max(dpmax[l][k], dpmax[r - (1 << k) + 1][k]);
|
||||
return x;
|
||||
}
|
||||
|
||||
signed main()
|
||||
{
|
||||
"toothless. #17";
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++)
|
||||
cin >> a[i];
|
||||
build();
|
||||
for (int i = 1; i <= m; i++) {
|
||||
int l, r;
|
||||
cin >> l >> r;
|
||||
cout << query(l, r) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user