【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]| ≤ ...
随机推荐
- JDK1.8改为JDK1.7过程
电脑之前eclipse版本要求JDK1.8版本,现在要用jboss7.1做性能测试,目前仅支持JDK7.故需要降级. 网上有很多说把1.8删掉,这种做法我是不建议的,那么要用的时候呢?又得装回来多蛋疼 ...
- C++11 type_traits 之is_same源码分析
请看源码: template<typename _Tp, _Tp __v> struct integral_constant { static const _Tp value = __v; ...
- Python数学运算入门把Python当作计算器
让我们尝试一些简单的 Python 命令.启动解释器,等待界面中的提示符,>>> (这应该花不了多少时间). 3.1.1. 数字 解释器就像一个简单的计算器一样:你可以在里面输入一个 ...
- ionic LoadingController 模块使用
html 代码: <ion-header> <ion-navbar> <ion-title>Loading</ion-title> </ion-n ...
- @meida 媒体查询
示例 @meida 媒体查询 在进行书写的时候需要考虑到加载顺序和样式权重使用meida响应式实现不同宽度布局示例 常用工具 https://mydevice.io 参考链接 https://deve ...
- 剑指offer-从上往下打印二叉树22
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom( ...
- 01背包问题:DP
题目描述: 有 N 件物品和一个容量是 V 的背包.每件物品只能使用一次. 第 i 件物品的体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大.输出 ...
- NMAP-主机扫描
1.全面扫描 2.扫描指定段 3.ping扫描 只进行ping操作,十分隐蔽 4.无ping扫描 适用于防火墙禁止ping 5.TCP SYN扫描 6.TCP ACK扫描 7.UDP扫描 8.ICMP ...
- 《剑指Offer》题三十一~题四十
三十一.栈的压入.弹出序列 题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的数字均不相等.例如,序列{1, 2, 3, 4 ,5}是某栈的压栈序列 ...
- Thunder团队第五周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:胡佑蓉 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...