前言

spoj需要翻墙注册,比较麻烦,大家就在luogu评测吧

题目大意:

$n$ 个数,$q$ 次操作

操作$0 _ x_ y$把$A_x$ 修改为$y$

操作$1 _ l _r$询问区间$[l, r]$ 的最大子段和

## 思路
###**维护一个区间的$sum,lmax,rmax,max$**
###**就是区间和,左端开头的最大字段和,右端开头的最大字段和和区间内的最大字段和**
###**他们都说转移难,我咋感觉是询问难呢**
###**转移的话**
####**①max 就是右边,左边,和交界处的最大值,取个max**
####**②lmax和rmax类似,就是有两种可能,一种是只在一个孩子内,一种是两个孩子内(完全包含一个孩子)**
####**③sum不必说(其实就是为了求lmax和rmax的)**

查询的话,当然不能加起来啦,如果不连续不就GG了

①如果全部在一个孩子内,直接去查询那个孩子就好

②如果横跨两个孩子的话,还要讨论一下交界处

看着很明白,也许是我太菜了不太会递归吧

我居然能忘记维护sum,也是醉了

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bitset>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
const int maxn = 50007;
const int maxm = 250007;
const int inf = 0x3f3f3f3f; int n, m, a[maxn],tot[maxn];
struct node {
int l, r;
int lk, rk, k, sum;
} e[maxm]; int read() {
int x = 0, f = 1; char s = getchar();
for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
for (; s <= '9' && s >= '0'; s = getchar()) x = x * 10 + s - '0';
return x * f;
} int max_2(int a, int b, int c) {
return max(max(a, b), c);
} void pushup(int rt) {
e[rt].k = max_2(e[ls].k, e[rs].k, e[ls].rk + e[rs].lk);
e[rt].lk = max(e[ls].sum + e[rs].lk, e[ls].lk);
e[rt].rk = max(e[ls].rk + e[rs].sum, e[rs].rk);
e[rt].sum= e[ls].sum +e[rs].sum;
} void build(int l, int r, int rt) {
e[rt].l = l, e[rt].r = r, e[rt].sum = tot[r] - tot[l-1];
if (l == r) {
e[rt].k = e[rt].lk = e[rt].rk = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, ls);
build(mid + 1, r, rs);
pushup(rt);
} void update(int L, int k, int rt) {
if (e[rt].l == e[rt].r) {
e[rt].sum=k;
e[rt].k = e[rt].lk = e[rt].rk = k;
return;
}
int mid = (e[rt].l + e[rt].r) >> 1;
if (L <= mid) update(L, k, ls);
else update(L, k, rs);
pushup(rt);
}
void debug() {
printf("debug\n");
printf(" %d\n", e[1].k);
printf(" %d %d\n", e[2].k, e[3].k );
printf(" %d %d %d %d\n", e[4].k, e[5].k, e[6].k, e[7].k );
} node query(int L, int R, int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
return e[rt];
}
int mid = (e[rt].l + e[rt].r) >> 1;
if(L <= mid && R > mid) {
node x=query(L, R, ls),y=query(L, R, rs);
node tmp={}; tmp.k = max_2(x.k, y.k, x.rk + y.lk);
tmp.lk =max(x.sum + y.lk, x.lk);
tmp.rk =max(x.rk + y.sum, y.rk);
return tmp;
}
if (L <= mid) return query(L, R, ls);
if (R > mid) return query(L, R, rs);
} int main() {
//freopen("in.txt","r",stdin);
n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
tot[i]=tot[i-1]+a[i];
}
build(1, n, 1);
m = read();
for (; m--;) {
int tmp = read(), a = read(), b = read();
if (tmp) {
printf("%d\n",max_2(query(a, b, 1).k,query(a,b,1).lk,query(a,b,1).rk) );
} else {
update(a, b, 1);
}
}
//debug();
return 0;
}

最大子段和SP1716GSS3 线段树的更多相关文章

  1. 51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)

    题目链接:子段求和 题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少. 题解:线段树区间求和 | 树状数组区间求和 线段树: #include <cstdio> ...

  2. 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新、区间查询)

    P4513 小白逛公园 题目背景 小新经常陪小白去公园玩,也就是所谓的遛狗啦… 题目描述 在小新家附近有一条“公园路”,路的一边从南到北依次排着nn个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩 ...

  3. codevs3981动态最大子段和(线段树)

    3981 动态最大子段和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 题目还是简单一点好... 有n个数,a ...

  4. codevs 3981 动态最大子段和(线段树)

    题目传送门:codevs 3981 动态最大子段和 题目描述 Description 题目还是简单一点好... 有n个数,a[1]到a[n]. 接下来q次查询,每次动态指定两个数l,r,求a[l]到a ...

  5. BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案

    Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...

  6. BZOJ1135:[POI2009]Lyz(线段树,Hall定理)

    Description 初始时滑冰俱乐部有1到n号的溜冰鞋各k双.已知x号脚的人可以穿x到x+d的溜冰鞋. 有m次操作,每次包含两个数ri,xi代表来了xi个ri号脚的人.xi为负,则代表走了这么多人 ...

  7. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  8. Luogu P2572 [SCOI2010]序列操作 线段树。。

    咕咕了...于是借鉴了小粉兔的做法ORZ... 其实就是维护最大子段和的线段树,但上面又多了一些操作....QWQ 维护8个信息:1/0的个数(sum),左/右边起1/0的最长长度(ls,rs),整段 ...

  9. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

随机推荐

  1. Maven中的pom.xml配置文件详解

    原文:http://blog.csdn.net/u012152619/article/details/51485297 <project xmlns="http://maven.apa ...

  2. LoadRunner-创建场景

    录制完脚本,并调试运行正常后,想要模拟并发进行压力测试,需要创建场景. 1.点击Tools->Create Controller Scenario... 2.选择手工场景,并设置并发用户数.点击 ...

  3. 007-Redi-命令-脚本命令、链接命令、服务器命令、事务、HyperLogLog

    Redis 脚本命令 下表列出了 redis 脚本常用命令: 序号 命令及描述 1 EVAL script numkeys key [key ...] arg [arg ...] 执行 Lua 脚本. ...

  4. inter x86 emulator accelerator(HAXM installer) not compatible with windows

    在SDK manager中遇到如下错误:这将导致AVD后期运行和启动方面的问题. 解决办法: 在如下的网址里面下载haxm-windows_v6_2_0这个文件的压缩包,自己手动安装即可.网站如下:点 ...

  5. XPath轴

    XPath 轴翻译:Linyupark / 2006-03-24 The XML Example DocumentXML举例文档 We will use the following XML docum ...

  6. (转)使用git stash解决git pull时的冲突

    在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...

  7. [LeetCode] 721. Accounts Merge_Medium tag: DFS recursive

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  8. http接口自动化测试框架实现

    一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值 输出:XML文件 eg:http://xxx.com/xxx_product/test/conten ...

  9. 【Java】系统漏洞:关于用户登录后操作的注意事项

    项目背景: SpringMVC + Mybatis  + MySql数据库(javaWeb项目开发) 相关模块:登录,个人详细信息修改,订单详情查询 相关漏洞介绍: 1.登录的验证码:登录的验证码一定 ...

  10. html5<embed>的完整属性

    问题起因:网页中插入Flash,看了代码,然后呢,小小的学习下 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000& ...