另一个画风的GSS1 - Can you answer these queries I(猫树)
前言
其实我觉得你看猫锟的解释也看不懂(主要是还有一些不良心的讲解者不讲清楚,当然这里不是针对了qwq)
猫锟链接
Solution
考虑我们的线段树是个啥玩意?
每一层都是一堆区间叠在一起。
我们在每一个节点维护的又是什么?
左边的max,右边的max,中间的max,还有sum。
那么我们改变一下:
令\(p_{dps,i}\)表示在深度为\(dps\)的线段树上\(i\)这个节点所在区间的左边的max,右边的max,然后就可以在\(build\)的时候求
再令\(p_{dps,i}\)表示在深度为\(dps\)的线段树上\(i\)这个节点所在区间的到中间的\(max\),然后也可以在\(build\)的时候求。
然后就可以\(\Theta(1)\)的询问就好了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=500010;
int pos[N],p[21][N],s[21][N],a[N],Log2[N<<3],n;
void build(int o,int l,int r,int dps)
{
if(l==r)
{
pos[l]=o;
return;
}
int mid=(l+r)>>1,pre,sm;
p[dps][mid]=s[dps][mid]=sm=pre=a[mid];
if(sm<0)sm=0;
for(int i=mid-1;i>=l;i--)
{
sm+=a[i];pre+=a[i];
s[dps][i]=max(s[dps][i+1],pre);
p[dps][i]=max(p[dps][i+1],sm);
if(sm<0)sm=0;
}
p[dps][mid+1]=s[dps][mid+1]=sm=pre=a[mid+1];
if(sm<0)sm=0;
for(int i=mid+2;i<=r;i++)
{
sm+=a[i];pre+=a[i];
s[dps][i]=max(s[dps][i-1],pre);
p[dps][i]=max(p[dps][i-1],sm);
if(sm<0)sm=0;
}
build(o<<1,l,mid,dps+1);
build(o<<1|1,mid+1,r,dps+1);
}
int query(int l,int r)
{
if(l==r)return a[l];
int dps=Log2[pos[l]]-Log2[pos[l]^pos[r]];
return max(max(p[dps][l],p[dps][r]),s[dps][l]+s[dps][r]);
}
int main()
{
n=gi();
for(int i=1;i<=n;i++)a[i]=gi();
int L=2;
while(L<n)L<<=1;
for(int i=2;i<=L<<1;i++)Log2[i]=Log2[i>>1]+1;
build(1,1,L,1);
int m=gi();
while(m--)
{
int l=gi(),r=gi();
printf("%d\n",query(l,r));
}
return 0;
}
另一个画风的GSS1 - Can you answer these queries I(猫树)的更多相关文章
- 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[线段树]
Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- SP1043 GSS1 - Can you answer these queries I 线段树
问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...
- 线段树 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
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- SP1043 GSS1 - Can you answer these queries I(猫树)
给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...
- 题解【SP1043】 GSS1 - Can you answer these queries I
题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...
随机推荐
- 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 1.cv2.resize(图片压缩) 2..get_shape()[1:4].num_elements(获得最后三维度之和) 3.saver.save(训练参数的保存) 4.tf.train.import_meta_graph(加载模型结构) 5.saver.restore(训练参数载入)
1.cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR) 参数说明:image表示输入图片,image_size表示变 ...
- vue浏览器滚动加载更多
created () { var that = this; window.addEventListener('scroll',this.scroll,true) console.log(this.$r ...
- MFC笔记8
1.在循环使用数组时需要清理数组 CString str; memset(str,0,strlen(str)); 判断两个字符串包含数字大小是否相等 CString str="22" ...
- python--第九天总结
python 多进程和多线程 多线程可以共享全局变量,多进程不能.多线程中,所有子线程的进程号相同:多进程中,不同的子进程进程号不同. [多进程] Python在2.6引入了多进程的机制,并提供了丰富 ...
- mysql学习笔记--数据库设计
一.数据库基本概念 1. 关系:两个表的公共字段 2. 行:也称记录,也称实体 3. 列:也称字段,也称属性 4. 数据冗余:相同的数据存在不同的地方. 注意:冗余只能减少,不能杜绝. 减少冗余,只能 ...
- 【测试工具】Macaca 自动遍历器 NoSmoke
Macaca 提供的基础能力上研发出了一套多端深度遍历爬虫工具. 希望可以最大化减少UI 测试脚本的编写涵盖以下功能点: 支持iOS, Android,PC-Web 三个平台的自动化测试 同时可以通过 ...
- VS打开SSAS或SSIS报错的解决办法
---------------------------Microsoft Visual Studio---------------------------无法加载类型为“Microsoft.Analy ...
- 联想RD450带Read10服务器操作系统密码忘记
联想RD450带Read10服务器操作系统密码忘记 可以用U盘PE进入重写密码 按F1进入BIOS界面设置如下 进入BOOT选项卡,设置U盘第一启动,也就是图中 Boot Option #1 设置为 ...
- Yii2.0关闭自带的debug功能
1.找到相应模块的config文件夹的main-local.php文件注释相关代码,如下: 2.将web下面的两个入口文件改成false index.php index-test.php
- node.js中fs文件系统模块的使用
node.js中为我们提供了fs文件系统模块,实现对文件或目录的创建,修改和删除等操作. fs模块中,所有的方法分为同步和异步两种实现. 有 sync 后缀的方法为同步方法,没有 sync 后缀的方法 ...