「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} ...
随机推荐
- Python NumPy中数组array.min(0)返回数组
如果没有参数min()返回一个标量,如果有参数0表示沿着列,1表示沿着行.
- python 环境迁移之requirements.txt (window环境)
一.背景 1.有时候你本地用python编写代码,然后本地也安装了很多的库,那么你要迁移到服务器上,那么该怎么快速弄环境库呢? #第二,三步就可以解决. 2.有时候你本地用pyth ...
- 使用Vue时localhost:8080中localhost换成ip地址后无法显示页面的问题
解决办法是:在package.json中 然后重新启动服务器 npm run dev 就正常显示了.
- Catalyst3560密码破解
1.关机:2.将设备MODE键按住,然后开机,进入switch:switch: flash_initInitializing Flash...flashfs[0]: 7 files, 2 direct ...
- SpringMVC中在Controller类的每个方法执行前调用某个方法的实现
在使用SpringMVC做项目的时候,如果想在@Controller类中每个@RequestMapping方法执行前都调用某个方法,要怎么实现呢?答案是使用Spring的@ModelAttribute ...
- springboot例子
@Mapperpublic interface FinancingMapper { @Insert("<script>" + "insert into fin ...
- IIS 配置迁移
使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- pagehelper 分页不生效,总页数总是1解决方案
问题: 后台查询后的数据只有1页,已经设置了PageHelper也没用 PageHelper.startPage(pageNum,pageSize); ModelAndView mv=new Mode ...
- Pandas 性能优化 学习笔记
摘要 本文介绍了使用 Pandas 进行数据挖掘时常用的加速技巧. 实验环境 import numpy as np import pandas as pd print(np.__version__) ...