SPOJ1043 GSS1(线段树)
题意
给出$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(线段树)的更多相关文章
- SPOJ - GSS1 —— 线段树 (结点信息合并)
题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...
- GSS1 - Can you answer these queries I(线段树)
前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...
- 线段树【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 ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- SP1043 GSS1 - Can you answer these queries I 线段树
问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树
传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
随机推荐
- windows下安装python包
1.windows下成功安装好python后,在安装目录的Scripts目录下有easy_install和pip工具 2.如果没有安装pip,进入命令行,切换到python的安装目录下的Scripts ...
- DHCP原理和配置
在大型网络中,会有大量的主机和设备需要获取ip地址和网络参数,为了解决手动配置的工作量大.ip冲突问题,因此需要使用DHCP(dynamic host configuration protocol). ...
- Java实现快排+小坑+partition的两种思路
在做一道剑指Offer的题的时候,有道题涉及到快排的思路,一开始就很快根据以前的思路写出了代码,但似乎有些细节不太对劲,自己拿数据试了下果然.然后折腾了下并记录下一些小坑,还有总结下划分方法parti ...
- D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...
- 【好书推荐】《剑指Offer》之软技能
俗话说,对于程序员来讲,每年都应该出去面试一下,看看自己的技术能力在外面处于什么水平.程序员在一个公司一个环境一个业务干得太久,很容易丧失学习的动力,获得的仅仅是从新手到熟手.当然,我相信在各行各业均 ...
- kill 与 kill -9(面试中问道的知识点)
转载自:http://www.2cto.com/os/201305/215267.html 需要特别说明的是,SIGKILL和SIGSTOP这两个信号既不能被应用程序捕获,也不能被操作系统阻塞或忽略. ...
- PHP笔试题和面试题
1.不用新变量直接交换现有两个变量的值 (1)list($a,$b)=array($b,$a); (2)a=a+b,b=a-b,a=a-b 2.PHP数字金额转大小格式,同时说明思路 function ...
- javascript 获取dom书的下一个节点。
利用javascript 写一个在页面点击加减按钮实现数字的累加.. 简略的html大概如此.看得懂就好不要在意这些细节啊 <input type="button" valu ...
- oracle中scott用户下四个基本表SQL语句练习
--选择部门中30的雇员SELECT * from emp where DEPTNO=30;--列出所有办事员的姓名.部门.编号--采用内连接方式,也就是等值链接,也是最常用的链接SELECT ena ...
- Spring cloud Eureka 服务治理(搭建服务注册中心)
服务之类是微服务架构中最为核心的基础模块,它主要用来实现各个微服务实例的自动化注册和发现. 1. 服务注册 在服务治理框架中,通常会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机. ...