【题目分析】

线段树,好强!

首先从左往右依次扫描,线段树维护一下f[]。f[i]表示从i到当前位置的和的值。

然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值。

关于历史最值问题:

标记是有顺序的,如果下方标记比较勤快,使得两个标记不会叠加,常数会很大,但是好写。

发现标记随着层数的递增越来越古老,(否则就被下放了),所以维护历史最大更新和当前更新即可。

好题!

【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> #include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 1000005
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i) void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("wa.txt","w",stdout);
#endif
} int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} const int buf=200005;
int n,m,a[maxn],last[maxn],bac[maxn];
struct Que{int l,r,id,ans;}q[maxn]; struct Segment_Tree{
int L,R,C;
int old_mx[maxn],old_lazy[maxn];
int now_mx[maxn],now_lazy[maxn];
void init()
{
memset(old_mx,0,sizeof old_mx);
memset(now_mx,0,sizeof now_mx);
memset(old_lazy,0,sizeof old_lazy);
memset(now_lazy,0,sizeof now_lazy);
}
void update(int o,int l,int r)
{
old_mx[o]=max(old_mx[o<<1],old_mx[o<<1|1]);
now_mx[o]=max(now_mx[o<<1],now_mx[o<<1|1]);
}
void pushdown(int o,int l,int r)
{
old_lazy[o<<1]=max(old_lazy[o<<1],now_lazy[o<<1]+old_lazy[o]);
old_lazy[o<<1|1]=max(old_lazy[o<<1|1],now_lazy[o<<1|1]+old_lazy[o]); old_mx[o<<1]=max(old_mx[o<<1],now_mx[o<<1]+old_lazy[o]);
old_mx[o<<1|1]=max(old_mx[o<<1|1],now_mx[o<<1|1]+old_lazy[o]); now_lazy[o<<1]+=now_lazy[o];
now_lazy[o<<1|1]+=now_lazy[o]; now_mx[o<<1]+=now_lazy[o];
now_mx[o<<1|1]+=now_lazy[o]; now_lazy[o]=old_lazy[o]=0;
}
void add(int o,int l,int r)
{
if (L<=l&&r<=R)
{
old_lazy[o]=max(old_lazy[o],now_lazy[o]+=C);
old_mx[o]=max(old_mx[o],now_mx[o]+=C);
return ;
}
pushdown(o,l,r);
int mid=l+r>>1;
if (R<=mid) add(o<<1,l,mid);
else if (L>mid) add(o<<1|1,mid+1,r);
else add(o<<1,l,mid),add(o<<1|1,mid+1,r);
update(o,l,r);
}
int query(int o,int l,int r)
{
if (L<=l&&r<=R) return old_mx[o];
pushdown(o,l,r);
int mid=l+r>>1;
if (R<=mid) return query(o<<1,l,mid);
if (L>mid) return query(o<<1|1,mid+1,r);
else return max(query(o<<1,l,mid),query(o<<1|1,mid+1,r));
}
}t; bool cmp1(Que x,Que y){return x.r<y.r;}
bool cmp2(Que x,Que y){return x.id<y.id;} int main()
{
Finout();
n=Getint();
F(i,1,n) a[i]=Getint();
F(i,1,n)
{
last[i]=bac[a[i]+buf];
bac[a[i]+buf]=i;
}
m=Getint();
F(i,1,m)
{
q[i].l=Getint();
q[i].r=Getint();
q[i].id=i;
}
sort(q+1,q+m+1,cmp1);
int h=0;
F(i,1,m)
{
while (h<q[i].r&&h<=n)
{
h++;
t.L=last[h]+1;
t.R=h;
t.C=a[h];
// printf("Add %d %d %d\n",t.L,t.R,t.C);
t.add(1,1,n);
}
t.L=q[i].l;t.R=q[i].r;
// printf("Query %d %d for %d\n",q[i].l,q[i].r,q[i].id);
q[i].ans=t.query(1,1,n);
}
sort(q+1,q+m+1,cmp2);
F(i,1,m) printf("%d\n",q[i].ans);
}

  

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

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

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

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

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

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

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

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

  8. SPOJ GSS2 Can you answer these queries II

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

  9. 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. thinkphp写的登录注册的小demo

    和asp.net类似,一个FormAction对应Form文件夹 demo结构: ‘ 对于项目结构有疑问的: http://www.thinkphp.cn/document/60.html login ...

  2. docker 配置国内镜像源 linux/mac/windows

    部分内容来自:http://guide.daocloud.io/dcs/daocloud-9153151.html 加速器官方DaoCloud承诺:加速器服务永久免费且无流量限制 使用前提:注册Dao ...

  3. fiddler+willow问题总结

    本文纯属用来记录自己学习过程中遇到的坑,如有朋友也遇到,可移步到这里查看是否为该问题导致. fiddler 安装不用说了,到官网直接去下载,自行下载最新版本 willow下载地址:http://qzo ...

  4. shell中的-z

    -z 字符串为"null",即是指字符串长度为零.

  5. Nengo 神经网络

    Nengo被加拿大滑铁卢大学的神经学家和软件工程师表示,这是迄今为止产生的世界上最复杂.最大规模的人类大脑模型模拟.这个名叫Spaun的大脑由250万 个模拟神经元组成,它能执行8种不同类型的任务.这 ...

  6. 爆零系列—补题A

    http://codeforces.com/contest/615/problem/A 读错题 结果发现是无脑题  直接标记统计 #include<cstdio> #include< ...

  7. 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  8. MATLAB——解数独

    数独 数独是一种逻辑游戏,玩家需要根据9x9盘面的已知数字,推理出剩余所有空格的数字,并满足每一行.每一列和每个粗线宫(3x3)内均含1~9,不重复. MATLAB中有关函数 M = dlmread( ...

  9. IE6 bug总结

    IE6bug总结: 1.双边距bug产生原因 margin的方向与浮动的方向相同 解决方法: 浮动的元素身上加 display:inline; ---------------------------- ...

  10. 机器学习(1)- 概述&线性回归&逻辑回归&正则化

    根据Andrew Ng在斯坦福的<机器学习>视频做笔记,已经通过李航<统计学习方法>获得的知识不赘述,仅列出提纲. 1 初识机器学习 1.1 监督学习(x,y) 分类(输出y是 ...