【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]| ≤ ...
随机推荐
- MySQL高级-索引优化
索引失效 1. 2.最佳左前缀法则 4. 8. 使用覆盖索引解决这个问题. 二.索引优化 1.ORDER BY 子句,尽量使用Index方式排序,避免使用FileSort方式排序 MySQL支持两种方 ...
- jquery 点滴
jQuery——动态给表格添加序号 $(function(){ //$('table tr:not(:first)').remove(); var len = $('table tr').length ...
- 「日常训练」All Friends(POJ-2989)
题意 分析 代码 #include <iostream> #include <cstring> #include <algorithm> #define MP ma ...
- Qt 独立运行时伴随CMD命令窗口
用Qt写了一个小软件,在把程序release后,打包分装后,发现程序运行的时候会伴随cmd命令窗口,可把我愁怀了 不过功夫不负有心人,在老师和我网友的帮助下,终于搞完了 CONFIG:指定工程配置和编 ...
- 第一篇 Flask基础篇之(配置文件,路由系统,模板,请求响应,session&cookie)
Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- tensorflow模型持久化保存和加载--深度学习-神经网络
模型文件的保存 tensorflow将模型保持到本地会生成4个文件: meta文件:保存了网络的图结构,包含变量.op.集合等信息 ckpt文件: 二进制文件,保存了网络中所有权重.偏置等变量数值,分 ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- c# 计算两个时间的时间差
//计算2个日期之间的天数差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDat ...
- HDU 5816 Hearthstone 概率dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5816 Hearthstone Time Limit: 2000/1000 MS (Java/Othe ...