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. js profiler

    https://developers.google.com/web/fundamentals/performance/critical-rendering-path/?hl=en https://de ...

  2. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  3. 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--瘦身计划

    Orchard CMS是针对CMS开发的,对于很多开发需求来说,内容管理这块儿可能并不需要,而需要它的模块式开发模式.所以我这里通过对OrchardCMS进行瘦身,去除 内容管理部分的内容,保留简单的 ...

  4. FastFourierTransform (FFT)

    FastFourierTransform.h #pragma once #include <stdio.h> #include <math.h> #ifndef INCLUDE ...

  5. 【动态域名解析工具】tunnel,国内版的ngrok,花生壳可以睡觉了

    在笔者的系列微信开发教程中,有一个比较基础且重要的一节:微信开发的调试.在文章中我推荐了两种动态域名解析的工具用于将本地的开发环境部署成服务器,一种是花生壳,一种是ngrok,但毕竟我等屌丝用不起或者 ...

  6. Orchard搜索与索引

    Orchard提供了索引与搜索的功能.开启Indexing属性可实现索引功能,伴随着一个特定的索引执行(默认包含基础搜索引擎).除了Indexing和Search提供查询索引的功能外(通过关键字或使用 ...

  7. MATLAB实现频数直方图——hist的使用

      "hist" is short for "Histogram(直方图.柱状图)". 1.N = hist(Y) bins the elements of Y ...

  8. Beta版本冲刺Day3

    会议讨论: 628:已经将原本写在jsp中的所有界面修饰代码转移到了css文件中,同时当页面跳转的时候也不会出现崩溃的现象,并且已经解决了上次无法连接数据库的问题.但是又遇到了一些新的小问题,希望明天 ...

  9. MyBatis学习--mybatis开发dao的方法

    简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...

  10. SVG的使用

    一,svg可以在浏览器中直接打开 二,在html使用<img/>标签引用 三,直接在html中使用svg标签 四,作为css背景 SVG支持ie9+ ,chrome 33.0+,firef ...