BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
来源:互联网 发布:nginx 二级域名转发 编辑:程序博客网 时间:2024/05/18 03:14
题意:给定n个元素的序列,给出m个询问:求l[i]~r[i]的最大子段和(可选空子段)。
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。
比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6。
(n, m, |a[i]|) <= 1e5
思路:每个子串都是某个前缀的后缀,所以我们依旧枚举每个前缀,用线段树维护它的所有后缀。
(点击打开链接)
按照右端点排序询问。我们令s[i]
为不重复的sum[i..x]
,其中x
在最外层循环中从1~n
。x
每前进一位,把s[last[x]+1]
到s[x]
全部加上a[x]
,其中last[x]
是x上一次出现的位置。现在,当x等于某一个询问的右端点r
的时候,我们的询问[l, r]
,相当于询问所有历史中(包括现在)出现的最大的s[k] (k <= r)
。听到历史这个词,很容易想到可持久化数据结构,或者说二维数据结构(比如套一个时间维)。但是实际上,这题要求的历史,是从一开始的历史,而不是从某个时间点开始的,因此,还是可以直接使用线段树的。
线段树节点[L, R]
记录四个东西:
max: 现在的s[L..R]中最大的一个tag: 现在还有多少数没有下传给孩子evermax: 历史到现在的s[L..R]中最大的一个evertag: 从上一次清除tag到现在最大的tag
然后clear
过程就是一个非常纠结的事情。
son.evertag = max(son.evertag, son.tag + p.evertag);son.evermax = max(son.evermax, son.max + p.evertag);son.tag += p.tag;son.max += p.tag;p.tag = p.evertag = 0;
后面三句很好理解就不说了。第一句是因为tag是一个累加变量,而距离上一次clear我们还有p.evertag
没有下传给孩子,所以说孩子还有son.tag+p.evertag
这么多没有给孩子的孩子- -|| 而第二句的话就同理了,参照第三句和第四句的关系以及第一句的意义。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn = 2e5+5;const int N = 1e5;ll a[maxn], ans[maxn], pre[maxn*2], n, m;ll sum[maxn*4], preSum[maxn*4], lazy[maxn*4], preLazy[maxn*4];struct node{ int l, r, id; bool operator <(const node &a) const { return r < a.r; }}op[maxn];void pushDown(int root){ if(lazy[root] || preLazy[root]) { preSum[root*2] = max(preSum[root*2], sum[root*2]+preLazy[root]); preLazy[root*2] = max(preLazy[root*2], lazy[root*2]+preLazy[root]); sum[root*2] += lazy[root], lazy[root*2] += lazy[root]; preSum[root*2+1] = max(preSum[root*2+1], sum[root*2+1]+preLazy[root]); preLazy[root*2+1] = max(preLazy[root*2+1], lazy[root*2+1]+preLazy[root]); sum[root*2+1] += lazy[root], lazy[root*2+1] += lazy[root]; lazy[root] = preLazy[root] = 0; }}void pushUp(int root){ preSum[root] = max(preSum[root*2], preSum[root*2+1]); sum[root] = max(sum[root*2], sum[root*2+1]);}void update(int root, int l, int r, int i, int j, ll val){ if(i <= l && j >= r) { lazy[root] += val; sum[root] += val; preLazy[root] = max(preLazy[root], lazy[root]); preSum[root] = max(preSum[root], sum[root]); return ; } pushDown(root); int mid = (l+r)/2; if(i <= mid) update(root*2, l, mid, i, j, val); if(j > mid) update(root*2+1, mid+1, r, i, j, val); pushUp(root);}ll query(int root, int l, int r, int i, int j){ if(i <= l && j >= r) return preSum[root]; pushDown(root); int mid = (l+r)/2; if(j <= mid) return query(root*2, l, mid, i, j); else if(i > mid) return query(root*2+1, mid+1, r, i, j); else return max(query(root*2, l, mid, i, j), query(root*2+1, mid+1, r, i, j));}int main(void){ while(cin >> n) { memset(pre, 0, sizeof(pre)); memset(sum, 0, sizeof(sum)); memset(preSum, 0, sizeof(preSum)); memset(lazy, 0, sizeof(lazy)); memset(preLazy, 0, sizeof(preLazy)); for(int i = 1; i <= n; i++) scanf("%lld", &a[i]); scanf("%lld", &m); for(int i = 1; i <= m; i++) scanf("%d%d", &op[i].l, &op[i].r), op[i].id = i; sort(op+1, op+1+m); int p = 1; for(int i = 1; i <= n; i++) { update(1, 1, n, pre[a[i]+N]+1, i, a[i]); pre[a[i]+N] = i; while(p <= m && op[p].r == i) { ans[op[p].id] = query(1, 1, n, op[p].l, op[p].r); p++; } } for(int i = 1; i <= m; i++) printf("%lld\n", ans[i]); } return 0;}
阅读全文
1 0
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- [SPOJ GSS2] Can you answer these queries II [线段树]
- 【线段树】spoj GSS2 Can you answer these queries II
- GSS2 - Can you answer these queries II(线段树)
- SPOJ 1557 Can you answer these queries II(GSS2 线段树)
- spoj 1557 Can you answer these queries II (gss2)线段树
- SPOJ GSS2 Can you answer these queries II
- spoj 1557. Can you answer these queries II(线段树)
- 【SPOJ】1557 Can you answer these queries II 线段树
- SPOJ 1557 Can you answer these queries II(离线处理+线段树求历史最大)
- [SPOJ1557][GSS2][线段树]Can you answer these queries II[好题]
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1043. Can you answer these queries I (线段树)
- spoj 1716. Can you answer these queries III(线段树)
- spoj 2713. Can you answer these queries IV(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- SPOJ GSS4 Can you answer these queries IV (线段树)
- 链表J 不敢死队问题
- 第四课、C程序结构
- 性能核心概念
- 慎用Servlet成员变量-导致线程安全问题
- css之水平垂直居中篇
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- 组合数取模
- 51单片机之外设——74HC138、74HC02、74HC573讲解(三)
- 贪心A 删数问题
- Spring事务管理的使用
- HDOJ2044
- 贪心B 活动选择问题(很经典)
- 第五课、 C基本语法
- php 获取当前的域名