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 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
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9
Sample Output
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 线段树的更多相关文章
- 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 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...
- 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 ...
- 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 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 ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
- 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]| ≤ ...
随机推荐
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
在android网络开发过程中,经常需要获取网络资源,比如下载图片,下载文本文件内容等,这个时候就需要http请求来获取相应的网络资源.首先看看实例效果图: 下载图片截图 ...
- 第七篇:web之前端之ajax
前端之ajax 前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascri ...
- MySQL索引视图
一.索引 索引是存放在模式(schema)中的一个数据库对象,索引的作用就是提高对表的检索查询速度, 索引是通过快速访问的方法来进行快速定位数据,从而减少了对磁盘的读写操作. 索引是数据库的一个对象, ...
- Use excel Macro export data from database
Sub DownLoadMacro() '定义过程名称 Dim i As Integer, j As Integer, sht As Worksheet 'i,j为整数变量:sht 为excel工作表 ...
- Java 泛型类型的一些限制
由于泛型类型在运行时被消除,因此,对于如何使用泛型类型是有一些限制的. 限制1:不能使用new E() 不能使用泛型类型参数创建实例.例如,下面的语句是错误的: E object = new E(); ...
- Angularjs总结(八)$ cookie和$rootscope
AngularJS 提供了很好的 $cookie 和 $cookieStore API 用来处理 cookies .这两个服务都能够很好的发挥HTML5 cookies,当HTML5 API可用时浏览 ...
- 伪Base16的构思和实现
最近看见了一个迅雷地址,发现将其转换为普通链接的工具后,发现所谓专用地址地址就是原地址前加一个表示迅雷的前缀,后进行Base64编码.查阅Base64编码过程后,突发奇想:能否做一个Base16算法? ...
- prototype原型链继承
依旧是恶补js基础,上代码: 1.定义父类及父类方法 function Animal(){ this.name = "动物"; } Animal.prototype.eat = f ...
- 04_过滤器Filter_02_Filter解决中文乱码问题
[过滤器解决中文乱码问题实例] [工程截图] [web.xml] <?xml version="1.0" encoding="UTF-8"?> &l ...
- time.h文件中包含的几个函数使用时须注意事项
time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...