区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个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(线段树)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  9. SPOJ GSS2 Can you answer these queries II

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

  10. 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. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  2. selenium自动追踪微信小程序审核方案

    小程序随着腾讯的不断推广,变的越来越普及,同时更新迭代的速度也越来越快,种类越来越多,那么在如何保证时效性就显得尤为重要,其中很重要一个环节就在于小程序审核通过之后,能否立刻通知到相关技术人员进行发布 ...

  3. GIt学习第二天之版本回退、工作区和暂存区

    搬运自 ‘廖雪峰的官方网站’ 地址:https://www.liaoxuefeng.com/ 1.版本回退 在Git中,我们用git log命令显示从最近到最远的提交日志,如果嫌输出信息太多,看得眼花 ...

  4. 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  5. Linux命令应用大词典-第19章 文件系统管理

    19.1 mkfs:创建Linux文件系统 19.2 mke2fs:创建ext2.3.4文件系统 19.3 mkfs.ext4:创建ext4文件系统 19.4 mkfs.ext3:创建ext3文件系统 ...

  6. 第3章 TCP协议详解

    第3章 TCP协议详解 3.1 TCP服务的特点 传输协议主要有两个:TCP协议和UDP协议,TCP协议相对于UDP协议的特点是 面向连接使用TCP协议通信的双方必须先建立连接,完成数据交换后,通信双 ...

  7. 如何在线测试Exchange的速度

    最新碰到了客户需要比较国内版和国际版的Office365的速度问题,微软提供在线工具测试 这里以Exchange 测试为例子,请参考. PS Onenote贴过来只能至图片,各位看官只能将就了 这里有

  8. Redis+Keepalived高可用方案详细分析

    背景 目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中.且目前官方开发中的Redis Cluster提供的功能尚不完善(可参考官方网站或http://www.redisdoc.com/ ...

  9. mysql 只返回一条数据

    问题描述: 需要得到时间最近的一条记录,但是按照时间字段排完序之后,得到的是全部. 解决办法: order by createtime desc //降序:asc:升序 LIMIT 1

  10. 从零讲JAVA ,给你一条 清晰地学习道路!该学什么就学什么!!

    1.计算机基础: 1.1数据机构基础: 主要学习:1.向量,链表,栈,队列和堆,词典.熟悉2.树,二叉搜索树.熟悉3.图,有向图,无向图,基本概念4.二叉搜索A,B,C类熟练,9大排序熟悉.5.树的前 ...