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

    Circle Through Three Points Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Jav ...

  2. jQuery免费资料

     JQvery免豆.pdf       jQuery实战之仿flash跳动的按钮效果[源码]http://down.51cto.com/data/188600JQuery 1.4.2 手册简体中文版h ...

  3. vi快捷键必知必会

    文本编辑器是所有计算机系统中最常用的一种工具.UNIX下的编辑器有ex,sed和vi等,其中,使用最为广泛的是vi,而vi命令繁多,论坛里好像这方面的总结不多,以下稍做总结,以资共享!渴望更正和补充! ...

  4. 使用ViewState[""]传递Hashtable的值

    //首先定义Hashtable myHach = new Hashtable(); //添加键值到Hashtable中myHash.Add("ServiceType1", &quo ...

  5. UML 结构图之包图 总结

    [注] 本文不是包图的基础教程, 只是包图的图形总结. 学习UML图形 推荐阅读<UML参考手册>第2版. http://www.umlchina.com/ 推荐微软的开发软件设计模型 h ...

  6. 深入分析 Java 中的中文编码问题 (文章来自网络)

    许令波,developerWorks 中国网站最佳作者,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,喜欢钻研开源框架的设计原理.有时间将学到的知识整理成文章,也喜欢记录 ...

  7. ionic(一) build你的第一个android apk

    1.ionic start myApp tabs    >>创建一个app 2.cd myApp  >>进入myApp文件 3.ionic platform add andro ...

  8. SQL Server调优系列基础篇 - 并行运算总结(一)

    前言 上三篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符.联合运算符的优化技巧. 本篇我们分析SQL Server的并行运算,作为多核计算机盛行的今天,SQL Server也会适时调整自 ...

  9. /etc/resolv.conf文件详解

    大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...

  10. addLoadEvent函数

    首先是addLoadEvent函数的代码清单: function addLoadEvent(func){    var oldonload=window.onload;    if(typeof wi ...