线段树结业题:SPOJ1557 GSS2 经典中的经典

http://www.spoj.com/problems/GSS2/

传说中的GSS系统

SPOJ Problem Set (classical)

1557. Can you answer these queries II

Problem code: GSS2

Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in contests.

When having a contest, Yang Zhe looks at the score of every problems first. For the problems of the same score, Yang Zhe will do only one of them. If he's lucky enough, he can get all the scores wanted.

Amber is going to hold a contest in SPOJ. She has made a list of N candidate problems, which fit Yang Zhe very well. So Yang Zhe can solve any problem he want. Amber lined up the problems, began to select. She will select a subsequence of the list as the final problems. Being A girl of great compassion, she'd like to select such a subsequence (can be empty) that Yang Zhe will get the maximal score over all the possible subsequences.

Amber found the subsequence easily after a few minutes. To make things harder, Amber decided that, Yang Zhe can take this contest only if Yang Zhe can answer her Q questions. The question is: if the final problems are limited to be a subsequence of list[X..Y] (1 <= X <= Y <= N), what's the maximal possible score Yang Zhe can get?

As we know, Yang Zhe is a bit idiot (so why did he solve the problem with a negative score?), he got Wrong Answer again... Tell him the correct answer!

Input

  • Line 1: integer N (1 <= N <= 100000);
  • Line 2: N integers denoting the score of each problem, each of them is a integer in range [-100000, 100000];
  • Line 3: integer Q (1 <= Q <= 100000);
  • Line 3+i (1 <= i <= Q): two integers X and Y denoting the ith question.

Output

  • Line i: a single integer, the answer to the ith question.

Example

Input:
9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9

Output:
4
5
3



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int MAXN = 100010;

struct Tree
{
	int l, r;
	long long curmax, premax, lazy, sum;
};

struct Node
{
	int l, r, id;
};

Node node[MAXN];
Tree tree[MAXN << 2];

int a[MAXN], n, m, pre[MAXN << 1];
long long ans[MAXN];

inline bool cmp(const Node a, const Node b)
{
	return a.r < b.r;
}

void build(int l, int r, int p)
{
	tree[p].l = l;
	tree[p].r = r;
	tree[p].curmax = tree[p].premax = 0;
	tree[p].sum = tree[p].lazy = 0;

	if (l >= r) return;
	int mid = (l + r) >> 1;

	build(l, mid, p << 1);
	build(mid + 1, r, p << 1 | 1);
	return;
}

void pushDown(int p)
{
	int l = p << 1, r = l | 1;
	if (tree[p].lazy || tree[p].premax )
    {
		tree[l].premax = max(tree[l].premax, tree[l].lazy + tree[p].premax);
		tree[l].curmax = max(tree[l].curmax, tree[l].sum + tree[p].premax);
		tree[l].lazy += tree[p].lazy;
		tree[l].sum += tree[p].lazy;

		tree[r].premax = max(tree[r].premax, tree[r].lazy + tree[p].premax);
		tree[r].curmax = max(tree[r].curmax, tree[r].sum + tree[p].premax);
		tree[r].lazy += tree[p].lazy;
		tree[r].sum += tree[p].lazy;

		tree[p].lazy = 0;
		tree[p].premax = 0;
	}
	return;
}

void pushUp(int p)
{
	tree[p].sum = max(tree[p << 1].sum, tree[p << 1 | 1].sum);
	tree[p].curmax = max(tree[p << 1].curmax, tree[p << 1 | 1].curmax);
	return;
}

void insert(int l, int r, int p, long long x)
{
	if(l <= tree[p].l && r >= tree[p].r)
    {
		tree[p].sum += x;
		tree[p].lazy += x;
		tree[p].premax = max(tree[p].premax, tree[p].lazy);
		tree[p].curmax = max(tree[p].curmax, tree[p].sum);
		return;
	}
	if( l > tree[p].r || r < tree[p].l ) return;
	pushDown( p );
	insert(l, r, p << 1, x);
	insert(l, r, p << 1 | 1, x);
	pushUp(p);
}

long long query(int l, int r, int p)
{
	if(l <= tree[p].l && r >= tree[p].r) return tree[p].curmax;

	if(l > tree[p].r || r < tree[p].l) return 0;

	pushDown( p );
	return max(query(l, r, p << 1), query(l, r, p << 1 | 1));
	//pushUp( p );
}

int main(void)
{
	while(scanf("%d", &n) != EOF){

        memset(pre, 0, sizeof(pre));

		for (int i = 1; i <= n; ++i) scanf("%d", a + i);

		scanf("%d", &m);
		build(1, n, 1);

		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d", &node[i].l, &node[i].r);
			node[i].id = i;
		}

		sort(node, node + m, cmp);

		int j = 0;
		for (int i = 1; i <= n; ++i)
		{
			insert(pre[a[i] + MAXN] + 1, i, 1, a[i]);
			pre[a[i] + MAXN] = i;
			while (j < m && node[j].r == i)
            {
				ans[node[j].id] = query(node[j].l, node[j].r, 1);
				j++;
			}
		}

		for (int i = 0; i < m; ++i) printf("%lld\n", ans[i]);
	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值