【题目分析】

线段树,好强!

首先从左往右依次扫描,线段树维护一下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. setuid

    -r-s--x--x   #s就是setuid,仅可用在二进制文件,对目录设置无效

  2. mac下相关操作命令

    查看端口使用情况 lsof -i tcp:

  3. HDU 1171 Big Event in HDU 杭电大事件(母函数,有限物品)

    题意: 分家问题,对每种家具都估个值,给出同样价值的家具有多少个,要求尽可能平分,打印的第一个数要大于等于第二个数. 思路: 可以用背包做,也可以用母函数.母函数的实现只需要注意一个点,就是每次以一种 ...

  4. COGS 1710. [POJ2406]字符串的幂

    ★☆   输入文件:powerstrings.in   输出文件:powerstrings.out   简单对比时间限制:3 s   内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...

  5. HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场

    题意: 给出一个仙人掌图,然后求他的前K小生成树. 思路: 先给出官方题解 由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉.所以问题就变为有 M 个集合,每个集合里面都有一堆 ...

  6. cmake 指定输出目录

    $ mkdir ~/cpp-netlib-build $ cd ~/cpp-netlib-build $ cmake -DCMAKE_BUILD_TYPE=Debug \ > -DCMAKE_C ...

  7. Linux C++/C开发所必需的一系列工具

    系统平台下的开发工具.开发环境各有不同.Linux C++/C开发所必需的一系列工具: 1. vi(vim)文本编辑器一个UNIX世界标准的文本编辑器,简约而强大,不论作为开发人员还是系统管理员,熟练 ...

  8. 什么是无符号段整数,什么又是有符号数,(c++与java语言里边的不同)

    c++中:整型数分为有符号数和无符号数两种 unsigned int a;无符号整型变量a,意思是这个数最小值为0,最大值为2的32次方-1,因为一个整型数占四个字节,一个字节8位,共32位 int ...

  9. 安装ruby开发环境

    如何快速正确的安装 Ruby, Rails 运行环境 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 ...

  10. Clover KextsToPatch 使用方法 2015.10.21

    Clover KextsToPatch 使用方法 2015.10.21   前些天,因为 Thinkpad X230 BIOS 白名单限制,给她换了一块 ar9285 无线网卡,只是因为这块网卡正好可 ...