线段树操作。

维护一个区间最大连续子段和,左最大连续子段和,右最大连续子段和即可。

最后不知道怎么搞,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的更多相关文章

  1. 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 ...

  2. 数据结构(线段树):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 ...

  3. 线段树 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] ...

  4. 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 ...

  5. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  6. SPOJ GSS3 Can you answer these queries III

    Time Limit: 330MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  7. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...

  8. SP1716 GSS3 - Can you answer these queries III

    题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...

  9. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. C#--线程存储数据的机制

    面试题:线程存储数据的机制 Local variables 局部变量 临时存储 栈 Instance class fields 对象存储 堆 (堆的大小只有硬件的限制) Static local va ...

  2. java 命令行 编译 运行程序

    学习java使用IDE前最好先用用命令行的javac.java来跑一跑简单的程序,这样能够熟悉一下包管理对.class文件路径的影响. 我们先写一段简单的代码: package com.csdn.lk ...

  3. nyoj--79--导弹拦截(动态规划)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 某国为了防御敌国的导弹袭击,发展中一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任 ...

  4. 函数和指针 C++

    一.用函数指针变量调用函数. 指针变量也可以指向一个函数,一个函数在编译时被分配给一个入口地址.这个函数入口地址就称为函数的指针.可以用一个指针变量指向函数,然后通过该指针变量调用此函数. 定义指向函 ...

  5. 线段树(1)——点修改&建树

    #include<cstdio> #include<algorithm> using namespace std; #define MAX 10000 #define INF ...

  6. linux编译安装ccache3.2.4

    1.下载ccache3.2.4安装包 #cd /opt #wget http://samba.org/ftp/ccache/ccache-3.2.4.tar.gz 2.解压 #.tar.gz 3.创建 ...

  7. WebDav协议基于HTTP 1

    首先第一篇提供配置WebDav的方式 网上找了两篇比较好的配置方式分别适用于Win7 Win2003,而且都经过测试配置可以正常使用 原文中保留了引用地址,这个纯属为了要尊重别人的劳动成果 在第二篇中 ...

  8. BZOJ2134: 单选错位(期望乱搞)

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1101  Solved: 851[Submit][Status][Discuss] Descripti ...

  9. css网页布局方式的理解

    一,标准流(默认状态,元素盒按照文档中出现的顺序排列) 块级元素--垂直排版 display:block; 单独一行,可以设置宽高,宽度默认和父元素宽度一致 一般结构性标记都为块级元素,如div,h, ...

  10. python os 模块常用操作

    python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...