「SP1043」GSS1 - Can you answer these queries I
传送门
Luogu
解题思路
这题就是 GSS3 的一个退化版,不带修改操作的区间最大子段和,没什么好讲的。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 50000 + 10;
int n, m, a[_];
struct node{ int sum, L, R, mx; }t[_ << 2];
inline int lc(int p) { return p << 1; }
inline int rc(int p) { return p << 1 | 1; }
inline void pushup(int p) {
t[p].sum = t[lc(p)].sum + t[rc(p)].sum;
t[p].L = max(t[lc(p)].L, t[lc(p)].sum + t[rc(p)].L);
t[p].R = max(t[rc(p)].R, t[rc(p)].sum + t[lc(p)].R);
t[p].mx = max(t[lc(p)].R + t[rc(p)].L, max(t[lc(p)].mx, t[rc(p)].mx));
}
inline void build(int p = 1, int l = 1, int r = n) {
if (l == r) { t[p] = (node) { a[l], a[l], a[l], a[l] }; return; }
int mid = (l + r) >> 1;
build(lc(p), l, mid), build(rc(p), mid + 1, r), pushup(p);
}
inline node query(int ql, int qr, int p = 1, int l = 1, int r = n) {
if (ql <= l && r <= qr) return t[p];
int mid = (l + r) >> 1;
if (ql > mid) return query(ql, qr, rc(p), mid + 1, r);
if (qr <= mid) return query(ql, qr, lc(p), l, mid);
node ls = query(ql, mid, lc(p), l, mid);
node rs = query(mid + 1, qr, rc(p), mid + 1, r);
node res;
res.sum = ls.sum + rs.sum;
res.L = max(ls.L, ls.sum + rs.L);
res.R = max(rs.R, rs.sum + ls.R);
res.mx = max(ls.R + rs.L, max(ls.mx, rs.mx));
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(a[i]);
build();
read(m);
for (int ql, qr; m--; )
read(ql), read(qr), printf("%d\n", query(ql, qr).mx);
return 0;
}
完结撒花 \(qwq\)
「SP1043」GSS1 - Can you answer these queries I的更多相关文章
- 线段树【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 ...
- 题解【SP1043】 GSS1 - Can you answer these queries I
题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...
- 「SP2713」GSS4 - Can you answer these queries IV
传送门 Luogu 解题思路 区间开方以及区间求和. 考虑用线段树来做. 开方操作看似没有任何结合律可言,但这题有另外一个性质: 一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右 ...
- 「SP1716」GSS3 - Can you answer these queries III
传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...
- 线段树 SP1043 GSS1 - Can you answer these queries I
SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 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]| ≤ ...
- [SP1043] GSS1 - Can you answer these queries I
传送门:>Here< 题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改) 解题思路 线段树的一道好题 我们可以考虑,如果一组数据全部都是正数,那么问题等同 ...
- SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))
题目描述 给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...
随机推荐
- Nmap工具用法详解
Nmap Network Mapper 是一款开放源代码的网络探测和安全审核工具 1.介绍
- 【原】python总结
python3浅拷贝和深拷贝:https://www.jianshu.com/p/c7e72fcad407
- thinkphp5.1注解插件
前言: thinkphp5.1中用注解的方式实现: v0.1.0版本 数据验证器 请求过滤.格式化 属性对象注入 dev-master版本 额外支持 自动事务 数据缓存 如果您觉得好用,点个star哈 ...
- Wcf托管在IIS中,HttpContext.Current为空
config中需要配置 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 另需要在服务类上加 ...
- 第七届蓝桥杯javaB组真题解析-四平方和(第八题)
题目 /* 四平方和 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1 ...
- linux目录与路径
1.相对路径和绝对路径 绝对路径:一定是从根目录开始,如:/usr/share/doc 相对路径:如果想从/usr/share/doc/到/usr/share/man下,可以写成 cd ../man, ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:用户管理与权限分配
create user mr identified by mrsoft default tablespace users temporary tablespace temp; create user ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设定文本小写
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- 3_01_MSSQL课程_Ado.Net_连接数据库
1. Ado.Net 是一组微软的c#操作数据库的类库. 2.开发人员将界面上的数据(用户的操作和输入的数据)存储到数据库当中.. 3.数据库访问的驱动.即:应用程序和数据库的桥梁. 4.驱动层. S ...
- gensim加载词向量文件
# -*- coding: utf-8 -*- # author: huihui # date: 2020/1/31 7:58 下午 ''' 根据语料训练词向量,并保存向量文件 ''' import ...