56分求调
查看原帖
56分求调
733345
ZYF26楼主2023/2/12 13:49
#include <bits/stdc++.h>
using  namespace std;

int n, m, x, y;
int Log[100010], dp[100010][25];
inline int read()
{
	int f = 1, x = 0;
	char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar())
		if(ch == '-')
			f = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar())
		x = x * 10 + ch - '0';
	return f * x;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	Log[0] = -1;
	Log[1] = 0;
	
	for(int i = 2; i <= n; i ++)
		Log[i] = Log[i >> 1] + 1;
	for(int i = 1; i <= n; i ++)
		dp[i][0] = read();
	for(int j = 1; j <= Log[n]; j ++)
		for(int i = 1; i <= n -(1 << j) + 1; i ++)
			dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
	for(int i = 1; i <= m; i ++)
	{
		x = read(), y = read();
		int l = Log[y - x + 1];
		cout << max(dp[x][l], dp[y - (1 << l) + 1][l]) << endl;
	}
	return 0;
}
2023/2/12 13:49
加载中...