洛谷P4513 小白逛公园
区间最大子段和模板题。。
维护四个数组:prefix, suffix, sum, tree
假设当前访问节点为cur
- prefix[cur]=max(prefix[lson],sum[lson]+preifx[rson])
- suffix[cur]=max(suffix[rson],sum[rson]+suffix[lson])
- sum[cur]=sum[lson]+sum[rson]
- tree[cur]=max(tree[lson], tree[rson], suffix[lson]+suffix[rson])
在query的时候要注意合并,其实和pushup差不多,需要返回一个节点
// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C yql){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
return ans;
}
const int N = 500005;
struct Node{
int prefix, suffix, tree, sum;
Node(int p, int s, int t, int u): prefix(p), suffix(s), tree(t), sum(u){}
};
int n, m, a[N];
int prefix[N<<2], suffix[N<<2], sum[N<<2], tree[N<<2];
void push_up(int treeIndex){
int lson = treeIndex << 1, rson = treeIndex << 1 | 1;
prefix[treeIndex] = max(prefix[lson], sum[lson] + prefix[rson]);
suffix[treeIndex] = max(suffix[rson], sum[rson] + suffix[lson]);
sum[treeIndex] = sum[lson] + sum[rson];
tree[treeIndex] = max(tree[lson], tree[rson], suffix[lson] + prefix[rson]);
}
void buildTree(int treeIndex, int l, int r){
if(l == r){
tree[treeIndex] = prefix[treeIndex] = suffix[treeIndex] = sum[treeIndex] = a[l];
return;
}
int mid = (l + r) >> 1;
buildTree(treeIndex << 1, l, mid);
buildTree(treeIndex << 1 | 1, mid + 1, r);
push_up(treeIndex);
}
void modify(int treeIndex, int l, int r, int k, int e){
if(l == r){
tree[treeIndex] = prefix[treeIndex] = suffix[treeIndex] = sum[treeIndex] = e;
return;
}
int mid = (l + r) >> 1;
if(k <= mid) modify(treeIndex << 1, l, mid, k, e);
else modify(treeIndex << 1 | 1, mid + 1, r, k, e);
push_up(treeIndex);
}
Node query(int treeIndex, int l, int r, int queryL, int queryR){
if(l == queryL && r == queryR){
return Node(prefix[treeIndex], suffix[treeIndex], tree[treeIndex], sum[treeIndex]);
}
int mid = (l + r) >> 1;
if(queryL > mid) return query(treeIndex << 1 | 1, mid + 1, r, queryL, queryR);
else if(queryR <= mid) return query(treeIndex << 1, l, mid, queryL, queryR);
Node lr = query(treeIndex << 1, l, mid, queryL, mid);
Node rr = query(treeIndex << 1 | 1, mid + 1, r, mid + 1, queryR);
Node ret = Node(max(lr.prefix, lr.sum + rr.prefix),
max(rr.suffix, rr.sum + lr.suffix),
max(lr.tree, rr.tree, lr.suffix + rr.prefix),
lr.sum + rr.sum);
return ret;
}
int main(){
n = read(), m = read();
for(int i = 1; i <= n; i ++) a[i] = read();
buildTree(1, 1, n);
while(m --){
int opt = read(), p = read(), q = read();
if(opt == 1){
if(p > q) swap(p, q);
printf("%d\n", query(1, 1, n, p, q).tree);
}
else if(opt == 2){
modify(1, 1, n, p, q);
}
}
return 0;
}
洛谷P4513 小白逛公园的更多相关文章
- 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新、区间查询)
P4513 小白逛公园 题目背景 小新经常陪小白去公园玩,也就是所谓的遛狗啦… 题目描述 在小新家附近有一条“公园路”,路的一边从南到北依次排着nn个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩 ...
- 2018.07.23 洛谷P4513 小白逛公园(线段树)
传送门 线段树常规操作了解一下. 单点修改维护区间最大连续和. 对于一个区间,维护区间从左端点开始的连续最大和,从右端点开始的连续最大和,整个区间最大和,区间和. 代码如下: #include< ...
- 洛谷P4513 小白逛公园 (线段树)
这道题看起来像是线段树和最大子段和的结合,但这里求最大子段和不用dp,充分利用线段树递归的优势来处理.个人理解:线段树相当于把求整个区间的最大子段和的问题不断划分为很多个小问题,容易解决小问题,然后递 ...
- 线段树 || BZOJ1756: Vijos1083 小白逛公园 || P4513 小白逛公园
题面:小白逛公园 题解: 对于线段树的每个节点除了普通线段树该维护的东西以外,额外维护lsum(与左端点相连的最大连续区间和).rsum(同理)和sum……就行了 代码: #include<cs ...
- 【题解】洛谷P3953 [NOIP2017TG] 逛公园(记忆化搜索+SPFA)
题目来源:洛谷P3953 思路 先用SPFA求一遍最短路 在求最短路的同时可以把所有点到终点的最短路求出来 dis数组 注意要反向SPFA 因为从起点开始可能会走到一些奇怪的路上导致时间负责度增加 ...
- P4513 小白逛公园
题目背景 小新经常陪小白去公园玩,也就是所谓的遛狗啦… 题目描述 在小新家附近有一条“公园路”,路的一边从南到北依次排着 nnn 个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了. 一开始,小白 ...
- luogu P4513 小白逛公园 (区间合并)
链接:https://www.luogu.org/problemnew/show/P4513 思路: 很基础的区间合并,开四个数组: num: 区间数字的和 lsum:从左端点起最大连续字段和 rsu ...
- P4513 小白逛公园 动态维护最大子段和
题目链接:https://www.luogu.org/problem/P4513 #include<iostream> #include<cstdio> #include< ...
- 洛谷P3953 [NOIP2017]逛公园
K<=50,感觉可以DP 先建反图求出从n到各个点的最短路,然后在正图上DP 设f[当前点][比最短路多走的距离]=方案数 转移显然是 $f[v][res]=\sum f[u][res+tmp] ...
随机推荐
- 微软是如何让我再次爱上.Net Core和C#的
“为什么你还想用ASP.NET,难道你还活在90年代吗?”这正是我的一位老同事在几年前我们即将开始的项目中我提出考虑使用ASP.NET时所说的话.当时我很大程度上认同他的看法,微软已经开发了伟大的开发 ...
- Python-序列化模块-json-62
序列化模块 Eva_J 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给? 现在我们能想到 ...
- 把玩Alpine linux(二):APK包管理器
导读 Alpine Linux非常精简,开机内存占用也在二三十兆大,没有拆箱即用,就需要我们自己去做一些了解和配置 Alpine Linux的优劣 优势 Alpine Linux的Docker镜像特点 ...
- 这款APP太像微信 腾讯起诉索赔1000万
去年8月,“币应”(inChat)APP上线,号称是一款原创的区块链加密通讯工具,而界面与微信极为相似,图标是白配绿色调,内部界面几乎一模一样,通讯录.朋友圈的界面完全相同.里面的小游戏,也从微信拿来 ...
- Less or Equal CodeForces - 977C (sort+细节)
You are given a sequence of integers of length nn and integer number kk. You should print any intege ...
- Array Division CodeForces - 808D (构造+实现)
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...
- Jenkins部署net core小记
作为一个不熟悉linux命令的neter,在centos下玩Jenkins真的是一种折磨啊,但是痛并快乐着,最后还是把demo部署成功!写这篇文章是为了记录一下这次部署的流程,和心得体会. 网上很多资 ...
- eclipse如何添加web dynamic project
很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新. 比较方便的是在Help → Install-New-Software,点击add按钮,在Loca ...
- StackWalk64
#include <Windows.h> #define PULONG_PTR ULONG** #define PULONG ULONG* #define ULONG_PTR U ...
- MySQL索引的设计、使用和优化
原文:http://bbs.landingbj.com/t-0-243071-1.html MySQL索引概述 所有MySQL列类型可以被索引.对相关列使用索引是提高SELECT操作性能的最佳途径.根 ...