You are given an array consisting of n non-negative integers a1, a2, ..., an.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.
After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
The third line contains a permutation of integers from 1 to n — the order used to destroy elements.
Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.
4 1 3 2 5 3 4 1 2
5 4 3 0
5 1 2 3 4 5 4 2 3 5 1
6 5 5 1 0
8 5 5 4 4 6 6 5 5 5 2 8 7 1 3 4 6
18 16 11 8 8 6 6 0
Consider the first sample:
- Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5 consists of one integer 5.
- Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum 4 consists of two integers 1 3.
- First element is destroyed. Array is now * 3 * * . Segment with maximum sum 3 consists of one integer 3.
- Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
题意:
有两个数列a[], b[],按照数组b的顺序依次删除数组a中的数值,问a数组每次删除后剩余数字的最大连续和
【b[i]表示删除a[b[i]]】
思路:
题目要求是依次删除,我们倒着求,依次加入某个数,用并查集更新连续数值和,用树状数组求最大值
【嘿~说点废话,对知识点的运用不够熟练,打cf的时候完全没想到用并查集做,以后如果看到“点合并,区间合并,并求区间值”这种问题的时候,应该要马上想到并查集。】
#include <bits/stdc++.h>
using namespace std;
#define ll __int64
const int N=1e5+10;
ll sum[N], a[N], ans[N], h[N];
int b[N], fa[N];
int find(int x){
return fa[x]==x?x:find(fa[x]);
}
void Union(int x, int y){
int fx = find(x);
int fy = find(y);
sum[fx]+=sum[fy];
fa[fy] = fx;
}
int lowbit(int x)
{
return x & (-x);
}
void update(int x, int n)
{
int lx, i;
while (x <= n)
{
h[x] = sum[x];
lx = lowbit(x);
for (i=1; i<lx; i<<=1)
h[x] = max(h[x], h[x-i]);
x += lowbit(x);
}
}
ll query(int x, int y)
{
ll ans = 0;
while (y >= x)
{
ans = max(sum[y], ans);
y --;
for (; y-lowbit(y) >= x; y -= lowbit(y))
ans = max(h[y], ans);
}
return ans;
}
int main(){
int n;
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%I64d", &a[i]);
for(int i=1; i<=n; i++) scanf("%d", &b[i]);
memset(fa, -1, sizeof(fa));
memset(sum, 0, sizeof(sum));
for(int i=n; i>=1; i--){
ans[i] = query(1, n);
int id = b[i];
sum[id] = a[id];
fa[id] = id;
if(fa[id-1]!=-1) Union(id-1, id);
if(fa[id+1]!=-1) Union(id, id+1);
int fid = find(id);
update(fid, n);
}
for(int i=1; i<=n; i++) printf("%I64d\n", ans[i]);
return 0;
}
【(⊙o⊙)…我想的复杂了,没必要用树状数组求最大值,直接用maxn来记录最大值就可以了,每次更新了sum[fa[id]],直接比较maxn跟sum[fa[id]]的大小就可以了】
做法还有很多,拓展思路,我来记录一下
灵活运用STL的做法
用set来存区间,一开始的区间是[1, n],删除一个数后,去掉[1, n]区间,存入[1, b[1]-1]跟[b[1]+1, n]这两个区间。
每去掉一个区间就加上两个区间
用multi_set来存区间和,每次删掉相应的一个区间和,加上两个区间和,然后输出最大值
用set<pair<终点, 起点> >s来存区间和
用set< pair<int,int> > ::iterator it=s.lower_bound(make_pair(b[i],1))找区间
#include <bits/stdc++.h>
using namespace std;
#define ll __int64
const int N=1e5+10;
ll sum[N], a[N], ans[N];
int b[N];
set<pair<int, int> >s;
set<pair<int, int> > :: iterator it;
multiset<ll>ms;
int main(){
int n;
scanf("%d", &n);
sum[0] = 0;
for(int i=1; i<=n; i++) scanf("%I64d", &a[i]), sum[i] = sum[i-1]+a[i];
for(int i=1; i<=n; i++) scanf("%d", &b[i]);
s.clear(), ms.clear();
s.insert(make_pair(n, 1)), ms.insert(sum[n]);
for(int i=1; i<=n; i++){
it = s.lower_bound(make_pair(b[i], 1));
int r = it->first, l = it->second;
s.erase(*it);
s.insert(make_pair(b[i]-1, l)), s.insert(make_pair(r, b[i]+1));
ms.erase(ms.find(sum[r]-sum[l-1]));
ms.insert(sum[b[i]-1]-sum[l-1]), ms.insert(sum[r]-sum[b[i]]);
multiset<ll>:: reverse_iterator reit = ms.rbegin();
printf("%I64d\n", *reit);
}
return 0;
}