【题目分析】

线段树,好强!

首先从左往右依次扫描,线段树维护一下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. [翻译] API测试的最佳实践 - 介绍

    API测试的最佳实践 - 介绍 在上一篇“是什么让API测试很叼”一文中,我们讨论API与其他形式的软件测试的差异.部分是因为API之间的通信压根就没考虑让你能读懂,纯粹是为了方便计算机之间的交互而设 ...

  2. SQL简单查询后续记录

    --首先创建数据库TEST CREATE DATABASE TEST --创建表tb_user USE TEST CREATE TABLE [tb_user]( [name] [nvarchar] ( ...

  3. 当ThreadLocal碰上线程池

    ThreadLocal使用 ThreadLocal可以让线程拥有本地变量,在web环境中,为了方便代码解耦,我们通常用它来保存上下文信息,然后用一个util类提供访问入口,从controller层到s ...

  4. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  5. MIPS简单入门

    What ‘s the MIPS? 汇编语言 汇编语言是一类语言的总称,因处理器不同,而对应的不同机器的指令集也不同,产生了很多种汇编语言. 目前最流行的是ARM,MIPS,x86.ARM用于大量的移 ...

  6. 1653: Champion of the Swordsmanship

    1653: Champion of the Swordsmanship Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 11  Solved: 8[Subm ...

  7. python爬虫---实现项目(三) Selenium分析美团美食

    上一期博客,我本来想爬取美团美食的,但是由于请求头太复杂,没有破解开其中的几个参数,所以放弃,这次我们来用selenium来模式浏览器抓取数据,我们先来简单看一下流程: 1,利用selenium驱动浏 ...

  8. 给 MSYS2 添加国内源

    https://wiki.qt.io/MSYS2pacman -S base-devel git mercurial svn wget p7zip软件包 开发包 http://mirrors.ustc ...

  9. 服务器配置iis,php网站

    1.在iis中选择物理路径.配置域名 2.添加php默认文档 3.修改处理程序映射 4.设置模块映射信息

  10. js 一维数组转成tree 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...