其他分享
首页 > 其他分享> > Can you answer these queries 1?

Can you answer these queries 1?

作者:互联网

Can you answer these queries 1?

代码

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int M = 1e5 + 5;
int m, n, len, a[M];
int lg[M << 2], pos[M], p[21][M], s[21][M];
#define max(a,b) ((a) > (b) ? (a) : (b))
void build(int u,int l,int r,int dep)
{
	if (l == r)
	{
		pos[l] = u;
		return;
	}
	int mid = (l + r) >> 1;
	int prep = a[mid];
	int sm = a[mid];
	p[dep][mid] = a[mid];
	s[dep][mid] = a[mid];
	sm = max(sm, 0);
	for (int i = mid - 1; i >= l; i--)
	{
		prep += a[i], sm += a[i];
		s[dep][i] = max(s[dep][i + 1], prep);
		p[dep][i] = max(p[dep][i + 1], sm);
		sm = max(sm, 0);
	}
	p[dep][mid + 1] = a[mid + 1];
	s[dep][mid + 1] = a[mid + 1];
	prep = sm = a[mid + 1];
	sm = max(sm, 0);
	for (int i = mid + 2; i <= r; i++)
	{
		prep += a[i], sm += a[i];
		s[dep][i] = max(s[dep][i - 1], prep);
		p[dep][i] = max(p[dep][i - 1], sm);
		sm = max(sm, 0);
	}
	build(u << 1, l, mid, dep + 1);
	build(u << 1 | 1, mid + 1, r, dep + 1);
	return;
}
int query(int l, int r)
{
	if (l == r)
		return a[l];
	int d = lg[pos[l]] - lg[pos[l] ^ pos[r]];
	return max(max(p[d][l], p[d][r]), s[d][l] + s[d][r]);
}
int main() 
{
	cin >> n;
	for (len = 2; len < n; len <<= 1);
	for (int i = 1; i <= n; i++) cin>>a[i];
	for (int l = len * 2, i = 2; i <= l; ++i) lg[i] = lg[i >> 1] + 1;
	build(1, 1, len, 1);
	cin >> m;
	for (int l, r; m; m--)
	{
		cin >> l >> r;
		cout<<query(l, r)<<endl;
	}
	return 0;
}

标签:dep,these,max,mid,len,int,sm,queries,answer
来源: https://www.cnblogs.com/Lewis--blog/p/15857057.html