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 ...
随机推荐
- Java多线程总结(二)锁、线程池
掌握Java中的多线程,必须掌握Java中的各种锁,以及了解Java中线程池的运用.关于Java多线程基础总结可以参考我的这篇博文Java多线程总结(一)多线程基础 转载请注明出处——http://w ...
- 打印机设置(PrintDialog)、页面设置(PageSetupDialog) 及 RDLC报表如何选择指定打印机
如果一台电脑同时连接多个打印机,而且每个打印机使用的纸张大小各不相同(比如:票据打印钱用的小票专用张,办公打印机用的是A4标准纸),在处理打印类的需求时,如果不用代码干预,用户必须每次打印时,都必须在 ...
- (一)GATT Profile和GAP 简介(目前所有的BLE应用都基于GATT,所以也要了解是怎么一回事)-转发
个人大总结:(先后顺序) 1.GAP协议定义多个角色(其中就有中心设备[GATT客户端](唯一)叫主设备||和外围设备[GATT服务端端](多个)也叫从设备). 2.先经过GAP协议,再有GATT协议 ...
- jQuery.uploadify-----文件上传带进度条,支持多文件上传的插件
借鉴别人总结的uploadify:基于jquery的文件上传插件,支持ajax无刷新上传,多个文件同时上传,上传进行进度显示,控制文件上传大小,删除已上传文件. uploadify有两个版本,一个用f ...
- js的数组
转载:http://blog.163.com/sammer_rui/blog/static/846200442010717900634/ https://developer.mozilla.org/z ...
- eclipse汉化全程
在开始之前我说一下我的环境,eclipse版本eclipse-java-indigo-SR2-win32-x86_64,操作系统Win7,但是这个基本上没有影响.红字的那个注意一下,在下面需要根据这个 ...
- XML的解析和保存
1.XML(extensible markup language;XML ) 定义:,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. XML语法规范: 标 ...
- 内网穿透神器ngrok——将本地项目驾到外网
相信做Web开发的同学们,经常会遇到需要将本地部署的Web应用能够让公网环境直接访问到的情况,例如微信应用调试.支付宝接口调试等.这个时候,一个叫ngrok的神器可能会帮到你,它提供了一个能够在公网安 ...
- vijos-1447 开关灯泡-大整数开方算法
描述 一个房间里有n盏灯泡,一开始都是熄着的,有1到n个时刻,每个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的. 提示 范围:40 ...
- 关于Hibernate的sequence diagram