spoj 1557 GSS3 - Can you answer these queries III 线段树
给出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 线段树的更多相关文章
- 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 ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- 数据结构(线段树):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 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- 线段树 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] ...
- 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 ...
- 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 ...
- 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]| ≤ ...
随机推荐
- Oracle 字符集问题
1 简介 ORACLE数据库字符集,即Oracle全球化支持(Globalization Support),或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数据.利用全球化支持,O ...
- iOS进阶:Objective-C runtime(一)
第一次看到runtime时,觉得太高大上,动态获取方法.属性等简直厉害的不要不要的.在经过查找资料+实践后,发现runtime并没有想象中那么复杂,接下来对runtime进行基本的介绍. 要使用运行时 ...
- JqMobi学习
JqMobi+phonegap+html5 开发Android.ios应用
- InterviewProblems
package com.xiaoysec; /** * 下面是面试趣医网技术面的时候出现的一个简单的题目 题目的要求是将一个数组中的奇数和偶数进行分离 以奇数在前一部分为例进行解题 * 算法的主要思想 ...
- BZOJ 3875: [Ahoi2014]骑士游戏
d[i]表示消灭i所需的最小体力值, d[i] = min(S[i], K[i]+Σd[x]), Σd[x]表示普通攻击而产生的其他怪兽. 因为不是DAG, 所以用个队列类似SPFA来更新答案. -- ...
- JQuery使用on绑定动态生成元素时碰到的问题
今天在写界面时,希望对一些由JS代码动态添加的html标签进行事件绑定,使用了jquery 1.6+才提供的on函数.我的JQ版本是1.9.2. 以下这段代码是动态生成的. <div class ...
- 处理date类型对象的方式
在jsp中通过key属性映射到编译后jsp页面的name属性,通过model中的 setxxxx()方法将jsp页面通过wdatepicker插件产生的string类型的数据转换为timestamp类 ...
- Eclipse --Type /com.xx.app/gen already exists but is not a source folde解决方案
两种解决方案: Two actions, first: 1.Right click on the project and go to "Properties" 2.Select & ...
- javaEE简答题整理
1. 什么是JavaEE?其编程思想是什么? JavaEE是一个标准的中间件体系结构,是企业分布式应用开发标准.JavaEE的编程思想是组件—容器. 2. 为什么提出JavaEE体系结构? (1)为满 ...
- ODI KM二次开发手册
ODI KM二次开发手册 分类: ODI(16) 目录(?)[+] 1 引言 1.1 编写目的 本手册面向的读者对象为具备数据集成业务知识及对ODI操作了解的开发人员,作为其完成基于ODI基础上K ...