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. jvm 内存溢出

    堆内存溢出 堆内存中存在大量对象,这些对象都有被引用,当所有对象占用空间达到堆内存的最大值,就会出现内存溢出OutOfMemory:Java heap space 永久代溢出 类的一些信息,如类名.访 ...

  2. python+selenium一:对浏览器的常规操作

    # 1.打开Firefox浏览器 from selenium import webdriverdriver = webdriver.Firefox()driver.get("https:// ...

  3. charles SSL代理设置

    本文参考:charles SSL代理设置 charles SSL代理设置 SSL Proxying Srtting 这里最常用的设置就是第一个ssl proxying,这里记录了需要捕获哪些ssl的信 ...

  4. 05:(H5*) node、npm、nrm

    目录: 1:NPM 2:CNPM 3:NRM 4:homebrew 5:具体指令 6: -g -S -D 1:NPM NPM的全称是Node Package Manager, 是一个NodeJS包管理 ...

  5. 快速获取dom到body左侧和顶部的距离,简单粗暴无bug-getBoundingClientRect

    获取dom到body左侧和顶部的距离-getBoundingClientRect 平时在写js的时候,偶尔会需要用js来获取当前div到 body 左侧.顶部的距离.网上查一查,有很多都是通过offs ...

  6. Kubernetes学习之k8s

    k8s是什么 云原生 越来越多的开发者不仅使用容器作为应用部署和运行的载体,还积极使用了与容器这个应用载体天生匹配的微服务的架构,并依靠容器调度编排引擎的帮助,以保持对外部的敏捷性,这种容器化的微服务 ...

  7. 品Spring:bean定义上梁山

    认真阅读,收获满满,向智慧又迈进一步... 技术不枯燥,先来点闲聊 先说点好事高兴一下.前段时间看新闻说,我国正式的空间站建设已在进行当中.下半年,长征五号B运载火箭将在海南文昌航天发射场择机将空间站 ...

  8. Webpack配置区分开发环境和生产环境

    在项目开发的时候,我们通常会将程序分为开发环境和生产环境(或者叫线上环境),开发环境通常指的是我们正在开发的这个阶段所需要的一些环境配置,也就是方便我们开发人员调试开发的一种环境:生产环境通常指的是我 ...

  9. embedding技术

    目录 word2vec 负采样 目标函数 反向梯度 层次softmax NPLM的目标函数和反向梯度 目标函数 反向梯度 GNN(图神经网络) deepwalk node2vec 附录 word2ve ...

  10. DataFrame 转换为Dataset

    写在前面: A DataFrame is a Dataset organized into named columns. A Dataset is a distributed collection o ...