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 ...
随机推荐
- sudo su权限案例
一 控制sudo: 允许执行所有命令,排除某几个命令(带参数) lanny ALL=(ALL) NOPASSWD:ALL, !/bin/su - root, !/usr/sbin/visudo 如果需 ...
- linux:nohup 不生成 nohup.out的方法
nohup java -jar /xxx/xxx/xxx.jar >/dev/null 2>&1 & 关键在于最后的 >/dev/null 2>&1 部 ...
- 纯手工搭建JSF开发环境(JSF2.2+maven+weblogic 12c/jboss EAP 6.1+)
前言: JSF 2.X因为种种原因(我个人觉得主要是因为推出太晚),再加上EJB2之前的设计过于复杂,引起很多开发人员对官方解决方案的反感,即使EJB3后来做了大量改进,国内也很少有人对EJB3感兴趣 ...
- MvvmLight ToolKit .Net4.5版本 CanExecute不能刷新界面bug
一 问题重现 1.在使用最新版本v5.1的MvvmLight中(其实这个问题很早就有了),发现CanExecute不能很好地工作了.一个简单的工程,只有MainWindow和MainWindow ...
- 30行代码实现Javascript中的MVC
从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...
- Validform表单验证总结
近期项目里用到了表单的验证,选择了Validform_v5.3.2. 先来了解一下一些基本的参数: 通用表单验证方法:Demo: $(".demoform").Validform( ...
- 20160205 - Windows 10 家庭版没有组策略
问题描述:买笔电自带的正版系统,有一个需要使用gpedit.msc,发现并不存在. 解决办法:升级到 Windows 10 专业版
- linux 内存清理/释放命令
1.清理前内存使用情况 free -m 2.开始清理 echo 1 > /proc/sys/vm/drop_caches 3.清理后内存使用情况 free -m 4.完成! 查看内存条数命令: ...
- SqlServer——批量插入数据
像Major表里面批量插入数据演示: 代码如下: Declare @I int Set @I= Begin Tran InsertData: Insert into Major values(@I,' ...
- 在eclipse中使用第三方库总结
一.建立user library 导入第三方jar文件,最简单的方式是:右键工程/属性/java build path/add external jars. 另一种方式是:window/prefren ...