另一个画风的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 ...
随机推荐
- Delphi中Chrome Chromium、Cef3学习笔记(一)
原文 http://blog.csdn.net/xtfnpgy/article/details/46635225 官方下载地址:https://cefbuilds.com/ CEF简介: 嵌入 ...
- STL里的内存池实现
这个貌似有点复杂,解决的主要问题 就是 减少 内存分配次数,减少用户态核心态切换中断次数,提高运行速度,预分配 和线程池一个道理,预分配 ////////////////////自由链表 union ...
- 微软microsoft word的api文档地址
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa172758(v%3doffice. ...
- list中null或者空的区别
1.list 空 默认值是空,即没有值 null 理解为没有对list集合分配内存空间,实际上压根就不存在. 也可以这样理解: null 未建立对象 空 建立对象未放入值 例如 我有一个空着 ...
- cdnbest配置强制ssl跳转
如何配置强制ssl跳转 1. 登陆用户站点,点击下图图标: 2. 如下图添加证书和开启强制ssl即可 hsts解释和作用: 国际互联网工程组织IETF正在推行一种新的Web安全协议HTTP Stric ...
- TP-Link 路由器配置 config.bin 解密工具 python 代码 (转载)
转自吾爱破解 https://www.52pojie.cn/forum.php?mod=viewthread&tid=574222 需要安装crypto #!/usr/bin/env pyth ...
- Python学习—基础篇之基本数据类型(一)
数据类型 在python中,能够直接处理的基本数据类型主要有数字类型.字符串类型.字节类型.布尔类型.列表.元组.字典.集合等. 一.数字类型 1.1 数字类型的创建 # 数字类型 a = 10 b ...
- I/O系统(一)
输入输出系统的发展大致可以分为4个阶段1.早期阶段 特点: 1.1每个IO设备都得有一套独立的逻辑电路和CPU相连. 1.2输入输出过程需要通过CPU,穿插在程序运行的过程中,处理IO时候 ...
- RFID数据清洗与数据清洗的区别
RFID数据清洗和一般数据清洗的不同: RFID数据清洗已经跨越到硬件范畴!造成脏数据的原因是硬件原理和硬件所处环境本身!要提高RFID数据清洗能力,就必须同时研究技术原理和环境本身之间的互动关系,而 ...
- FortiGate 5.2/5.4 SSLVPN建立
1.定义源IP池 即用户通过sslvpn拨号成功后获取到的IP地址. 2.定义路由地址 即用户通过sslvpn拨号成功后允许获取到的路由表. 3.建立sslvpn portal 4.定义用户和用户组 ...