BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
2017-10-03 19:14
405 查看
题意:给定n个元素的序列,给出m个询问:求l[i]~r[i]的最大子段和(可选空子段)。
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。
比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6。
(n, m, |a[i]|) <= 1e5
思路:每个子串都是某个前缀的后缀,所以我们依旧枚举每个前缀,用线段树维护它的所有后缀。
(点击打开链接)
按照右端点排序询问。我们令
线段树节点
然后
后面三句很好理解就不说了。第一句是因为tag是一个累加变量,而距离上一次clear我们还有
-|| 而第二句的话就同理了,参照第三句和第四句的关系以及第一句的意义。
代码:
#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,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;
}
相关文章推荐
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
- 【线段树】spoj GSS2 Can you answer these queries II
- BZOJ2482: [Spoj1557] Can you answer these queries II
- [SPOJ1557][GSS2][线段树]Can you answer these queries II[好题]
- bzoj2482 [Spoj1557] Can you answer these queries II
- bzoj 2482: [Spoj1557] Can you answer these queries II
- [SPOJ GSS2] Can you answer these queries II [线段树]
- 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 (gss2)线段树
- 【SPOJ - GSS2】Can you answer these queries II(线段树)
- SPOJ 1557. Can you answer these queries II (很强的线段树)
- spoj(GSS3) 1716 Can you answer these queries III(线段树求最大子段和)
- SPOJ GSS3 Can you answer these queries III(线段树)
- SPOJ 1043 Can you answer these queries I(GSS1 线段树)
- [SPOJ GSS1] Can you answer these queries I [线段树]
- SPOJ GSS1 Can you answer these queries I ——线段树