题目链接

给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间。

开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值。 合并起来好麻烦......

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int maxn = 5e4+;
int sum[maxn<<], suf_max[maxn<<], pre_max[maxn<<], maxx[maxn<<];
void pushUp(int rt) {
sum[rt] = sum[rt<<] + sum[rt<<|];
suf_max[rt] = max(suf_max[rt<<|], suf_max[rt<<]+sum[rt<<|]);
pre_max[rt] = max(pre_max[rt<<], pre_max[rt<<|]+sum[rt<<]);
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
maxx[rt] = max(maxx[rt], suf_max[rt<<]+pre_max[rt<<|]);
}
void build(int l, int r, int rt) {
if(l == r) {
scanf("%d", &maxx[rt]);
sum[rt] = pre_max[rt] = suf_max[rt] = maxx[rt];
return ;
}
int m = l+r>>;
build(lson);
build(rson);
pushUp(rt);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
sum[rt] = pre_max[rt] = suf_max[rt] = maxx[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
int query_sum(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return sum[rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret += query_sum(L, R, lson);
if(R>m)
ret += query_sum(L, R, rson);
return ret;
}
int query_suf(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return suf_max[rt];
}
int m = l+r>>;
if(R<=m)
return query_suf(L, R, lson);
if(L>m)
return query_suf(L, R, rson);
return max(query_suf(L, m, lson)+query_sum(m+, R, rson), query_suf(m+, R, rson));
}
int query_pre(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return pre_max[rt];
}
int m = l+r>>;
if(R<=m)
return query_pre(L, R, lson);
if(L>m)
return query_pre(L, R, rson);
return max(query_pre(L, m, lson), query_pre(m+, R, rson)+query_sum(L, m, lson));
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>;
if(R<=m)
return query(L, R, lson);
if(L>m)
return query(L, R, rson);
return max(max(query(L, m, lson), query(m+, R, rson)), query_pre(m+, R, rson)+query_suf(L, m, lson));
}
int main()
{
int n, q, x, y, sign;
scanf("%d", &n);
build(, n, );
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &sign, &x, &y);
if(sign)
printf("%d\n", query(x, y, , n, ));
else
update(x, y, , n, );
}
return ;
}

spoj 1557 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 ——线段树

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

  3. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

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

  5. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

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

  7. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  8. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  9. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

随机推荐

  1. Android入门——UI(9)

    SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...

  2. RandomAccessFile浅析

    RandomAccessFile类中的write方法有以下的注意事项: 首先write方法每次都写入一个字节 api中write方法如下 public void write(int b) throws ...

  3. sql 设计反模式

    ---恢复内容开始--- 1.乱穿马路 ---- > 目标 : 存储多值属性. 1) 错误方法: 使用格式化的逗号分割列表. 1-> 不适合查询,定位数据,无法运用聚合函数进行分组,不利于 ...

  4. Servlet学习第一天--Servlet开发映射URL配置

    基础不扎实,从头学,认真记录笔记. 感谢@孤傲苍狼:http://www.cnblogs.com/xdp-gacl/p/3760336.html -为什么要配置? 由于客户端是通过URL访问web服务 ...

  5. SQL Server 权限的分类

    SQL Server 的权限可以分三类 第一类 server 层面上的: select * from sys.fn_builtin_permissions(default) where class_d ...

  6. Oracle EBS-SQL (GL-1):从总帐追溯到接收

    SELECT je_header_id, je_line_num, trx_class_name, trx_type_name, trx_number_displayed, trx_date,comm ...

  7. 使用MIDAS访问远程Access数据库

    使用MIDAS访问远程Access数据库         Allen Tao(http://blog.csdn.net/allentao/) 2005-5-3 本文源码下载 访问远程数据库常用的办法是 ...

  8. shell 学习笔记1501-1800

    .巧用bash的{}扩展备份目录: cp file.txt{,.bak} .利用at执行一次性命令: echo "ls -l" | at midnight #Execute a c ...

  9. Redis数据结构及相应的命令

    Redis可以存储键(key)与5种不同类型值(value)之间的映射,5中不同类型的值分别为字符串(string),列表(list),散列(hash),集合(set)和有序集合(sorted set ...

  10. 如何在VMware虚拟机上安装Linux操作系统(Ubuntu)

    作为初学者想变为计算机大牛非一朝一夕,但掌握基本的计算机操作和常识却也不是多么难的事情.所以作为一名工科男,为了把握住接近女神的机会,也为了避免当白痴,学会装系统吧!of course为避免把自己的电 ...