题意

给出$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. PHPExcel探索之旅---阶段三 绘制图表

    利用PHPExcel插件进行绘制图表 <?php /** * 使用PHPExcel插件在excel文件中利用其中的数据建立折线图 * */ $dir = dirname(__FILE__); r ...

  2. MySQL表结构,表空间,段,区,页,MVCC

    索引组织表(IOT表):为什么引入索引组织表,好处在那里,组织结构特点是什么,如何创建,创建IOT的限制LIMIT. IOT是以索引的方式存储的表,表的记录存储在索引中,索引即是数据,索引的KEY为P ...

  3. PHP var_export

    var_export可以将一个数组转为一个字符串 不同于var_dump,var_export并不会输出数据的类型以及字符大小等,只会简单把数组的key跟value拼接成一个字符串 <?php ...

  4. 一个简单的类似Vue的双向绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. D - 稳住GCD DP

    http://acm.uestc.edu.cn/#/problem/show/923 给定一堆数字,求其所有数字的gcd. 现在要删除最多的数字,使得剩下的数字的gcd和原来的一样. 设dp[i][v ...

  6. SQL2008无法启动,报错"17051"解决方法

    SQL2008无法启动,这是错误日志: C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/Log 2011-06-0 ...

  7. DataBinding初探 数据绑定的用法 ,import 集合类型,绑定的表达式,访问集合类型2

    数据绑定的用法 import语法   <data> <import type="android.view.view"/> </data>   如 ...

  8. ajax post方式表单提交的注意事项。

    当我们创建一个异步对象XMLHttpRequest同时post方式向后台传输数据的时候. 我们要设置异步对象的xhr.setRequestHeader成员的值为 XMLHttpRequest.setR ...

  9. mysql用户常见操作

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  10. 详解Unity 4.6新UI的布局

    本文所讲的是Unity 4.6中新加入的uGUI,官方称Unity UI,而不是过去的OnGUI式的旧UI(官方称Legacy GUI). 我曾经在8月份对照4.6 Beta的文档写过一篇笔记学习Un ...