Destroying Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
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.
Examples
input
Copy
4
1 3 2 5
3 4 1 2
output
Copy
5
4
3
0
input
Copy
5
1 2 3 4 5
4 2 3 5 1
output
Copy
6
5
5
1
0
input
Copy
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
output
Copy
18
16
11
8
8
6
6
0
Note
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.
题目大意:给出一个序列,求按顺序删掉某些数后的最大连续和。
解题思路:删除不好处理,倒着改成插入,然后用线段树维护最大连续和。。。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pb(x) push_back(x)
#define LL long long
const int N = 1e5+5;
LL a[N];
int b[N];
vector<LL>v;
struct node
{
LL sum;
LL msum;
LL lsum,rsum;
int sz;
}t[N*4];
void push_up(int rt,int l,int r)
{
int len=r-l+1;
int rlen=len/2;
int llen=len-len/2;
if(t[rt<<1].sz==llen)
{
t[rt].lsum=t[rt<<1].sum+t[rt<<1|1].lsum;
}
else t[rt].lsum=t[rt<<1].lsum;
if(t[rt<<1|1].sz==rlen)
{
t[rt].rsum=t[rt<<1|1].sum+t[rt<<1].rsum;
}
else t[rt].rsum=t[rt<<1|1].rsum;
t[rt].msum=max(t[rt<<1].lsum,t[rt].msum);
t[rt].msum=max(t[rt<<1|1].rsum,t[rt].msum);
t[rt].msum=max(t[rt<<1].rsum+t[rt<<1|1].lsum,t[rt].msum);
t[rt].msum=max(t[rt<<1].msum,t[rt].msum);
t[rt].msum=max(t[rt<<1|1].msum,t[rt].msum);
t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
t[rt].sz=t[rt<<1].sz+t[rt<<1|1].sz;
}
void build(int rt,int l,int r)
{
if(l==r)
{
t[rt].sum=0;
t[rt].msum=0;
t[rt].lsum=0;
t[rt].rsum=0;
t[rt].sz=0;
return ;
}
int m=(l+r)>>1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
}
void upd(int rt,int l,int r,int pos)
{
if(l==r)
{
t[rt].sum=a[l];
t[rt].msum=a[l];
t[rt].lsum=a[l];
t[rt].rsum=a[l];
t[rt].sz=1;
return ;
}
int m=(l+r)>>1;
if(pos<=m) upd(rt<<1,l,m,pos);
else upd(rt<<1|1,m+1,r,pos);
push_up(rt,l,r);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",b+i);
}
for(int i=n;i>=1;i--)
{
upd(1,1,n,b[i]);
v.pb(t[1].msum);
}
if(n==1)
{
puts("0");
return 0;
}
for(int i=v.size()-2;i>=0;i--)
{
printf("%lld\n",v[i]);
}
printf("%lld\n",0);
}