您的位置:首页 > 产品设计 > UI/UE

SPOJ GSS2 Can you answer these queries II

2015-10-17 18:41 597 查看

Can you answer these queries II

Time Limit: 1000ms
Memory Limit: 262144KB
This problem will be judged on SPOJ. Original ID: GSS2
64-bit integer IO format: %lld Java class name: Main

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

Warning: large input/output data,be careful with certain languages

Source

Description, standard program and test data by Yang Zhe

解题:非常尤其特别厉害的线段树
假设没有重复的数据,那么

$s[1] = a[1] + a[2] + a[3] + ... + a[i];$
$s[2] = a[2] + a[3] + ... + a[i];$

$s[3] = a[3] + ... + a[i];$

$s[i] = a[i];$

也就是说,线段树上的每个叶子代表的是s[x]

我们说假设这个是后缀和,但是,我们要前缀和,所以在做lazy标记的时间做了手脚,只把最大的lazy下传去修改最大子段和

$ss[1] = max( a[1] + a[2] + ... + a[k_1] ) ( k_1\leq i );$

$ss[2] = max( a[2] + a[3] + ...+ a[k_2] ) ( k_2\leq i );$

我们可以求的是以i为右端点的询问,就是 $ss[l] ....ss[r]( r == i )$ 中的最大值;

这样子就是维护了前缀和,既有前缀和,也有后缀和,答案自然就出来了

问题是有重复的怎么办?只要记录每个元素上次出现位置,然后把上次出现的位置+1到当前位置这一段加上当前元素就是了,这样保证每个s[i]里面最多只加入了该元素一次

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
struct node{
int lt,rt;
LL lazy,sum,maxlazy,maxsum;
}tree[maxn<<2];
struct QU{
int x,y,id;
bool operator<(const QU &rhs)const{
return y < rhs.y;
}
}q[maxn];
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].sum = tree[v].maxsum = 0;
tree[v].lazy = tree[v].maxlazy = 0;
if(lt == rt) return;
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid + 1,rt,v<<1|1);
}
void _set(int x,int y){
tree[x].maxlazy = max(tree[x].maxlazy,tree[x].lazy + tree[y].lazy);
tree[x].maxsum = max(tree[x].maxsum,tree[x].sum + tree[x].maxlazy);
tree[x].lazy += tree[y].lazy;
tree[x].sum += tree[x].lazy;
}
void pushdown(int v){
if(tree[v].lazy || tree[v].maxlazy){
_set(v<<1,v);
_set(v<<1|1,v);
tree[v].lazy = tree[v].maxlazy = 0;
}
}
void pushup(int v){
tree[v].sum = max(tree[v<<1].sum,tree[v<<1|1].sum);
tree[v].maxsum = max(tree[v<<1].maxsum,tree[v<<1|1].maxsum);
}
void update(int lt,int rt,int val,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt){
tree[v].lazy += val;
tree[v].sum += val;
tree[v].maxlazy = max(tree[v].maxlazy,tree[v].lazy);
tree[v].maxsum = max(tree[v].maxsum,tree[v].sum);
return;
}
pushdown(v);
if(lt <= tree[v<<1].rt) update(lt,rt,val,v<<1);
if(rt >= tree[v<<1|1].lt) update(lt,rt,val,v<<1|1);
pushup(v);
}
LL query(int lt,int rt,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxsum;
pushdown(v);
if(rt <= tree[v<<1].rt) return query(lt,rt,v<<1);
if(lt >= tree[v<<1|1].lt) return query(lt,rt,v<<1|1);
return max(query(lt,rt,v<<1),query(lt,rt,v<<1|1));
}
int pre[maxn<<2],a[maxn],n,m;
LL ans[maxn];
int main(){
while(~scanf("%d",&n)){
memset(pre,0,sizeof pre);
for(int i = 1; i <= n; ++i)
scanf("%d",a + i);
scanf("%d",&m);
for(int i = 0; i < m; ++i){
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q,q + m);
build(1,n,1);
for(int i = 1,j = 0; i <= n; ++i){
update(pre[a[i] + maxn] + 1,i,a[i],1);
pre[a[i] + maxn] = i;
while(j < m && q[j].y == i){
ans[q[j].id] = max(0LL,query(q[j].x,q[j].y,1));
++j;
}
}
for(int i = 0; i < m; ++i) printf("%lld\n",ans[i]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: