[SPOJ1716] GSS3 - Can you answer these queries III
线段树操作。
维护一个区间最大连续子段和,左最大连续子段和,右最大连续子段和即可。
最后不知道怎么搞,query的时候返回了个结构体。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=50005;
int n,q,a[N],opt,x,y;
struct Segtree{int l,r,lmx,rmx,mx,sum;}t[N<<3];
void pushup(int cur){
t[cur].sum=t[cur<<1].sum+t[cur<<1|1].sum;
t[cur].lmx=max(t[cur<<1].sum+t[cur<<1|1].lmx,t[cur<<1].lmx);
t[cur].rmx=max(t[cur<<1|1].sum+t[cur<<1].rmx,t[cur<<1|1].rmx);
t[cur].mx=max(t[cur<<1].mx,max(t[cur<<1|1].mx,t[cur<<1].rmx+t[cur<<1|1].lmx));
}
void build(int cur,int l,int r){
t[cur].l=l,t[cur].r=r;
if(l==r) {t[cur].sum=t[cur].rmx=t[cur].lmx=t[cur].mx=a[l];return;}
int mid=l+r>>1;
build(cur<<1,l,mid);
build(cur<<1|1,mid+1,r);
pushup(cur);
}
void update(int cur,int c,int now) {
if(t[cur].l==t[cur].r) {t[cur].sum=t[cur].lmx=t[cur].rmx=t[cur].mx=c;return;}
int mid=t[cur].l+t[cur].r>>1;
if(now<=mid) update(cur<<1,c,now);
else update(cur<<1|1,c,now);
pushup(cur);
}
Segtree query(int l,int r,int cur) {
if(l<=t[cur].l&&t[cur].r<=r) return t[cur];
int mid=t[cur].l+t[cur].r>>1;
if(l>mid) return query(l,r,cur<<1|1);
if(r<=mid) return query(l,r,cur<<1);
Segtree x=query(l,r,cur<<1),y=query(l,r,cur<<1|1),ans;
ans.mx=max(x.mx,max(y.mx,x.rmx+y.lmx));
ans.lmx=max(x.lmx,x.sum+y.lmx);
ans.rmx=max(x.rmx+y.sum,y.rmx);
return ans;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&q);
build(1,1,n);
while(q--) {
scanf("%d%d%d",&opt,&x,&y);
if(opt) printf("%d\n",query(x,y,1).mx);
else update(1,y,x);
}
return 0;
}
[SPOJ1716] GSS3 - 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
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] ...
- 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 ...
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
随机推荐
- Rails 异常处理 && 性能
Rails 异常处理 的多种处理方法 1. routes match '*path', via: :all, to: 'controller#action' 2. application.rb 的 ...
- 使用Python生成源文件的两种方法
利用Python的字符串处理模块,开发者能够编写脚本用来生成那些格式同样的C.C++.JAVA源程序.头文件和測试文件,从而避免大量的反复工作. 本文概述两种利用Python string类生成jav ...
- 用hadoop实现SimRank++算法(1)----权值转移矩阵的计算
本文主要针对广告检索领域的查询重写应用,依据查询-广告点击二部图,在MapReduce框架上实现SimRank++算法.关于SimRank++算法的背景和原理请參看前一篇文章<基于MapRedu ...
- chrome 插件开发2
登录 | 注册 基础文档 综述 调试 Manifest 文件 代码例子 模式匹配 分类索引 改变浏览器外观 Browser Actions 右键菜单 桌面通知 Omnibox 选项页 覆写特定页 ...
- luogu1965 转圈游戏
题目大意 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,--,依此 ...
- 【NOIP 2009】 Hankson的趣味题
[题目链接] https://www.luogu.org/problemnew/show/P1072 [算法] x是b1的约数 筛出b1的约数,判断是否符合条件即可 [代码] #include< ...
- [.Net] 导出Excel中身份证等数字串的解决方式
public static void DataTableToExcel(System.Data.DataTable dtData, String FileName) { GridView dgExpo ...
- centos 80端口占用
netstat -lnp|grep 80 kill -9 1777 #杀掉编号为1777的进程(请根据实际情况输入)service httpd start #启动apache
- BZOJ 2729 高精度+组合数学
思路: 考虑 把男生排成一排 女生和老师往里插 分成两种情况. 1. 女生中间夹着老师 2. 女生中间没有夹着老师 求一下组合* 阶乘就好了 先放Python代码 简洁易懂 def fact(n): ...
- Unity3d 销毁
using UnityEngine; using System.Collections; public class destroy : MonoBehaviour { void Start () { ...