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
  1. 4
    1 3 2 5
    3 4 1 2
Output

Copy
  1. 5
    4
    3
    0
Input

Copy
  1. 5
    1 2 3 4 5
    4 2 3 5 1
Output

Copy
  1. 6
    5
    5
    1
    0
Input

Copy
  1. 8
    5 5 4 4 6 6 5 5
    5 2 8 7 1 3 4 6
Output

Copy
  1. 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数组判断是否出现过;


  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int n, a[], p[];
  5. int fa[];
  6. long long val[];
  7. int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
  8. long long ans;
  9. long long put[];
  10. bool des[];
  11. int main()
  12. {
  13. scanf("%d", &n);
  14. for (register int i = ; i <= n ; i ++) scanf("%d", &a[i]);
  15. for (register int i = ; i <= n ; i ++) scanf("%d", &p[i]);
  16. for (register int i = ; i <= n + ; i ++) fa[i] = i;
  17. for (register int i = n ; i >= ; i --){
  18. put[i] = ans;
  19. des[p[i]] = ;
  20. int l = p[i] - , r = p[i] + ;
  21. int fl = Find(l), fr = Find(r), fp = Find(p[i]);
  22. if (fl != fp and des[l])fa[fl] = fp, val[fp] += val[fl];
  23. if (fr != fp and des[r])fa[fr] = fp, val[fp] += val[fr];
  24. val[fp] += a[p[i]];
  25. ans = max(ans, val[fp]);
  26. }
  27. for (register int i = ; i <= n ; i ++) printf("%lld\n", put[i]);
  28. return ;
  29. }

[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. 将dos格式文件转换为unix格式

    在windows下换行符是\r\n,表示回到行首并换到下一行 而unix系统中换行符是\n 这样就存在一个问题,在windows上的文档到了unix上可能就无法使用了 针对这个情况有几种解决办法: 1 ...

  2. Centos 安装java

    1.下载jdk:jdk-8u181-linux-x64.tar.gz,下载地址不用我说了把.. 2.新建java文件夹 mkdir /usr/java 3.将下载的包传到此文件夹中,然后解压 cd / ...

  3. 【linux】【jenkins】自动化运维七 整合sonarqube代码审查

    1.安装插件:SonarQube Scanner for Jenkins 插件安装教程参考:https://www.cnblogs.com/jxd283465/p/11542680.html 2.So ...

  4. Java图片处理:ico格式转 PNG/JPG等格式

    一. 什么是ico图标? ico是一种图标格式,大量应用于网站,各个软件的logo或图标展示. 我们在进入某个网站或网页,它们上方标题左侧各自都带有logo图标. 这就是favicon.ico图标,它 ...

  5. Flutter学习笔记(27)--数据共享(InheritedWidget)

    如需转载,请注明出处:Flutter学习笔记(27)--数据共享(InheritedWidget) InheritedWidget是Flutter中非常重要的一个功能型组件,它提供了一种数据在widg ...

  6. Spring 梳理 - javaConfig在App和webApp中的应用

    package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Co ...

  7. Java中Synchronized的优化原理

    我们知道,从 JDK1.6 开始,Java 对 Synchronized 同步锁做了充分的优化,甚至在某些场景下,它的性能已经超越了 Lock 同步锁.那么就让我们来看看,它究竟是如何优化的. 原本的 ...

  8. 无暇代码(js的整洁之道)

    如果你关注代码本身和代码的编写方式,而不是只关心它是否能工作,那么你写代码是有一定的水准.专业开发人员将为未来的自己和“其他人”编写代码,而不仅仅只编写当前能工作就行的代码.在此基础上,简洁代码可以定 ...

  9. 第二篇:php开发工具

    倍,这里为您介绍一些常用的工具. PHP IDE PHP IDE也不少,主要从几个方面进行筛选: 跨平台(能够同时在windows,mac或者ubuntu上面运行) 版本控制(SVN,GIT) 文件历 ...

  10. JavaScript实现各种排序算法

    前言:本文主要是用JavaScript实现数据结构中的各种排序算法,例如:插入排序.希尔排序.合并排序等. 冒泡排序 function bubbleSort(arr) { console.time(& ...