2482: [Spoj1557] Can you answer these queries II

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 145  Solved: 76
[Submit][Status][Discuss]

Description

给定n个元素的序列。
给出m个询问:求l[i]~r[i]的最大子段和(可选空子段)。
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。
比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6。

Input

第一行一个数n。
第二行n个数,为给定的序列,这些数的绝对值小于等于100000。
第三行一个数m。
接下来m行,每行两个数,l[i],r[i]。

Output

M行,每行一个数,为每个询问的答案。

Sample Input

9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9

Sample Output

4
5
3

HINT

【数据说明】

30%:1 <= n, m <= 100

100%:1 <= n, m <= 100000

  一年前抄标程把spoj的gss2做了,一年后重新做了一遍,仍然把我恶心的。。。。【话说这道题没卡longlong很不爽】

  还是一步一步想吧,首先是离线,转成线段树的区间加,询问区间最大值的历史最大值。

  这东西怎么搞呢?

  我们先不考虑下放标记的问题,单考虑一个结点。

  记录plus[]表示从上次清除tag到现在,已经有多少“加”操作下降到当前点。

  记录hplus[]表示从上次清除tag到现在,历史加操作“峰值”与plus的差值。

  在不考虑下放的情况下每次加v操作及plus+=v,hplus=max(0,hplus-v);

  当然作为线段树,我们还要记录maxv表示当前区间的答案,及区间历史最大值。

  然后再考虑标记下方问题。

  在调试中发现单纯maxv很难维护,需要在加一个中间变量,视为nmaxv,及当前最大值。

  转移什么的知道变量含义就可以参考代码了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
using namespace std;
#define MAXN 110000
#define MAXQ MAXN
#define MAXT MAXN*5
#define lch (now<<1)
#define rch (now<<1^1)
#define smid ((l+r)>>1)
#define INF 0x3f3f3f3f
typedef long long qword;
struct sgt_node
{
qword plus,maxv;
qword hplus;
qword nmaxv;
}sgt[MAXT];
void make_plus(int now,qword v)
{
sgt[now].hplus-=v;
sgt[now].plus+=v;
sgt[now].hplus=max(sgt[now].hplus,0ll);
sgt[now].nmaxv+=v;
sgt[now].maxv=max(sgt[now].maxv,sgt[now].nmaxv+sgt[now].hplus);
}
void down(int now)
{
make_plus(lch,sgt[now].plus+sgt[now].hplus);
make_plus(lch,-sgt[now].hplus);
make_plus(rch,sgt[now].plus+sgt[now].hplus);
make_plus(rch,-sgt[now].hplus);
sgt[now].plus=sgt[now].hplus=;
assert(sgt[now].maxv==max(sgt[lch].maxv,sgt[rch].maxv));
}
void Build_sgt(int now,int l,int r)
{
sgt[now].plus=;
sgt[now].hplus=;
sgt[now].maxv=;
if (l==r)return ;
Build_sgt(lch,l,smid);
Build_sgt(rch,smid+,r);
}
void Add_sgt(int now,int l,int r,int x,int y,qword v)
{
if (l==x && r==y)
{
make_plus(now,v);
return ;
}
down(now);
if (y<=smid)
Add_sgt(lch,l,smid,x,y,v);
else if (smid<x)
Add_sgt(rch,smid+,r,x,y,v);
else
{
Add_sgt(lch,l,smid,x,smid,v);
Add_sgt(rch,smid+,r,smid+,y,v);
}
sgt[now].nmaxv=max(sgt[lch].nmaxv,sgt[rch].nmaxv);
sgt[now].maxv=max(sgt[lch].maxv,sgt[rch].maxv);
}
qword Query_sgt(int now,int l,int r,int pos)
{
if (l==r)
return sgt[now].maxv+sgt[now].plus+sgt[now].hplus;
down(now);
if (pos<=smid)
return Query_sgt(lch,l,smid,pos);
else
return Query_sgt(rch,smid+,r,pos);
}
int Query_sgt(int now,int l,int r,int x,int y)
{
if (l==x && r==y)
return sgt[now].maxv;
down(now);
if (y<=smid)
return Query_sgt(lch,l,smid,x,y);
else if (smid<x)
return Query_sgt(rch,smid+,r,x,y);
else
return max(Query_sgt(lch,l,smid,x,smid),Query_sgt(rch,smid+,r,smid+,y));
}
int a[MAXN],lastid[MAXN*],prv[MAXN];
struct qur_t
{
int l,r,id;
qword ans;
}qur[MAXQ];
bool cmp_r(qur_t q1,qur_t q2)
{
return q1.r<q2.r;
}
bool cmp_id(qur_t q1,qur_t q2)
{
return q1.id<q2.id;
}
int main()
{
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n,m;
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%d",a+i);
prv[i]=lastid[a[i]+MAXN];
lastid[a[i]+MAXN]=i;
}
scanf("%d",&m);
for (int i=;i<m;i++)
{
scanf("%d%d",&qur[i].l,&qur[i].r);
qur[i].id=i;
}
sort(qur,qur+m,cmp_r);
int qn=;
for (int i=;i<=n;i++)
{
// fprintf(stderr,"%d\n",i);
Add_sgt(,,n,prv[i]+,i,a[i]);
while (qn<m && qur[qn].r==i)
{
qur[qn].ans=Query_sgt(,,n,qur[qn].l,qur[qn].r);
qn++;
}
}
sort(qur,qur+m,cmp_id);
for (int i=;i<m;i++)
printf("%lld\n",qur[i].ans);
}

bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  9. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

随机推荐

  1. Ⅶ.spring的点点滴滴--自定义对象行为

    承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...

  2. 安卓开发中使用Genymotion模拟器

    在安卓开发中,运行和调试自己所写的安卓程序需要用到模拟器 在一般情况下 是直接在这创建一个模拟器,但是这种自带的模拟器运行效率不佳,而且启动时间漫长 所以,我们可以换一款安卓模拟器 Genymotio ...

  3. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  4. maven搭建个人仓库

    Maven环境搭建: 本地仓库+maven运行环境+构建项目 1.搭建nexus 本地仓库 1)拷贝jdk1.6和tomcat62)配置端口为8010 (端口自行定义,只要下面各处一致即可)3)复制n ...

  5. Java并发(6)带返回结果的任务执行

    携带结果的任务 JDK5提供了有可返回值的任务的执行.java.util.concurrent中Callable与Futrue用以实现带返回值的任务执行. 使用Callable与Futrue与使用Ru ...

  6. Modelsim的自动化脚本仿真平台

    自动化仿真平台由tcl语言搭建,大规模设计使用此平台让仿真便捷不少.大体上用tcl语言进行modelsim仿真的流程如下: 1. 建立库 2. 映射库到物理目录 3. 编译源代码 4. 启动仿真器 5 ...

  7. C#扫盲之:前台线程后台线程

    1.线程分类 线程由程序员创建,可是创建的方式不同,总体来说有两种,一种是个人构造,也就是使用thread类new线程对象创建,这一类线程是大部分程序员知道的,也叫专用线程;还有一种是由CLR创建,这 ...

  8. ACM——进制转换

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1012 进制转换 时间限制(普通/Jav ...

  9. aptitude

    aptitude比apt-get 要好用.是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些.举例来说,aptitude在 ...

  10. C#学习笔记(4)

    今天先把上几个系列的做一个总结,在这里给出一个面向对象版的简易计算器(重在理解面向对象的思想). 1.首先定义一个计算器类(类库)(Calculator) public abstract class ...