CF2043C Sums on Segments
题意概要
一个数组,最多有一个数的绝对值不是 \(1\),求出所有可以得到的区间和。
思路
这里提供一个 数据结构优化查询前缀和最值 的做法。
最多有一个数的绝对值不是 \(1\),那我们可以先忽略掉这个数(记该数下标为 \(x\))。
剩下的数要么是 \(1\),要么是 \(-1\),所以我们扩展一个区间的时候,区间和的变化(或增或减)单位大小是 \(1\)。
因此,我们最后的值一定是至多 \(2\) 个连续整数区间:
- 一个是由 \(0\)(空区间) 扩展而来,并且扩展出的区间不包含 \(x\)。
- 一个是由 \(a_x\) 扩展而来。
首先考虑第一种区间,我们求出不包含 \(x\) 的所有区间和的最大值和最小值即可。
这个很明显是一个区间最值问题,但是这里涉及到区间和,所以我们先把原数组 \(a\) 进行前缀和操作转化为前缀和数组 \(pre\)。
然后,我们把前缀和数组 \(pre\) 存入 线段树 或 ST表 中维护区间前缀和最大值和最小值。
对于 \(x\) 左边的区间,我们从左到右枚举区间的左端点 \(i\),查询 \(i\) 到 \(x\) 的区间前缀和最大值,减掉 \(pre_{i - 1}\),就可以得到以 \(i\) 作为左端点的不包含 \(x\) 的所有区间的最大区间和。最小区间和同理。
对于 \(x\) 右边的区间,我们从左到右枚举区间的左端点 \(i\),查询 \(i\) 到 \(n\) 的区间前缀和最大值,减掉 \(pre_{i - 1}\),就可以得到以 \(i\) 作为左端点的不包含 \(x\) 的所有区间的最大区间和。最小区间和同理。
对于此类区间得到的结果取个并集,就是此类区间最后的结果。
然后是第二种区间,由 \(a_{x}\) 扩展而来。
此时这个区间一定包含 \(x\) ,因此,我们只需要以 \(x\) 为起点,分别向左向右扩展。
向左扩展变化的最小值加上向右扩展变化的最小值,就是此类区间的最小区间和。
向左扩展变化的最大值加上向右扩展变化的最大值,就是此类区间的最大区间和。
最后对两种区间求得的结果再取一个并集即可。
时间复杂度:\(O(n \log n)\)。
AC CODE
// Problem: C. Sums on Segments
// Contest: Codeforces - Educational Codeforces Round 173 (Rated for Div. 2)
// URL: https://codeforces.com/contest/2043/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define int long long
#define inf 2e18
#define ull unsigned long long
#define ls o << 1
#define rs o << 1 | 1
using namespace std;
const int N = 2e5 + 9;
int a[N];
int tmx[N << 2], tmi[N << 2];
int n;
//线段树维护区间最值
void pushup(int o)
{
tmx[o] = max(tmx[ls], tmx[rs]);
tmi[o] = min(tmi[ls], tmi[rs]);
}
void build(int s = 1, int e = n, int o = 1)
{
if(s == e)return tmx[o] = a[s], tmi[o] = a[s], void();
int mid = s + e >> 1;
build(s, mid, ls);
build(mid + 1, e, rs);
pushup(o);
}
int querymx(int l, int r, int s = 1, int e = n, int o = 1)
{
if(l <= s && e <= r) {
return tmx[o];
}
int mid = s + e >> 1;
int res = -inf;
if(mid >= l)res = max(res, querymx(l, r, s, mid, ls));
if(mid + 1 <= r)res = max(res, querymx(l, r, mid + 1, e, rs));
return res;
}
int querymi(int l, int r, int s = 1, int e = n, int o = 1)
{
if(l <= s && e <= r) {
return tmi[o];
}
int mid = s + e >> 1;
int res = inf;
if(mid >= l)res = min(res, querymi(l, r, s, mid, ls));
if(mid + 1 <= r)res = min(res, querymi(l, r, mid + 1, e, rs));
return res;
}
void solve()
{
cin >> n;
for(int i = 1;i <= n;i ++)cin >> a[i];
int ix = -1;
for(int i = 1;i <= n;i ++)
if(abs(a[i]) != 1)ix = i;
for(int i = 1;i <= n;i ++)//前缀和
a[i] += a[i - 1];
build();//构建线段树
if(ix == -1)ix = n + 1;
int nomi = 0, nomx = 0;
//第一类区间
for(int i = 1;i <= n;i ++) {
if(i == ix)continue;
if(i <= ix) {
nomi = min(nomi, querymi(i, ix - 1) - a[i - 1]);
nomx = max(nomx, querymx(i, ix - 1) - a[i - 1]);
} else {
nomi = min(nomi, querymi(i, n) - a[i - 1]);
nomx = max(nomx, querymx(i, n) - a[i - 1]);
}
}
//第二类区间
int hasmi = 0, hasmx = 0;
if(ix <= n) {
hasmi = a[ix] - a[ix - 1];
hasmx = a[ix] - a[ix - 1];
int lmi = 0, lmx = 0;
int rmi = 0, rmx = 0;
int now = 0;
for(int i = ix - 1;i;i --) {
now += a[i] - a[i - 1];
lmi = min(lmi, now);
lmx = max(lmx, now);
}
now = 0;
for(int i = ix + 1;i <= n;i ++) {
now += a[i] - a[i - 1];
rmi = min(rmi, now);
rmx = max(rmx, now);
}
hasmi = hasmi + lmi + rmi;
hasmx = hasmx + lmx + rmx;
}
set<int> ans;
for(int i = nomi;i <= nomx;i ++)ans.insert(i);
for(int i = hasmi;i <= hasmx;i ++)ans.insert(i);
cout << ans.size() << '\n';
for(auto &i : ans)cout << i << ' ';
cout << '\n';
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t = 1;cin >> t;
while(t --)solve();
return 0;
}
CF2043C Sums on Segments的更多相关文章
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...
- B. Odd Sum Segments CF(分割数组)
题目地址 http://codeforces.com/contest/1196/problem/B B. Odd Sum Segments time limit per test 3 seconds ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor
结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...
- Application package 'AndroidManifest.xml' must have a minimum of 2 segments.
看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...
- segments&cache
Segments 执行效果 命令 在 sense 里边执行 GET /abcd/_segments 前边的是索引名称,后边是请求 段信息 说明 索引是面向分片的,是由于索引是由一个或多个分片( ...
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Covering Segments by Points
Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...
随机推荐
- FineReport其他js记录
1.js修改日期组件 背景色 边框 setTimeout(function () { var color = "#092347"; var fontColor = "wh ...
- 金TECH频道|最近备受关注的应用重构,到底怎么做?
"金TECH频道"旨在为您分享中电金信助力行业数字化转型的最新产品业务动态.技术观点洞察与应用实践案例.让我们在这里,与行业发展同频共振,共筑数字新基石.
- python 检测免费代理ip是否有效
python 检测免费代理ip是否有效,免费ip获取地址https://www.zdaye.com/free/ import requests IPAgents = [ "218.89.51 ...
- Qt开源作品40-图片及文字与base64编码互换
一.前言 对于图片的传输,通俗的做法一般有两种,一种是直接二进制文件传输,比如先传输开始标记,带文件名称.文件字节长度,然后挨个分包发送.最后发送文件结束标记,这种方式必须要求接收方按照自己定义的规则 ...
- [转]在MyBatis中使用pageHelper5.1.9分页插件实现物理分页
pagehelper的GIT地址:https://github.com/pagehelper/Mybatis-PageHelper/ 废话少说,直接给出中文官方链接: 1.如何使用分页插件 2.HOW ...
- [LC1161]最大层内元素和
题目概述 给你一个二叉树的根节点 root.设根节点位于二叉树的第 1 层,而根节点的子节点位于第 2 层,依此类推. 请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那 ...
- 人类讨厌AI的缺点,其实自己也有,是时候反思了。
马特·科拉默摄于Unsplash 前言:人类讨厌AI,其实就是讨厌自己! 如果你问一些人对人工智能的看法,你可能会听到诸如不道德.偏见.不准确甚至操纵这样的词语. 人工智能因为种种原因正备受批评.它让 ...
- Solution -「LOCAL」Minimal DFA
\(\mathscr{Description}\) Private link. 令 \(\Sigma=\{\texttt a,\texttt b\}\),对于所有形式语言 \(L\subset ...
- CDS标准视图:维护通知原因数据 I_MaintNotificationCauseData
视图名称:维护通知原因数据 I_MaintNotificationCauseData 视图类型:基础 视图代码: 点击查看代码 @AbapCatalog.sqlViewName: 'INOTIFCAU ...
- LCR 170. 交易逆序对的总数
交易逆序对的总数 在股票交易中,如果前一天的股价高于后一天的股价,则可以认为存在一个「交易逆序对」.请设计一个程序,输入一段时间内的股票交易记录 record,返回其中存在的「交易逆序对」总数. 示例 ...