题意

给出$n$个数,每次询问区间$(l, r)$内最大字段和

Sol

在合并子树的时候,答案仅有四种情况

打四个标记维护即可

查询同理,用类似update的方式合并

注意查询的时候不能按照以前的方式写,因为不知道变量的下界,最稳妥的办法就是判三种情况

/*

*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define rg register
#define sc(x) scanf("%d", &x);
#define pt(x) printf("%d ", x);
#define db(x) double x
#define rep(x) for(int i = 1; i <= x; i++)
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
char buf[( << )], *p1 = buf, *p2 = buf;
char obuf[<<], *O = obuf;
#define OS *O++ = '\n';
using namespace std;
using namespace __gnu_pbds;
const int MAXN = , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
void print(int x) {
if(x > ) print(x / );
*O++ = x % + '';
}
#define ls k << 1
#define rs k << 1 | 1
int N, M;
int a[MAXN];
struct Node {
int l, r, lmx, rmx, mx, sum;
}T[MAXN << ];
void update(int k) {
T[k].sum = T[ls].sum + T[rs].sum;
T[k].mx = max(T[ls].mx, T[rs].mx);
T[k].mx = max(T[k].mx, T[ls].rmx + T[rs].lmx);
T[k].rmx = max(T[rs].rmx, T[rs].sum + T[ls].rmx);
T[k].lmx = max(T[ls].lmx, T[ls].sum + T[rs].lmx);
}
void Build(int k, int ll, int rr) {
T[k] = (Node) {ll, rr, , , };
if(ll == rr) {
T[k].lmx = T[k].rmx = T[k].mx = T[k].sum = a[ll];
return ;
}
int mid = ll + rr >> ;
Build(ls, ll, mid);
Build(rs, mid + , rr);
update(k);
}
Node merge(Node a, Node b) {
Node now;
now.sum = a.sum + b.sum;
now.mx = max(a.mx, b.mx);
now.mx = max(now.mx, a.rmx + b.lmx);
now.rmx = max(b.rmx, b.sum + a.rmx);
now.lmx = max(a.lmx, a.sum + b.lmx);
// printf("%d %d %d %d\n", now.mx, now.lmx, now.rmx, now.sum);
return now;
}
Node Query(int k, int ll, int rr) {
Node ans = (Node) {, , , , };
if(ll <= T[k].l && T[k].r <= rr) return T[k];
int mid = T[k].l + T[k].r >> ;
/*if(ll <= mid) ans = Query(ls, ll, rr);
if(rr > mid) ans = merge(ans, Query(rs, ll, rr)); WA!*/
if(ll > mid) return Query(rs, ll, rr);
else if(rr <= mid) return Query(ls, ll, rr);
else return merge(Query(ls, ll, rr), Query(rs, ll, rr));
return ans;
}
main() {
//freopen("a.in", "r", stdin);
N = read();
for(int i = ; i <= N; i++) a[i] = read();
Build(, , N);
int M = read();
while(M--) {
int x = read(), y = read();
printf("%d\n", Query(, x, y).mx);
}
//fwrite(obuf, O-obuf, 1 , stdout);
return ;
}
/*
5
-10 12 1 -45 134
5
1 5
2 3
4 5
1 4
3 5
*/

SPOJ1043 GSS1(线段树)的更多相关文章

  1. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  2. GSS1 - Can you answer these queries I(线段树)

    前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...

  3. 线段树【SP1043】GSS1 - Can you answer these queries I

    Description 给出了序列\(A_1,A_2,-,A_n\). \(a_i \leq 15007,1 \leq n \leq 50000\).查询定义如下: 查询\((x,y)=max{a_i ...

  4. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  5. SP1043 GSS1 - Can you answer these queries I 线段树

    问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...

  6. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

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

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

  8. SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树

    传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...

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

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

随机推荐

  1. jap的教程

    第一个资料:   https://wenku.baidu.com/view/5ca6ce6a1eb91a37f1115cee.html 第二个资料 :http://www.yiibai.com/jpa ...

  2. HTML常用标签参考学习

    1.跑马灯标签 功能<marquee>...</marquee> 普通卷动<marquee behavior=slide>...</marquee> 滑 ...

  3. ElasticSearch java API-使用More like this实现基于内容的推荐

    ElasticSearch java API-使用More like this实现基于内容的推荐 基于内容的推荐通常是给定一篇文档信息,然后给用户推荐与该文档相识的文档.Lucene的api中有实现查 ...

  4. git&github学习【尚硅谷】

    2019/01/17 18:22 集中式版本工具会有单点故障的问题 分布式版本工具能够避免单点故障 git在本地的结构: 团队内部协作: pull  push  add  commit  等等 关于g ...

  5. 使用AOP监控用户操作并插入数据库

    引入依赖 <!--spring切面aop依赖--> <dependency> <groupId>org.springframework.boot</group ...

  6. 项目开发bug记录

    项目开发中遇到了一个问题,类中出现未知属性 ‘ $jacocoData ’,准确的来说,实际上在集成测试阶段,系统自动运行测试用例时,抛出来的异常提示信息,但是在开发阶段是不存在的.这个问题是以前没有 ...

  7. ElasticSearch 处理自然语言流程

    ES处理人类语言 ElasticSearch提供了很多的语言分析器,这些分析器承担以下四种角色: 文本拆分为单词 The quick brown foxes → [ The, quick, brown ...

  8. 七、SSR(服务端渲染)

    使用框架的问题 下载Vue.js 执行Vue.js 生成HTML页面(首屏显示,依赖于vue.js的加载) 以前没有前端框架时,用jsp/php在服务器端进行数据的填充,发送给客户端就是已经填充好的数 ...

  9. Mysql数据库操作语句总结(二)

    Mysql字符串字段判断是否包含字符串的3中方法 方法一: select * from user where email like "%b@email.com%";// 这个理解起 ...

  10. Unity3D Shader性能排行

    整体上,性能由高到低: Unlit,仅为纹理,光线不产生效果 VertexLit Diffuse 漫反射 Normal Mapped 法线贴图 Specular 高光 Normal Mapped Sp ...