「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} ...
随机推荐
- The Preliminary Contest for ICPC Asia Nanjing 2019 - D Robots(概率dp+拓扑排序)
这题概率dp + 拓扑排序可以写 改天补解释 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; ve ...
- pywinauto教程
转:pywinauto教程https://blog.csdn.net/weixin_40161673/article/details/83246861 ** 一.环境安装**1.命令行安装方法pip ...
- netty同时实现http与socket
(1)启动类 package test; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.EventLoopGro ...
- Educational Codeforces Round 74 (Rated for Div. 2)E(状压DP,降低一个m复杂度做法含有集合思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100005];int pos[ ...
- 洛谷 P3371 【模板】单源最短路径(弱化版) && dijkstra模板
嗯... 题目链接:https://www.luogu.org/problem/P3371 没什么好说的,这是一个最短路的模板,这里用的dijkstra做的... 注意: 1.dijkstra和邻接表 ...
- Could not find a version that satisfies the requirement win32api (from versions: ) No matching distribution found for win32api
pip install win32api pip install pywin32 都会提示错误,如下: Could not find a version that satisfies the requ ...
- linux动态监控dstat&&glances&&psutil&&bottle
安装dstat yum install dstat 安装glances yum install python-devel pip install glances 如果我们安装了 Bottle 这个 w ...
- Pentaho6.1中D3可视化库的集成及数据联动的实现
1.软件环境 操作系统版本:Win 10 64位 可视化图形库:D3 Pentaho版本: biserver-ce-6.1.0.1-196 2.对D3的简单介绍 D3允许你将任意的数据绑定到文档对象模 ...
- python 的参数总结
一.形参和实参 函数参数的作用是传递数据给函数使用 在使用的过程中,参数有两种形式:形式参数和实际参数 形参: 定义函数的参数 实参: 调用函数时的参数 根据实际参数类型不同,将实际参数传递给形参的方 ...
- IntelliJ IDEA常用快捷键大全
如果想要非常高效的使用IDEA这款工具,应该掌握图中已被标记的快捷键. 另: 代码实时模板生成:psvm/sout/ifn等 按Tab键快速生成模板. 转载请保留或注明出处:http://www.cn ...