CH4301 Can you answer on these queries III 题解
给定长度为N的数列A,以及M条指令 (N≤500000, M≤100000),每条指令可能是以下两种之一:
“2 x y”,把 A[x] 改成 y。
“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 max(x≤l≤r≤y) { \(\sum_{i=l}^r\) A[i] }。
对于每个询问,输出一个整数表示答案。
一道模板题,线段树的区间最大子段和。
代码
#include<bits/stdc++.h>
using namespace std;
const int SIZE=500010;
struct node{
long long data;
long long sum;
long long l,r;
long long lmax,rmax;
}t[SIZE*4];
long long n,m,u,v,k,sum,a[SIZE];
void build(long long p,long long l,long long r){
t[p].l=l,t[p].r=r;
if(l==r){
t[p].sum=t[p].data=t[p].lmax=t[p].rmax=a[l];
return;
}
long long mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
t[p].lmax=max(t[p*2].lmax,t[p*2].sum+t[p*2+1].lmax);
t[p].rmax=max(t[p*2+1].rmax,t[p*2+1].sum+t[p*2].rmax);
t[p].data=max(max(t[p*2].data,t[p*2+1].data),t[p*2].rmax+t[p*2+1].lmax);
}
void change(long long p,long long x,long long v){
if(t[p].l==t[p].r){
t[p].data=t[p].sum=t[p].lmax=t[p].rmax=v;
return;
}
long long mid=(t[p].l+t[p].r)/2;
if(x<=mid) change(p*2,x,v);
else change(p*2+1,x,v);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
t[p].lmax=max(t[p*2].lmax,t[p*2].sum+t[p*2+1].lmax);
t[p].rmax=max(t[p*2+1].rmax,t[p*2+1].sum+t[p*2].rmax);
t[p].data=max(max(t[p*2].data,t[p*2+1].data),t[p*2].rmax+t[p*2+1].lmax);
}
node ask(long long p,long long l,long long r){
if(l<=t[p].l&&r>=t[p].r) return t[p];
long long mid=(t[p].l+t[p].r)/2;
if(mid>=r) return ask(p*2,l,r);
if(mid<l) return ask(p*2+1,l,r);
else{
node ans,a1,b;
a1=ask(p*2,l,r);
b=ask(p*2+1,l,r);
ans.sum=a1.sum+b.sum;
ans.data=max(max(a1.data,a1.rmax+b.lmax),b.data);
ans.lmax=max(a1.lmax,a1.sum+b.lmax);
ans.rmax=max(b.rmax,b.sum+a1.rmax);
return ans;
}
}
int main(){
scanf("%lld",&n);
scanf("%lld",&m);
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
}
build(1,1,n);
while(m--){
scanf("%lld %lld %lld",&k,&u,&v);
if(k==1){
if(u>v) swap(u,v);
printf("%lld\n",ask(1,u,v).data);
}
else{
change(1,u,v);
}
}
return 0;
}
CH4301 Can you answer on 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 ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...
- 线段树 SP1716 GSS3 - Can you answer these queries III
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
- 「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- Can you answer these queries III
Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改 ...
- 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 ...
- C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
随机推荐
- Usaco Training [2.1] The Castle 搜索
传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...
- python基础--基于套接字进行文件传输、异常处理、socketserver模块
异常处理: 什么是异常处理: 程序在运行过程中出现了不可预知的错误,并且该错误没有对应的处理机制,那么就会以异常的形式表现出来,造成的影响就是整个程序无法再正常运行 异常的结构: 异常的类型.异常的信 ...
- 【算法】【排序】【插入类】希尔排序 ShellSort
#include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ ]; //设立随机数 sran ...
- 数据结构之堆栈C++版
/* 堆栈本身就是一种线性数据结构,说白了他与容器线性表是一种数据类型,不要认为他多高大上. 实时上他还没有线性表复杂,下面简单的实现一下堆栈. 事实上整个核心操作都是在操作指向堆栈的顶部元素的指针 ...
- 利用反射,更改string对象的value数组以达到改变string值。
package test; import java.lang.reflect.Field; import lombok.Value; public class Test1{ public static ...
- 动态SQL查询
if+where: 用于查询操作,where标签可以智能判断是否添加and.or.where关键词 示例: <select id="findByParam" resultTy ...
- C#之BackgroundWorker从简单入门到深入精通的用法总结
需求分析 经常用到的耗时操作,例如: 1.文件下载和上载(包括点对点应用程序传输文件,从网络下载文件.图像等)2.数据库事务(从数据库读到大量的数据到WinForm界面中的DataGridview里呈 ...
- Yii2 登录报错
当用数据库登录系统报如下错误时 PHP Recoverable Error – yii\base\ErrorException Argument 1 passed to yii\web\User::l ...
- mysql根据逗号将一行数据拆分成多行数据
mysql根据逗号将一行数据拆分成多行数据 原始数据 处理结果展示 DDL CREATE TABLE `company` ( `id` ) DEFAULT NULL, `name` ) DEFAULT ...
- 10G的变态SQL文件,如何快速打开编辑?
工作中,偶尔需要编辑一些大文件,比如 log 文件,后者一些变态的 SQL,此时用平常的编辑器就会显得力不从心,要么直接打不开,要么打开后卡得要死. 本文就给大家推荐几款可以操作大文件的编辑器,准备好 ...