【SPOJ - GSS2】Can you answer these queries II(线段树)
区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 150000
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define LL long long
using namespace std; typedef struct {
LL nmax,hmax,nlazy,hlazy;
}Tree;
Tree tree[maxn*]; typedef struct {
int l,r,id;
} Que;
Que que[maxn*]; LL num[maxn*],ans[maxn*];
int n,m,pre[maxn*]; int cmp(Que x,Que y)
{
if (x.r<y.r) return ;
return ;
} void update(int x)
{
tree[x].nmax=max(tree[x<<].nmax,tree[x<<|].nmax);
tree[x].hmax=max(tree[x<<].hmax,tree[x<<|].hmax);
} void pushdown(int x)
{
if (tree[x].hlazy) {
tree[x<<].hmax=max(tree[x<<].hmax,tree[x<<].nmax+tree[x].hlazy);
tree[x<<|].hmax=max(tree[x<<|].hmax,tree[x<<|].nmax+tree[x].hlazy);
tree[x<<].hlazy=max(tree[x<<].hlazy,tree[x].hlazy+tree[x<<].nlazy);
tree[x<<|].hlazy=max(tree[x<<|].hlazy,tree[x].hlazy+tree[x<<|].nlazy);
tree[x].hlazy=;
}
if (tree[x].nlazy) {
tree[x<<].nmax=tree[x<<].nmax+tree[x].nlazy;
tree[x<<|].nmax=tree[x<<|].nmax+tree[x].nlazy;
tree[x<<].nlazy+=tree[x].nlazy;
tree[x<<|].nlazy+=tree[x].nlazy;
tree[x].nlazy=;
}
} void change(int x,int l,int r,int ll,int rr,LL y)
{
if (ll<=l && r<=rr) {
tree[x].nlazy+=y;
tree[x].nmax+=y;
tree[x].hlazy=max(tree[x].hlazy,tree[x].nlazy);
tree[x].hmax=max(tree[x].nmax,tree[x].hmax);
return;
}
pushdown(x);
int mid=(l+r)>>;
if (ll<=mid) change(x<<,l,mid,ll,rr,y);
if (rr>mid) change(x<<|,mid+,r,ll,rr,y);
update(x);
} LL ask(int x,int l,int r,int ll,int rr)
{
// printf("%d %d %d %lld %lld\n",x,l,r,tree[x].hmax,tree[x].nmax);
if (ll<=l && r<=rr) return tree[x].hmax;
pushdown(x);
int mid=(l+r)>>;
if (rr<=mid) return ask(x<<,l,mid,ll,rr);
else
if (ll>mid) return ask(x<<|,mid+,r,ll,rr);
else
return max(ask(x<<,l,mid,ll,mid),ask(x<<|,mid+,r,mid+,rr));
} void build(int x,int l,int r)
{
tree[x].hlazy=tree[x].nlazy=;
if (l==r) {
scanf("%lld",&num[l]);
tree[x].hmax=tree[x].nmax=;
return;
}
int mid=(l+r)>>;
if (l<=mid) build(x<<,l,mid);
if (mid<r) build(x<<|,mid+,r);
update(x);
} int main()
{
scanf("%d",&n);
build(,,n);
scanf("%d",&m);
rep(i,,m-) {
scanf("%d %d",&que[i].l,&que[i].r);
que[i].id=i;
}
sort(que,que+m,cmp);
memset(pre,,sizeof(pre));
int now=;
rep(i,,n) {
change(,,n,pre[num[i]+maxn]+,i,num[i]);
pre[num[i]+maxn]=i;
while (now<=m && que[now].r==i) {
ans[que[now].id]=ask(,,n,que[now].l,que[now].r);
++now;
}
}
rep(i,,m-) printf("%lld\n",ans[i]);
return ;
}
【SPOJ - GSS2】Can you answer these queries II(线段树)的更多相关文章
- 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 ...
- 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]| ≤ ...
随机推荐
- Python Map 并行
Map是一个酷酷的小东西,也是在Python代码轻松引入并行的关键.对此不熟悉的人会认为map是从函数式语言(如Lisp)借鉴来的东西.map是一个函数 - 将另一个函数映射到一个序列上.例如: ur ...
- 虚拟机克隆CentOs后网卡问题
1.直接修改 /etc/sysconfig/network-scripts/ifcfg-eth0 删掉UUID HWADDR配置静态地址 2.修改配置文件vi /etc/udev/rules.d/7 ...
- Python入门编程中的变量、字符串以及数据类型
//2018.10.10 字符串与变量 1. 在输出语句中如果需要出现单引号或者双引号,可以使用转义符号\,它可以将其中的歧义错误解释化解,使得输出正常: 2. 对于python的任何变量都需要进行赋 ...
- UE4蓝图小记
http://www.element3ds.com/forum.php?mod=viewthread&tid=76930&page=1&authorid=104414 http ...
- Unity 特殊目录
其他目录 Application.persistentDataPath:webGL平台只能使用这个
- ADO.NET基础学习-----四种模型,防止SQL注入
1.ExcuteNonQuery 执行非查询语句,返回受影响的行数. // 1.ExcuteNonQuery string sqlconn = "Data Source=wss;Initia ...
- 博客更换至 www.zhaoch.top
博客更换至 www.zhaoch.top 随手拷了一些链接 http://www.zhaoch.top/操作系统/linux/常用命令备忘.html http://www.zhaoch.top/操作系 ...
- Docker Remote API v1.24
1. Brief introduction The Remote API has replaced rcli. The daemon listens on unix:///var/run/docker ...
- 什么是Spark
什么是Spark Apache Spark是一个开源集群运算框架, 相对于Hadoop的MapReduce会在运行完工作后将中介数据存放到磁盘中,Spark使用了存储器内运算技术,能在数据尚未写入硬盘 ...
- AC 自动机——多模式串匹配
网站上的敏感词过滤是怎么实现的呢? 实际上,这些功能最基本的原理就是字符串匹配算法,也就是通过维护一个敏感词的字典,当用户输入一段文字内容后,通过字符串匹配算法来检查用户输入的内容是否包含敏感词. B ...