TLE80求调
查看原帖
TLE80求调
374769
Epi4any楼主2022/11/1 15:07
#include <iostream>
#include <cmath>
using namespace std;
inline int read() {
	int w = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		w = (w << 1) + (w << 3) + (ch ^ 48);
		ch = getchar();
	}
	return w * f;
}
const int maxn = 1e6 + 5;
int n, m, a[maxn], st[maxn][25];
void st_build() {
	for (int i = 1; i <= n; i++) st[i][0] = a[i];
	for (int j = 1; (1 << j) <= n; j++) {
		for (int i = 1; i + (1 << j) -1 <= n; i++) {
			st[i][j] = max(st[i][j - 1], st[i + (1 << (j-1))][j-1]);
		}
	}
}
inline int st_getmax(int l, int r) {
	int i = 0;
	while ((1 << (i + 1)) <= r - l + 1) i++;
	return max(st[l][i], st[r - (1 << i) + 1][i]);
}
int main() {
	ios :: sync_with_stdio(false), cout.tie(0);
	n = read(), m = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	st_build();
	for (int i = 1, l, r; i <= m; i++) {
		l = read(), r = read();
		cout << st_getmax(l, r) << endl;
	}
	return 0;
}

大佬轻喷qwq

2022/11/1 15:07
加载中...