线段树 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] 的最大子段和
依旧是维护最大子段和,还是再敲一遍比较好。
code:
#include<iostream>
#include<cstdio>
#define ls(o) o<<1
#define rs(o) o<<1|1
using namespace std;
const int wx=100017;
inline int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
return sum*f;
}
struct val_tree{
int l,r,lsum,rsum,sum,tot;
#define sum(o) t[o].sum
#define lsum(o) t[o].lsum
#define rsum(o) t[o].rsum
#define tot(o) t[o].tot
}t[wx*4];
int a[wx];
int n,m;
void up(int o){
tot(o)=tot(ls(o))+tot(rs(o));
lsum(o)=max(lsum(ls(o)),tot(ls(o))+lsum(rs(o)));
rsum(o)=max(rsum(rs(o)),tot(rs(o))+rsum(ls(o)));
sum(o)=max(rsum(ls(o))+lsum(rs(o)),max(sum(ls(o)),sum(rs(o))));
}
void build(int o,int l,int r){
t[o].l=l;t[o].r=r;
if(l==r){sum(o)=lsum(o)=rsum(o)=tot(o)=a[l];return ;}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)build(ls(o),l,mid);
if(r>mid)build(rs(o),mid+1,r);
up(o);
}
void update(int o,int l,int r,int k){
if(l<=t[o].l&&t[o].r<=r){
sum(o)=lsum(o)=rsum(o)=tot(o)=k;
return ;
}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)update(ls(o),l,r,k);
if(r>mid)update(rs(o),l,r,k);
up(o);
}
val_tree query(int o,int l,int r){
if(l<=t[o].l&&t[o].r<=r){
return t[o];
}
int mid=t[o].l+t[o].r>>1;
if(r<=mid)return query(ls(o),l,r);
if(l>mid)return query(rs(o),l,r);
val_tree tmp,tmp1,tmp2;
tmp1=query(ls(o),l,r);
tmp2=query(rs(o),l,r);
tmp.tot=tmp1.tot+tmp2.tot;
tmp.lsum=max(tmp1.lsum,tmp1.tot+tmp2.lsum);
tmp.rsum=max(tmp2.rsum,tmp2.tot+tmp1.rsum);
tmp.sum=max(tmp1.rsum+tmp2.lsum,max(tmp1.sum,tmp2.sum));
return tmp;
}
int main(){
n=read();
for(int i=1;i<=n;i++)a[i]=read();
build(1,1,n);
m=read();
for(int i=1,x,y,opt;i<=m;i++){
opt=read();x=read();y=read();
if(opt)printf("%d\n",query(1,x,y).sum);
else update(1,x,x,y);
}
return 0;
}
线段树 SP1716 GSS3 - Can you answer these queries III的更多相关文章
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...
- SP1716 GSS3 - Can you answer these queries III
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- SP1716 GSS3 - Can you answer these queries III(单点修改,区间最大子段和)
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are give ...
- 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
GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- 【SP1716】GSS3 - Can you answer these queries III(动态DP)
题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ...
- 题解 SP1716 【GSS3 - Can you answer these queries III】
\[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...
随机推荐
- 2015.7.17 case when then else end用法Oralcle与SQLserver一致
SELECT CASE airway_point_type_id WHEN 1 THEN 'VOR' WHEN 2 THEN 'VOR/DME' WHEN 3 THEN 'NDB' WHEN 10 T ...
- nginx for windows之负载均衡
1.编辑c:\nginx\conf\nginx.conf 添加以下内容: #user nobody; #启动进程worker_processes 1; #全局错误日志及PID文件err ...
- delphi 蓝牙 TBluetoothLE
delphi 蓝牙 TBluetoothLE.TBluetoothLEManager BLE http://docwiki.embarcadero.com/RADStudio/Seattle/en/U ...
- 斐波那契数列-java实现
1,1,2,3,5,8,13,21...... 以上的数列叫斐波那契数列,今天的面试第一题,输出前50个,这里记录下. 方式一 package com.geenk.demo.my; /** * @au ...
- python中not的用法
python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...
- OpenCV 官方工程报错(1) Couldn't load mixed_sample from loader
openCV/OpenCV-android-sdk/samples/tutorial-2-mixedprocessing 工程 - ::): Trying to get library list - ...
- ES6中的Set与Map数据结构
本文实例讲述了ES6学习笔记之Set和Map数据结构.分享给大家供大家参考,具体如下: 一.Set ES6提供了新的数据结构Set.类似于数组,只不过其成员值都是唯一的,没有重复的值. Set本身是一 ...
- iOS 添加Empty Application模板
在Apple最新的XCode6.x中没有了Empty Application模板,好在XCode可以添加模板,而且可以自定义模板. 首先可以到XCode5.x中复制 Empty Application ...
- 算法Sedgewick第四版-第1章基础-023-MultiwordSearch.java
Multi-word search. Program MultiwordSearch.java reads a sequence of query words q[1], ..., q[k] from ...
- Be a Smart Raftsman SGU475
传送门 题目大意 有m+1个点,0是起点,m是终点,i-1到i有一条边,有一个船由0驶往m,不能返回,它在载重小于等于ci时通过第i条边消耗的时间为di否则为Di,现在有n个人,每个人体重为wi,上船 ...