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 ...
随机推荐
- Python爬虫快速上手教程
1 这个是什么 整理Python中requests常用的API 2 代码 from bs4 import BeautifulSoup import requests import re ...
- a 链接锚点
建立锚点的元素必须要有 id 或 name 属性,最好两个都有. 锚点超链接一定包含井号 "#",锚点超链接都在链接的最末端,具体看后面例子: 同一个页面不同部分的跳转 目标元素: ...
- "超时时间已到。在操作完成之前超时"的解决思路
错误往往是数据库操作超时引起 1.检查数据库访问连接字符串启用连接池,若是,适当增大超时时间 2.ADO sqlcommand相应调整超时时长 3.关键在于优化数据库操作,优化压缩执行时间
- Java Swing 资料(转载学习)
Swing图像界面简介:https://blog.csdn.net/xietansheng/article/details/72814531 Swing实用经验总结篇:https://blog.csd ...
- js 使用 "use strict"
"use strict"是JavaScript中一个非常好的特性,而且非常容易使用. 使用方法 // file.js "use strict" function ...
- Android数据存储原理分析
Android上常见的数据存储方式为: SharedPreferences是 Android 中比较常用的存储方法,本篇将从源码角度带大家分析一下Android中常用的轻量级数据存储工具SharedP ...
- oracle 中查询当前用户可以看到的表名、表对应的所有字段 原
转自:https://my.oschina.net/u/3783799/blog/2870207 1.oracle 查询当前用户下的表名,表注释 select t.table_name, f.comm ...
- 如何在SAP UI5应用里添加使用摄像头拍照的功能
昨天Jerry的文章 纯JavaScript实现的调用设备摄像头并拍照的功能 介绍了纯JavaScript借助WebRTC API来开发支持调用设备的摄像头拍照的web应用.而我同事遇到的实际情况是, ...
- Python学习日记(二十四) 继承
继承 什么是继承?就是一个派生类(derived class)继承基类(base class)的字段和方法.一个类可以被多个类继承;在python中,一个类可以继承多个类. 父类可以称为基类和超类,而 ...
- Linux命令——systemctl
前言 systemctl本身的意义并不仅仅是一个命令那么简单,他标志着SysV时代的终结,Systemd时代的开始.CentOS 7.X系列已经抛弃SysV,全面拥抱Systemd这个init sys ...