SPOJ GSS2 Can you answer these queries II
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- SPOJ GSS2 Can you answer these queries II ——线段树
[题目分析] 线段树,好强! 首先从左往右依次扫描,线段树维护一下f[].f[i]表示从i到当前位置的和的值. 然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值. 关于历史最值问题: 标 ...
- 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 ...
- SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...
- SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树
传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...
- SP1557 GSS2 - Can you answer these queries II
一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...
- SP1557 GSS2 - Can you answer these queries II(线段树)
传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...
随机推荐
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程
AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
- SM2国密证书合法性验证
通常我们遇到过的X509证书都是基于RSA-SHA1算法的,目前国家在大力推行国密算法,未来银行发行的IC卡也都是基于PBOC3.0支持国密算法的,因此我们来学习一下如何验证SM2国密证书的合法性.至 ...
- window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应
错误写法 , 主要是在 href="#"这里 <a href="#" id="send" onclick="return b ...
- 让 innerHTML 进来的 script 代码跑起来
今天来简单聊聊如何让 innerHTML 进来的 scrip 代码跑起来的问题. 前台请求一个接口,接口返回一些 HTML 标签拼接成的字符串,以供前端直接 innerHTML 生成 DOM 元素,这 ...
- versionCompare 版本号比较工具
简介 需求非常简单,需要比较软件或app的版本号,判断大小,形如 0.10.2形式的版本号字符串.实现逻辑是按照点(.)分割字符串,然后逐级比较版本大小.不存在的按0处理,空字符串小于非空字符串. 测 ...
- 使用Jekyll在Github上搭建博客
最近在玩github,突然发现很多说明网站或者一些介绍页面全部在一个域名是*****.github.io上. 好奇!!!真的好奇!!!怎么弄的?我也要一个~~~ 于是去网站上查询了一下,找到了http ...
- 完全开源Android网络框架 — 基于JAVA原生的HTTP框架
HttpNet网络请求框架基于HttpUrlConnection,采用Client + Request + Call的请求模型,支持https默认证书,数字安全证书.支持http代理!后续将会实现队列 ...
- MC700 安装双系统
2011年买的MBP MC700给老婆用了一段时间后,老婆还不习惯不了Mac OS或是虚拟机,要求必须给安装windows,无奈时隔四年后,只能重新尝试在MC700上用bootcamp安装Window ...
- react的基本学习
1.<SubSubComp {...this.props } /> 传递属性,{...props}的方式为组件传递了这两个属性,这就是JSX中的延展属性,"..."成为 ...
- AsyncTask异步任务类使用学习
new MyAsyncTask() .execute("http://pic.baike.soso.com/p/20120716/bki-20120716095331-640956396.j ...