Can you answer these queries III(线段树)
Can you answer these queries III(luogu)
Description
维护一个长度为n的序列A,进行q次询问或操作
0 x y:把Ax改为y
1 x y:询问区间【l,r】的最大子段和
数据范围:n,q<=5e4,-1e4<=Ai<=1e4;
Solution
线段树处理区间最大子段和
- res为区间最大子串和
- sum为区间和
- prel和prer分别为从区间左端点和右端点开始的最大子串和
Code
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
const int N=5e4+;
struct node
{
ll sum,res,prel,prer;
int l,r,lc,rc;
}f[N*],t;
int opt,tot,rt,n,q,x,y;
ll d[N];
void push_up(int g)
{
int lc=f[g].lc,rc=f[g].rc;
if(!lc) return ;
f[g].sum=f[lc].sum+f[rc].sum;
f[g].prel=max(f[lc].prel,f[lc].sum+f[rc].prel);
f[g].prer=max(f[rc].prer,f[rc].sum+f[lc].prer);
f[g].res=max(max(f[lc].res,f[rc].res),f[lc].prer+f[rc].prel);
}
void build(int &g,int l,int r)
{
g=++tot;
f[g].l=l,f[g].r=r;
if(l==r)
{
f[g].sum=f[g].prel=f[g].prer=f[g].res=d[l];
return ;
}
int mid=(l+r)>>;
build(f[g].lc,l,mid),build(f[g].rc,mid+,r);
push_up(g);
}
void change(int g,int x,int y)
{
if(f[g].l==f[g].r)
f[g].sum=f[g].res=f[g].prel=f[g].prer=y;
else
{
int mid=(f[g].l+f[g].r)>>;
if(x<=mid) change(f[g].lc,x,y);
else change(f[g].rc,x,y);
push_up(g);
}
}
ll get(int g,int l,int r,node &a)
{
if(f[g].l==l && f[g].r==r)
{
a=f[g];
return a.res;
}
else
{
int mid=(f[g].l+f[g].r)>>;
if(r<=mid) return get(f[g].lc,l,r,a);
else if(l>mid) return get(f[g].rc,l,r,a);
else
{
node b,c;
get(f[g].lc,l,mid,b);
get(f[g].rc,mid+,r,c);
a.sum=b.sum+c.sum;
a.prel=max(b.prel,b.sum+c.prel);
a.prer=max(c.prer,c.sum+b.prer);
a.res=max(max(b.res,c.res),b.prer+c.prel);
return a.res;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&d[i]);
build(rt,,n);
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&opt,&x,&y);
if(!opt) change(rt,x,y);
else printf("%lld\n",get(rt,x,y,t));
}
return ;
}
维护一个长度为 nn 的序列 AA,进行 mm 次询问或操作:
0 x y:将 A_xAx 单调修改为 yy1 x y:求出 \max\{\sum_{k=i}^j A_k\}(x\le i\le j\le y)max{∑k=ijAk}(x≤i≤j≤y)。
数据范围:N,M\le 5\times 10^4N,M≤5×104,|A_i|\le 10^4∣Ai∣≤104
Can you answer these queries III(线段树)的更多相关文章
- 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 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- SPOJ 1557. Can you answer these queries II 线段树
Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...
- 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 ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
- HDU 4027 Can you answer these queries?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
随机推荐
- hive查询中文乱码问题
问题1. hue中中文字符乱码问题,重现步骤如下 create external table test_1_txt (id int, name varchar(100)) location '/tm ...
- C++Review7_STL、容器、迭代器
我之前的博文中有专门的5篇整理并介绍了STL的概念: STL1——整体介绍:https://www.cnblogs.com/grooovvve/p/10467794.html STL2——泛型编程(模 ...
- Python程序执行顺序
#示例代码基于py3.6 一直对Python程序的执行顺序有些疑惑,例如python程序是顺序执行的,那怎么还有main函数的出现呢? 在查阅了资料后,参见这里后,算是有点明白了: 1.python程 ...
- Visio常规图表
包含的就是一些形状模块 比如框图就包含了“方块”以及“具有凸起效果的块”两个形状模版 打开visio 新建的时候选择常规类别 具有透视效果的框图 下面是基本操作: 这是自动调整大小的框 不能调整大小 ...
- SQLServer数据库之SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句
--查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( , ( ( THEN ( LEN(CONV ...
- Redis安装(单机及各类集群,阿里云)
Redis安装(单机及各类集群,阿里云) 前言 上周,我朋友突然悄悄咪咪地指着手机上的一篇博客说,这是你的博客吧.我看了一眼,是之前发布的<Rabbit安装(单机及集群,阿里云>.我朋友很 ...
- CodeForces - 1228D
乍一看,嗯,图论题,不错: 结果,这尼玛是模拟???? 传送链接:https://codeforces.com/contest/1228/problem/D 看了大佬的代码瞬间就明白了许多!!! #i ...
- 看完知乎上500条答案,我为大家整理了这21个B站学习类UP主
原文之前发在我的知乎,转载请注明出处. 虽然,今天算法文章还没更新┏(゜ロ゜;)┛,但还是溜过来跑个题~ 之前看到了博客上有小伙伴在分享自己的B站资源,才突然意识到自己其实也积攒了很多优秀UP的资 ...
- 「CF670C」Cinema 解题报告
题面 传送门 思路: 离散化.hash 对于这样一个明显的统计排序的题目,当然轻而易举啦~ 但是!看!语言的编号 a数组和 b数组的值最大在\(10^9\)的级别,所以开个数组来存---That's ...
- maven中scope标签各个值的意义
在使用maven配置时,有时候会见到scope这个标签,但是总是记不住他们所对应的含义,现在整理一下,以后忘记了再来查看. 版权声明:本文为CSDN博主「MrZhangBaby」的原创文章,遵循 CC ...