题目链接

给出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. 无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置

    错误描述:无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置.请按名称指示首选的终结点配置部分. 错误原因:该webservce在web.confi ...

  2. Java开发常用下载的网址

    cygwin国内镜像:http://mirrors.sohu.com/cygwin/ 旧版本的ant下载:http://archive.apache.org/dist/ant/ 旧版本的nutch下载 ...

  3. OC中协议的概念以及用法

    OC中协议的概念以及用法,协议也是OC中的一个重点,Foundation框架以及我们后面在写代码都会用到. OC中的协议就是相当于Java中的接口(抽象类),只不过OC中的名字更形象点,因为我们在学习 ...

  4. Csharp递归和循环实现折半查找

    static bool whilehalf(int n) { ; //低位 ; //高位 ; while(low <= hight) { if(n> arr[c]) { low = c + ...

  5. css background-position (图片裁取)

    语法:background-position : length || length background-position : position || position 取值:length  : 百分 ...

  6. SUBSTRING_INDEX ——网上的解释

    SUBSTRING_INDEX(str,delim,count)    Returns the substring from string str before count occurrences o ...

  7. Android 通过HTTP GET请求互联网数据

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...

  8. ubuntu rc.local 无效 解决方案(转)

    为了让mysql开机启动,我将mysql命令添加到/etc/rc.local中,但怎么也运行不了.一开始认为只是/etc/rc.local的权限问题,但通过以下命令修改后,还是不起作用. sudo c ...

  9. 如何使用win7自带的备份还原以及创建系统镜像------傻瓜式教程

    对于经常鼓捣电脑的童鞋来说,装系统是一件极其平常的事情,不过系统装多了之后,我们会感到比较烦躁,因为每一次装系统意味着驱动的重新安装,程序的重新安装,每次这么鼓捣几次,半天时间就花在这上面了,效率是在 ...

  10. 在VC6.0下如何调用Delphi5.0开发的进程内COM

    因为本人的语言水平很差,考大学时150的总分,我考了个60分.外语也是,初中及格过一次,会考及格过一次.其它的时间好像从没有及格过.所以我不写文章,因我一百字的文章给我写,至少要出八九个错别字.哈哈… ...