GSS1 A - Can you answer these queries I

//题意:询问一段区间的最大子序列的值。 //做法:维护四个值:包含当前区间左端点的最大子区间LM,包含当前区间右端点的最大子区间RM、当前区间的最大子区间M, 当前区间的区间和S
//tree[root].maxn=max(tree[root<<1].maxn,max(tree[root<<1|1].maxn,tree[root<<1].rmaxn+tree[root<<1|1].lmaxn));
// tree[root].lmaxn=max(tree[root<<1].lmaxn,tree[root<<1].sum+tree[root<<1|1].lmaxn);
// tree[root].rmaxn=max(tree[root<<1|1].rmaxn,tree[root<<1|1].sum+tree[root<<1].rmaxn);
// tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int N=5e4+; int n,m,l,r;
int L,R,M,S;
struct Tree
{
int l,r,mid;
int sum,lmaxn,rmaxn,maxn;
}tree[N<<]; int read()
{
char c=getchar();int num=,f=;
for(;!isdigit(c);c=getchar())
if(c=='-')
f=-;
for(;isdigit(c);c=getchar())
num=num*+c-'';
return num*f;
} void pushup(int root)
{
tree[root].maxn=max(tree[root<<].maxn,max(tree[root<<|].maxn,tree[root<<].rmaxn+tree[root<<|].lmaxn));
tree[root].lmaxn=max(tree[root<<].lmaxn,tree[root<<].sum+tree[root<<|].lmaxn);
tree[root].rmaxn=max(tree[root<<|].rmaxn,tree[root<<|].sum+tree[root<<].rmaxn);
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
} void build(int root,int l,int r)
{
tree[root].l=l,tree[root].r=r,tree[root].mid=l+r>>;
if(l==r)
{
tree[root].sum=tree[root].maxn=tree[root].rmaxn=tree[root].lmaxn=read();
return;
}
build(root<<,l,tree[root].mid);
build(root<<|,tree[root].mid+,r);
pushup(root);
} void query(int root,int l,int r,int &L,int &R,int &M,int &S)
{
if(l<=tree[root].l&&tree[root].r<=r)
{
L=tree[root].lmaxn,
R=tree[root].rmaxn,
M=tree[root].maxn,
S=tree[root].sum;
return;
}
if(tree[root].mid>=r)
{
query(root<<,l,r,L,R,M,S);
}
else if(tree[root].mid<l)
{
query(root<<|,l,r,L,R,M,S);
}
else
{
int lL=,lR=,lM=,lS=,rL=,rR=,rM=,rS=;
query(root<<,l,tree[root].mid,lL,lR,lM,lS);
query(root<<|,tree[root].mid+,r,rL,rR,rM,rS);
L=max(lL,lS+rL);
R=max(rR,rS+lR);
M=max(lR+rL,max(lM,rM));
S=lS+rS;
}
} int main()
{
n=read();
build(,,n);
m=read();
for(int i=;i<=m;++i)
{
l=read(),r=read();
query(,l,r,L,R,M,S);
printf("%d\n",M);
}
return ;
}
GSS1 A - Can you answer these queries I的更多相关文章
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- 线段树 SP1043 GSS1 - Can you answer these queries I
SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...
- 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]| ≤ ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...
- 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 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- .net core使用ocelot---第五篇 服务质量
简介 .net core使用ocelot---第一篇 简单使用 .net core使用ocelot---第二篇 身份验证使用 .net core使用ocelot---第三篇 日志记录 .net c ...
- svg实现圆环进度条
开源实现:https://github.com/lugolabs/circles 自行实现:圆环与svg画布间剩的空间太多. <!DOCTYPE html> <html> &l ...
- 前端以及django零碎补充
前端 1. js的循环each 前端需要循环,可以借助each这个方法 var dic = {'key1':'value1','key2':'value2'}; ']; //each(变量, 方法) ...
- 在angular 8中使用 less
在angular 6中使用 less 新项目 ng new [appname] --style less 已有的项目 修改 *.css 文件及引用处后缀名为 less并在 angular.json 文 ...
- Redis除了做缓存--Redis做消息队列/Redis做分布式锁/Redis做接口限流
1.用Redis实现消息队列 用命令lpush入队,rpop出队 Long size = jedis.lpush("QueueName", message);//返回存放的数据条数 ...
- java web编程 servlet读取配置文件参数
新建一个servlet. 然后在web.xml文件里面自动帮助你创建好了<servlet-name><servlet-class><servlet-mapping> ...
- Flask之threading.loacl方法
一.threading.loacl方法 import threading import time class Foo: pass foo = Foo() def func(num): foo.num ...
- 在openwrt上使用autossh(已放弃)
用了一天后发现,这东西真不靠谱,还不如自已写的SHELL检测重连来的精准和方便,放弃中 参考文章: https://my.oschina.net/umu618/blog/849345 https:// ...
- k8s 如何支持私有镜像
k8s如何支持私有镜像 实现无密钥编排 kubectl create secret docker-registry regsecret --docker-server=registry.cn-shen ...
- MySQL 5.7主从复制实战篇
MySQL 5.7主从复制实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装MySQL数据库并启动 1>.在MySQL官方下载相应的安装包(https://dev ...