SPOJ GSS2 Can you answer these queries II ——线段树
【题目分析】
线段树,好强!
首先从左往右依次扫描,线段树维护一下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 ——线段树的更多相关文章
- 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 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]| ≤ ...
随机推荐
- sql 函数 coalesce
SQL函数 coalesce 功能: 返回参数中第一个非null的值. 语法: coalesce(参数1,参数2,参数3,...);返回第一个非null的值. 一般情况下会与Nullif()函数一起使 ...
- JAVA的程序基本结构和数据类型
//源程序 Hello.java public class Hello { static String str ="Hello World"; public static void ...
- SQL2005中使用backup、restore来备份和恢复数据库
在SQL2005数据库中利用SQL语句进行数据备份与还原: 备份backup:backup database 数据库名称 tO disk = 备份路径例:BACKUP DATABASE test TO ...
- Xamarin.IOS binding库编译失败的解决办法
报错:目标框架 Xamarin.iOS,Version=v1.0 未找到 复制 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professi ...
- Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()
为什么使用Python-OpenCV? 虽然python 很强大,而且也有自己的图像处理库PIL,但是相对于OpenCV 来讲,它还是弱小很多.跟很多开源软件一样OpenCV 也提供了完善的pytho ...
- 单源最短路SPFA
#include<iostream> #include<queue> #include<cstring> #define INF 0x3f3f3f3f using ...
- jsTree展开根节点 设置用户图标
$("#jstree").on("loaded.jstree", function (event, data) { var n = 0; var root = ...
- ios之UIPageControl
分页控件是一种用来取代导航栏的可见指示器,方便手势直接翻页,最典型的应用便是iPhone的主屏幕,当图标过多会自动增加页面,在屏幕底部你会看到原点,用来只是当前页面,并且会随着翻页自动更新. 一.创建 ...
- Python使用三种方法实现PCA算法[转]
主成分分析(PCA) vs 多元判别式分析(MDA) PCA和MDA都是线性变换的方法,二者关系密切.在PCA中,我们寻找数据集中最大化方差的成分,在MDA中,我们对类间最大散布的方向更感兴趣. 一句 ...
- Python变量、常量、数据类型
1. 变量 变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储Script运行时可更改的程序信息.例如,可以创建一个名为ClickCount的变量来存储用户单击Web页面上某个对象的次 ...