luogu-HXOI U332989-身高

Horean0574 rp++

原题链接

Algorithms: 倍增算法


依照题意,我们需要每次求出区间 中的最小值,再把他与 小H 的身高 作比较。

但是看到数据范围,我们可以发现普通暴力的做法是绝对会超时的,所以考虑倍增算法,先用 的时间复杂度初始化,以后每次查询都只需要 的时间复杂度。

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
#include <iostream>
#include <cmath>

#define endl '\n'

using namespace std;

const int N = 1e6 + 5;
const int LN = log2(N) + 5;

int k, n, t;
int logx[N], f[N][LN];

inline void init() {
logx[0] = -1;
for (int i = 1; i <= n; ++i) {
logx[i] = logx[i / 2] + 1;
}

for (int j = 1; j <= logx[n]; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
}

inline int query(const int &x, const int &y) {
int j = logx[y - x + 1];
return min(f[x][j], f[y - (1 << j) + 1][j]);
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);

cin >> k >> n;
for (int i = 1; i <= n; ++i) {
cin >> f[i][0];
}
init();
cin >> t;
while (t--) {
int l, r;
cin >> l >> r;
if (k >= query(l, r)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
  • Title: luogu-HXOI U332989-身高
  • Author: Horean0574
  • Created at : 2023-08-27 22:12:54
  • Updated at : 2023-08-30 22:31:14
  • Link: https://blog.hxrch.top/2023/08/27/luogu-HXOI-U332989-身高/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
luogu-HXOI U332989-身高