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]| ≤ ...
随机推荐
- [转] 引用 Java自带的线程池ThreadPoolExecutor详细介绍说明和实例应用
PS: Spring ThreadPoolTaskExecutor vs Java Executorservice cachedthreadpool 引用 [轰隆隆] 的 Java自带的线程池Thre ...
- My97DatePicker日期控件使用方法
My97DatePicker是一款网页版非常简单而且好用的日期控件,其实几年前就使用过了,这次再次用到,总结下: 首先去官网下载地址:http://www.my97.net/dp/down.asp 在 ...
- DES加密系统的实现
这是一个高内聚低耦合可复用的DES加密系统的实现. Github 链接:https://github.com/cyendra/CyDES 要实现加密系统,先考虑数据的基本单位. 在DES加密中,数据是 ...
- WebWork2和Spring MVC Framework的比较
http://daihaixiang.blog.163.com/blog/static/3830134200711411515336/ WebWork2和Spring MVC Framework的比较 ...
- apparmor介绍
AppArmor AppArmor 类似于selinux ,主要的作用是设置某个可执行程序的访问控制权限,主要区别就在于apparmor是以路径(path)为基础,而selinux以i节点(inode ...
- 类型与通用语言运行时:System.Object
CLR 要求每个类型最终都要继承自 System.Object 类型 //隐式继承 Object class Employee { ... } //显示继承继承 Object class Employ ...
- IOS之沙盒(Sandbox)机制
IOS中每个App应用程序都有一个单独封闭的文件夹,这个文件夹称为沙盒,并且苹果规定,任何App都无权访问其他App的沙盒 沙盒目录通过 FOUNDATION_EXPORT NSString *NSH ...
- 在Windows下用gSoap实现简单加法实例
实现一个简单的a+b程序,在服务器端写一个程序,里面包含了a+b的函数,然后通过客户端代码向其发送两个数字,在服务器运算得到结果返回给客户端显示出来. 1.在gSoap的官网上下载文件夹,本人的版本是 ...
- (转)C++设计模式——观察者模式
转自:http://www.jellythink.com/archives/359 前言 之前做了一个性能测试的项目,就是需要对现在的产品进行性能测试,获得测试数据,然后书写测试报告,并提出合理化的改 ...
- linux根目录详解
ubuntu 文件说明:http://tech.ccidnet.com/art/302/20080118/1347213_1.html/ 根目录 | |-boot/ 启动文件.所有与系统启动有关的 ...