Codeforces 722C - Destroying Array By Assassin

C. Destroying Array
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 ≤ 100000) — 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 integerthe maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.

Examples

Input
4
1 3 2 5
3 4 1 2


Output
5
4
3
0

Input
5
1 2 3 4 5
4 2 3 5 1

Output
6
5
5
1
0

Input
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6

Output
18
16
11
8
8
6
6
0

Note

Consider the first sample: 
1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5. 
2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3. 
3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3. 
4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0. 

这题目不是我想的。。。正常大家都是并查集搞过的,但是想不通,于是学习了一下STL的方法,简直开阔了新的视野!

我们用到了set和multiset这两个STL,建议大家自己动手学一学,可以看这里学习一下迭代器
set和multiset学习

然后我们用set(pair<.int,int>)记录每个从左到右联通的线段,用multiset记录已经写入的每一段的总和!

那么为什么用这个,第一用set记录线段,不重合所以能保证不会出现相同项,和用multiset首先因为可能和相同,并且我们只是要最大值,set和multiset都是默认升序的,那么我们想要最大值,只需要反向迭代器的开始就行了(PS:如果觉得迭代器蒙蔽,一定学习一下set,基本上就明白了)。注意这里set存入这样存make_pair(tail,head) (这个make_pair很直观,生成pair组),至于为什么后面就知道了。

那么我们每一次只要找到销毁的东西的位置在哪个线段就行,怎么找!包含第p个数的区间就是 l_bound (make_pair(p,0))
lower_bound() 返回指向大于(或等于)某值的第一个元器)))

然后注意删除sum中的值是需要find一下的,否则你这么的数就
了。。。

之后就是插入,这里注意我么你的记录方式!(i,j]是介样的!

代码就简了

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
#define ll long long
#define P pair<int,int>
using namespace std;
long long head[100005]={0};
set<P>seg;
multiset<ll>sum;

void erase(int p)
{
    set<P> :: iterator it = seg.lower_bound(make_pair(p,0));
    sum.erase(sum.find(head[it->first]-head[it->second]));

    seg.insert(make_pair(p-1,it->second)),sum.insert(head[p-1]-head[it->second]);
    seg.insert(make_pair(it->first,p)),sum.insert(head[it->first]-head[p]);
    seg.erase(it);
}

ll max()
{
    multiset<ll> :: reverse_iterator mit=sum.rbegin();
    printf("%I64d\n",*mit);
}

int main(){
    input;
    int n,k;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&k);
            head[i]=head[i-1]+k;
        }
        seg.insert(make_pair(n,0));  //seg的初始状态,将开始多的一段加入(0,n] 
        sum.insert(head[n]);         //sum加入第一个总和
        for(int i=1;i<=n;i++){
            scanf("%d",&k);
            erase(k);
            max();
        } 
    }
    return 0;
} 

涨姿势!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值