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 ...
随机推荐
- springBoot从入门到源码分析
先分享一个springBoot搭建学习项目,和springboot多数据源项目的传送门:https://github.com/1057234721/springBoot 1. SpringBoot快速 ...
- 【Linux】Mac好用虚拟机 Parallels Desktop、FinalShell-多终端连接工具(支持Windows,macOS,Linux)
一.Mac好用虚拟机 Parallels Desktop 1.下载安装: 2.新建虚拟机: 3.配置管理: 二.FinalShell-多终端连接工具(支持Windows,macOS,Linux) 1. ...
- C++Review6_优先队列priority_queue
普通队列是一个先进先出的数据结构,元素在队尾添加,在队头删除. 优先队列的出队逻辑相比于普通队列发生了改变,具有最高优先级的元素先出队. 在C++中只要包含了#include<queue> ...
- OPEN GL
https://blog.csdn.net/cdut100/article/details/45753227 https://www.jianshu.com/p/d22cf555de47 https: ...
- 解决Jsp与Java后台之间url传值中文乱码问题
JSP页面中,由JavaScript拼接url时,对于中文名称变量应该加上encodeURIComponent方法,对中文进行十六进制编码. 例如: url = /com/xxx/B.jsp?chin ...
- 从零开始のcocos2dx生活(三)Scheduler
文章目录 取模 Timer() 变量 设置定时器Timer() 一些成员函数 Scheduler() 变量 初始化 哈希表 构造函数schedule() 开启定时器Update() 析构函数~Upda ...
- 洛谷$P4211\ [LNOI2014]\ LCA$ 树链剖分+线段树
正解:树剖+线段树 解题报告: 传送门$QwQ$ 看到$dep[lca]$啥的就想到之前托腮腮$CSP$模拟$D1T3$的那个套路,,, 然后试下这个想法,于是$dep[lca(x,y)]=\sum_ ...
- $POJ$3252 $Round\ Numbers$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...
- linux MySQL 5.7.20安装教程
安装MySQL 5.7.20shell> cd /usr/localshell> groupadd mysqlshell> useradd -g mysql mysqlshell&g ...
- Python 植物大战僵尸代码实现: 图片加载和显示切换
游戏介绍以前很火的植物大战僵尸游戏, 本想在网上找个python版本游戏学习下,无奈没有发现比较完整的,那就自己来写一个把.图片资源是从github上下载的,因为图片资源有限,只能实现几种植物和僵尸. ...