Codeforces 722C Destroying Array【逆序(离线)思维+并查集】

C. 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
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

题目大意:

给你长度为N的一个序列,其一开始所有数都在其对应的位子上,再给你一个长度为N的序列,表示第i次将删除哪个位子的数。每次操作之后,输出连续的序列中最大的和。

分析样例:

1 3 2 5

3 4 1 2

5

4

3

0

第一次删除第三个位子的数据后序列变成:

1 3 X 5

第一段连续的序列的和为4,第二段连续的序列的和为5,那么输出5

第二次删除第四个位子的数据后序列变成:

1 3 X X

只有一段连续的序列的和为4,输出4.

第三次删除第一个位子的数据后序列变成:

X 3 X X ,只有一段连续的序列的和为3,输出3。

最后一次删除之后,序列为空,输出0。


思路:


1、首先我们往下删除数据相对而言想起来比较难处理,难维护,那么我们逆序来想,从一个空的序列,向一个满的序列去加。


2、那么我们将每个点都先看成是孤立的,现在一共N个点,并且设定一个数组vis【i】,表示第i个数(点),是否已经加上了对应位子的数据。

那么对应我们将一个点的数据加上了之后:vis【b【i】】=1;表示这个数已经加上了对应位子的数据。然后判断其左边的数据是否也加上了(vis【b【i】-1】),如果vis【b【i】-1】==1,那么merge(b【i】,b【i】-1),表示将两个点并在一起,形成一段连续的序列,那么其序列和的确定问题呢?

我们再设定一个ji【i】,表示以i为根的集合的权值和,其实就是说是以i为根的序列的价值和。那么在merge(b【i】,b【i】-1)的时候,对应如果:

f【find(b【i】)】=find(b【i】-1),那么对应要有:ji【find(b【i】-1)】+=ji【find(b【i】)】;


3、那么每一次将操作进行完毕之后,我们维护要输出的解ans=max(ans,ji【find(b【i】)】)即可。


具体细节参考Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll ji[1000000];
ll b[1000000];
int vis[1000000];
int f[1000000];
ll output[1000000];
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        f[B]=A;
        ji[A]+=ji[B];
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&ji[i]);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&b[i]);
        }
        ll ans=0;
        int contz=0;
        for(int i=n;i>=1;i--)
        {
            output[contz++]=ans;
            vis[b[i]]=1;
            if(b[i]-1>=1&&vis[b[i]-1]==1)
            {
                merge(b[i],b[i]-1);
            }
            if(b[i]+1<=n&&vis[b[i]+1]==1)
            {
                merge(b[i],b[i]+1);
            }
            ans=max(ji[find(b[i])],ans);
        }

        for(int i=contz-1;i>=0;i--)
        {
            printf("%I64d\n",output[i]);
        }
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值