GSS2 - Can you answer these queries II

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

【题意】

给出A[1],A[2]...,A[N], 有Q次询问,每次询问包含x,y,

需要回答Max{a[i]+a[i+1]+...+a[j]; x <= i <= j <= y},相同的数只能计算一次。

看到题目还以为是DP,根本没往线段树上想,看了题解感觉好神奇啊。。。

先将查询区间离线并且排序(类似莫队算法),然后循环 i =1~n,对于每一个a[i],插入进线段树更新节点。而线段树的每一个节点维护四个数组。sum[rt]表示以a[i]结尾的

最大的后缀和,presum[rt]表示1,~n中最大的区间和(a[j]+...+a[k],1<=j<=k<=i),lazy[rt]为懒惰标记,做过区间修改的应该知道,prelazy[rt]则为此区间最大的lazy。

然后就是线段树的事了。代码不难,应该很好理解,虽然我花了两天才看懂=_=

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int N=2e5+;
const int M=N*N+;
ll a[N];
ll num,m,n,tot=;
ll sum[N*],presum[N*],ans[N];
ll lazy[N*],prelazy[N*],pre[N*];
struct man{
ll l,r,id;
bool operator < (const man & b) const {
return r < b.r;
}
}q[N];
void Push_down(int rt) {
if(lazy[rt]||prelazy[rt]) {
presum[rt*]=max(presum[rt*],sum[rt*]+prelazy[rt]);
prelazy[rt*]=max(prelazy[rt*],lazy[rt*]+prelazy[rt]);
sum[rt*]+=lazy[rt];lazy[rt*]+=lazy[rt]; presum[rt*+]=max(presum[rt*+],sum[rt*+]+prelazy[rt]);
prelazy[rt*+]=max(prelazy[rt*+],lazy[rt*+]+prelazy[rt]);
sum[rt*+]+=lazy[rt];lazy[rt*+]+=lazy[rt];
lazy[rt]=prelazy[rt]=;
}
}
void Push_up(ll rt){
presum[rt]=max(presum[rt*],presum[rt*+]);
sum[rt]=max(sum[rt*],sum[*rt+]);
}
void Update(ll L,ll R,ll l,ll r,ll rt,ll add) {
if(l>=L&&r<=R) {
lazy[rt]+=add;
sum[rt]+=add;
prelazy[rt]=max(prelazy[rt],lazy[rt]);
presum[rt]=max(presum[rt],sum[rt]);
return;
}
Push_down(rt);
ll m=(r+l)>>;
if(L<=m)Update(L,R,lson,add);
if(R>m) Update(L,R,rson,add);
Push_up(rt);
}
ll Query(ll L,ll R,ll l,ll r,ll rt) {
if(L<=l&&r<=R)return presum[rt];
Push_down(rt);
ll m=(l+r)>>,ans=-;
if(L<=m)ans=max(ans,Query(L,R,lson));
if(R>m)ans=max(ans,Query(L,R,rson));
return ans;
} int main() {
scanf("%lld",&n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
scanf("%lld",&m);
for(int i=;i<m;i++){
scanf("%lld%lld",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q,q+m);
ll cnt=;
for(int i=;i<=n;i++){
Update(pre[a[i]+N]+,i,,n,,a[i]);
pre[a[i]+N]=i;
while(cnt<m&&q[cnt].r==i){
ans[q[cnt].id]=Query(q[cnt].l,q[cnt].r,,n,);
cnt++;
}
}
for(int i=;i<m;i++)printf("%lld ",ans[i]);printf("\n");
return ;
}

SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)的更多相关文章

  1. 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 ...

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

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

  3. 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 ...

  4. Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字

    题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...

  5. HDU - 4027 Can you answer these queries?(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...

  6. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

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

  8. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  9. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

随机推荐

  1. [洛谷P4779]【模板】单源最短路径(标准版)

    题目大意:单元最短路径(卡$SPFA$) 题解:$dijkstra$($\underline{\hspace{0.5em}}\underline{\hspace{0.5em}}gnu\underlin ...

  2. oracle设置自动清理归档日志脚本

    设置定时自动清理归档日志脚本 root用户下 [root@localhost ~]# mkdir /nstg [root@localhost ~]# cd /nstg/ [root@localhost ...

  3. 【BZOJ 1124】[POI2008] 枪战Maf Tarjan+树dp

    #define int long long using namespace std; signed main(){ 这个题一看就是图论题,然后我们观察他的性质,因为一个图论题如果没有什么性质,就是真· ...

  4. bzoj3343: 教主的魔法 分块 标记

    修改:两边暴力重构,中间打标记.复杂度:O(n0.5) 查询:中间二分两边暴力.O(n0.5logn0.5) 总时间复杂度O(n*n0.5logn0.5) 空间复杂度是n级别的 标记不用下传因为标记不 ...

  5. [ST表/贪心] NOI2010 超级钢琴

    [NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i ...

  6. springboot与dubbo结合

    转:http://www.cnblogs.com/Alandre/p/6490142.html  写的很好! 本文提纲 一.为啥整合 Dubbo 实现 SOA 二.运行 springboot-dubb ...

  7. Spring中Resource接口的前缀书写格式

    Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");   //这个 ...

  8. mycat 管理MySQL5.7主从搭建

    1.首先安装MySQL ab: 192.168.6.163 master 192.168.6.167 slave master: vi /etc/opt/rh/rh-mysql57/my.cnf.d/ ...

  9. auto login vpnclient bat

    @echo offstart "" /b "C:\Program Files (x86)\Cisco Systems\VPN Client\vpngui.exe" ...

  10. MySql数据库学习总结(MySQL入门到精通)

    2017.1.24-2.3日(在大兴实验室) 1.数据库存储引擎: (1)MyISAM: 访问速度快,对事物完整性没要求,并以访问为主的适合这个 (2)InnoDB: 更占磁盘空间,需要进行频繁的更新 ...