Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Description

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

Hint

Added by: Fudan University Problem Setters
Date: 2007-05-16
Time limit: 1s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: C99 strict ERL JS
Resource: Description, standard program and test data by Yang Zhe

这系列题没按难度顺序来啊……1~5里面感觉这道最难。

用线段树的叶子结点存a[i]表示序列的后缀和(即data[i]+data[i+1]+data[i+2]+...+data[n]),以及区间内最优答案。

离线处理,将询问按右端点从小到大排序。for i:=1 to n 从左往右不断将data[i]添加到线段树,并回答右端点等于i的询问(此时线段树里存的后缀和都只到i)。

如何排除重复数字?离散化数据,对每个数字记录上次出现的位置last,往线段树里添加新值时,只修改闭区间[last+1,i]

具体维护方法看代码:

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define lc rt<<1
#define rc rt<<1|1
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int data[mxn];
int last[mxn<<];//离散化
struct node{
LL mx;//区间内最优解
LL prelazy,mksum;//标记
LL smm;//区间内最优的后缀和
}t[mxn<<],tmp0;
void push_up(int l,int r,int rt){
t[rt].mx=max(t[lc].mx,t[rc].mx);
t[rt].smm=max(t[lc].smm,t[rc].smm);
return;
}
void pushdown(int l,int r,int rt){
if(!t[rt].mksum && !t[rt].prelazy)return;
int ls=rt<<;int rs=rt<<|;
//L
t[ls].prelazy=max(t[ls].prelazy,t[ls].mksum+t[rt].prelazy);
t[ls].mx=max(t[ls].mx,t[ls].smm+t[rt].prelazy);
t[ls].mksum+=t[rt].mksum;
t[ls].smm+=t[rt].mksum;
//R
t[rs].prelazy=max(t[rs].prelazy,t[rs].mksum+t[rt].prelazy);
t[rs].mx=max(t[rs].mx,t[rs].smm+t[rt].prelazy);
t[rs].mksum+=t[rt].mksum;
t[rs].smm+=t[rt].mksum;
//
t[rt].prelazy=t[rt].mksum=;
return;
}
void change(int L,int R,LL v,int l,int r,int rt){
if(L<=l && r<=R){
t[rt].smm+=v;//最优后缀和
t[rt].mksum+=v;//后缀和的增加量
t[rt].mx=max(t[rt].mx,t[rt].smm);//答案
t[rt].prelazy=max(t[rt].prelazy,t[rt].mksum);//答案的增加量
return;
}
int mid=(l+r)>>;
pushdown(l,r,rt);
if(L<=mid)change(L,R,v,l,mid,lc);
if(R>mid)change(L,R,v,mid+,r,rc);
push_up(l,r,rt);
return;
}
LL query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R)return t[rt].mx;
pushdown(l,r,rt);
int mid=(l+r)>>;
LL res=-1e15;
if(L<=mid)res=max(res,query(L,R,l,mid,lc));
if(R>mid)res=max(res,query(L,R,mid+,r,rc));
return res;
}
struct qry{//存储询问
int l,r,id;
}q[mxn];
int cmp(qry a,qry b){
return a.r<b.r;//按右端点从小到大排序
}
LL ans[mxn];
int bas=;
int main(){
n=read();
int i,j,x,y,k;
for(i=;i<=n;i++)data[i]=read();
// Build(1,n,1);
m=read();
for(i=;i<=m;i++){
q[i].l=read();q[i].r=read();q[i].id=i;
}
sort(q+,q+m+,cmp);
//
int hd=;//待处理询问
for(i=;i<=n;i++){
change(last[data[i]+bas]+,i,data[i],,n,);
last[data[i]+bas]=i;
while(hd<=m && q[hd].r==i){
ans[q[hd].id]=query(q[hd].l,q[hd].r,,n,);
hd++;
}
}
for(i=;i<=m;i++)
printf("%lld\n",ans[i]);
return ;
}

SPOJ GSS2 Can you answer these queries II的更多相关文章

  1. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  2. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  3. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  4. SPOJ GSS2 Can you answer these queries II ——线段树

    [题目分析] 线段树,好强! 首先从左往右依次扫描,线段树维护一下f[].f[i]表示从i到当前位置的和的值. 然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值. 关于历史最值问题: 标 ...

  5. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  6. SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)

    都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...

  7. SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树

    传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...

  8. SP1557 GSS2 - Can you answer these queries II

    一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...

  9. SP1557 GSS2 - Can you answer these queries II(线段树)

    传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...

随机推荐

  1. 关于code reiview

    先谈谈三个code review的关键因素: 一.创建review要简单 code reivew是一个程序员日常工作中经常做的一件事,理论上来讲,任何一个将要submit到SCM的change,都必须 ...

  2. c++基础 使用智能指针

    三个智能指针模板(auto_ptr.unique_ptr和shard_ptr)都定义了类似指针的对象(c++11已将auto_ptr摒弃),可以将new获得(直接或间接) 的地址赋给这种对象.当智能指 ...

  3. 从Hadoop Summit 2016看大数据行业与Hadoop的发展

    前言: 好吧我承认已经有四年多没有更新博客了.... 在这四年中发生了很多事情,换了工作,换了工作的方向.在工作的第一年的时候接触机器学习,从那之后的一年非常狂热的学习机器学习的相关技术,也写了一些自 ...

  4. Java程序-进程中的"进程"

    进程 我们知道程序在磁盘上的时候是静态的,当他被加载到内存的时候,就变成了一个动态的,称为进程,如下图是程序被加载到内存后,在内存中的分布情况如下      此图来自http://blog.csdn. ...

  5. OS存储器管理(三) 虚拟存储器

    基本概念与实现 1)局部性原理 在一段时间内,运行的作业程序仅访问(涉及到)一部分作业代码,即不会涉及整个地址空间.即在一段时间间隔内,仅装入一部分代码,作业照样能正常运行 2)虚拟存储器的引入 作业 ...

  6. android和httpClient

    一.说起来都是泪 各大组织不同步,可是我想用别人的库. 二.谷歌和阿帕奇的爱恨情仇 初,谷歌安卓新出,库中自带HttpClient 4.0测试预览版.为与安卓保持API同步,HTTPClient不敢大 ...

  7. 1025WHERE执行顺序以及MySQL查询优化器

    转自http://blog.csdn.net/zhanyan_x/article/details/25294539 -- WHERE执行顺序-- 过滤比较多的放在前面,然后更加容易匹配,从左到右进行执 ...

  8. 【BZOJ 4600】【SDOI 2016】硬币游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=4600 转化成nim游戏 因为对于每一个反面朝上的硬币编号可以拆成\(2^a3^bc\),选择这个硬币 ...

  9. 我的防Q+

    Q+链接:  http://onemore.web-45.com/index1.html: 兼容IE8: __页面被主机屋收回去了,现在又在弄自己的服务器,稍等呗

  10. 网络流最小割 POJ 3469

    题意  2个CPU n个任务 给出在第一个 第二个运行时的花费 m  个  a  b 不在同一个CPU运行的额外花费 建图 源点 ->   n    -> 汇点 权          a1 ...