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

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:

  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.

Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.


如果正着考虑我们不好处理删除元素;

所以时光倒流, 我们从后往前加元素, 这样题就变成了加入元素;

于是我们可以用并查集维护联通性, 每次加入一个元素, 我们就可以把它两边的联通块合并起来;

然后每次取max就是当前的答案;

有一个细节,就是你不能合并一个还未“出现”的元素, 所以我们要开一个bool数组判断是否出现过;


#include <iostream>
#include <cstdio>
using namespace std;
int n, a[], p[];
int fa[];
long long val[];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
long long ans;
long long put[];
bool des[];
int main()
{
scanf("%d", &n);
for (register int i = ; i <= n ; i ++) scanf("%d", &a[i]);
for (register int i = ; i <= n ; i ++) scanf("%d", &p[i]);
for (register int i = ; i <= n + ; i ++) fa[i] = i;
for (register int i = n ; i >= ; i --){
put[i] = ans;
des[p[i]] = ;
int l = p[i] - , r = p[i] + ;
int fl = Find(l), fr = Find(r), fp = Find(p[i]);
if (fl != fp and des[l])fa[fl] = fp, val[fp] += val[fl];
if (fr != fp and des[r])fa[fr] = fp, val[fp] += val[fr];
val[fp] += a[p[i]];
ans = max(ans, val[fp]);
}
for (register int i = ; i <= n ; i ++) printf("%lld\n", put[i]);
return ;
}

[CF722C] Destroying Array的更多相关文章

  1. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

  2. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. [codeforces722C]Destroying Array

    [codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  7. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. 【37.38%】【codeforces 722C】Destroying Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

随机推荐

  1. Docker学习之docker常用命令

    docker ps -a 表示所有容器 docker pull 获取image docker build 创建image docker run 运行container docker images 列出 ...

  2. 实例化Bean的几种方法

    1.使用构造器实例化Bean.         当没有指定实例化方法时,Spring IoC容器能使用默认空构造器.构造器实例化包括默认空构造器和有参数构造器两种方式创建Bean. 2.使用构造器实例 ...

  3. [Spark] 00 - Install Hadoop & Spark

    Hadoop安装 Java环境配置 安装课程:安装配置 配置手册:Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04[依照步骤完成配置] jsk安装使用的链接中第 ...

  4. ##发送post时,设置了utf-8,中文还是乱码?

    发送post时,设置了utf-8,中文还是乱码? 我们用HttpUrlConnection或HttpClient发送了post请求,其中有中文,虽然我们两边都设置了utf-8,但还是乱码? 我们在re ...

  5. Loadrunner录制步骤及说明

    做好业务准备,如环境配置.服务启动等 打开Virtual User Generator界面---->New---->选择协议,录制网页时一般选择Web(HTTP/HTML) Start R ...

  6. Centos修改yum源为国内阿里源

    以下为修改Centos6.5的yum源: 1. 备份原镜像文件,便于后期恢复 [root@keepmydream ~]# mv /etc/yum.repos.d/CentOS-Base.repo /e ...

  7. filebeat相关registry文件内容解析

    filebeat的registry文件中存放的是被采集的所有日志的相关信息. linux中registry中一条日志记录的内容如下 {"source":"/var/log ...

  8. 蓝桥杯 algo122 未名湖的烦恼 简单题

    #include <iostream> using namespace std; int m, n, ans; void solve(int m, int n, int cnt) { &a ...

  9. Android Studio [Toast]

    ToastActivity.java package com.xdw.a122; import android.support.v7.app.AppCompatActivity; import and ...

  10. Unity - 存读档机制简析

    本文旨在于简要分析Unity中的两种存档机制,即:PlayerPrefs数据持久化方法及Serialization数据序列化方法 较比与源项目,我另加了JSON方法.XML方法等及一些Unity设置, ...