【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]| ≤ ...
随机推荐
- NB-IOT连接移动onenet平台流程
1. 先创建账号,然后创建产品 2. 创建设备,用AT+CGSN和AT+CIMI查询NB-IOT的IMEI和IMSI填写上去. 3. 创建好的设备.
- Java并发编程系列一:Future和CompletableFuture解析与使用
一.Future模式 Java 1.5开始,提供了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果. Future接口可以构建异步应用,是多线程开发中常见的设计模式. 当 ...
- 移动性能测试之gemebench安装
越来越多的人从事各种移动端性能测试,但工具和文档的资料却相对较少,这两天需要测试一款APP的性能,就来先简单介绍下gamebench的安装吧! 作为国人来说,使用gamebench还是有相当多的坑点: ...
- tpo-08 C2 A strategy for attracting customers
第 1 段 1.Listen to a conversation between a student and a business professor. 听一段学生和教授的对话. 第 2 段 1.So ...
- python学习笔记03 --------------程序交互与格式化输出
1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...
- org.apache.spark.sql.functions汇总
测试数据: id,name,age,comment,date 1,lyy,28,"aaa bbb",20180102020325 scala> var data = spar ...
- Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking---随笔
Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking DCF跟踪算法因边界效应,鲁棒性较差.SRD ...
- 一:yarn 介绍
yarn的了出现主要是为了拆分jobtracker的两个核心功能:资源管理和任务监控,分别对应resouceManager(RM)和applicationManager(AM).yarn中的任 ...
- HDU 4617 Weapon(三维几何)
Problem Description Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a c ...
- wpa_supplicant上行接口浅析
摘自http://blog.csdn.net/fxfzz/article/details/6176414 wpa_supplicant提供的接口 从通信层次上划分, 上行接口:wpa_supplica ...