另一个画风的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;
}

原文地址:https://www.cnblogs.com/cj-gjh/p/10268053.html

时间: 2024-08-30 18:24:31

另一个画风的GSS1 - Can you answer these queries I(猫树)的相关文章

SP1043 GSS1 - Can you answer these queries I 线段树

问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ,向上合并即可. 但是注意询问过程中也需要维护这些信息. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; template <typename Tp> void read(Tp &x){ x=0;ch

SP1716 GSS3 - Can you answer these queries III 线段树

题目传送门:SP1043 GSS1 - Can you answer these queries I 更好的阅读体验 动态维护子段和最大值 前置知识 静态维护子段和最大值:SP1043 GSS1 - Can you answer these queries I 题解传送 题解: 提供结构体指针线段树写法: 设\(l\)为区间左端点, \(r\)为区间右端点: \(ls\)为以\(l\)为左端点的最大子段和, \(rs\)为以\(r\)为右端点的最大子段和; \(sum\)为区间和, \(val\

hdu4027 Can you answer these queries?(线段树平方减少,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 Problem Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the bat

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的数开几次方之后都会变成1,所以到了1不用没完没了的更新. 1 //HDU 4027 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <iostream> 6 #defi

SPOJ GSS1 Can you answer these queries I

Can you answer these queries I Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: GSS164-bit integer IO format: %lld      Java class name: Main You are given a sequence $A[1], A[2], ..., A[N] $. $( |A[i]| \leq

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个查询,程序必须输出这些查询的结果. 这就是一个最大子段和,用线段树就能直接搞掉 然后这里学习了一下一个叫做猫树的神奇东西->这里 能做到预处理之后查询$O(1)$ 1 //minamoto 2 #include<iostream> 3 #include<cstdio> 4 us

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  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,