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. Android嵌入式开发初学者的几个注意点

    一:首先你必须了解ARM平台 Android 移植与驱动核心开发,当然也可以是X86和其他的平台,不过其他平台的Android智能终端开发并不是很多. Android嵌入式智能操作系统是基于Linux ...

  2. 面试官: 聊一聊Babel

    点击关注本公众号获取文档最新更新,并可以领取配套于本指南的 <前端面试手册> 以及最标准的简历模板. 前言 Babel 是现代 JavaScript 语法转换器,几乎在任何现代前端项目中都 ...

  3. 主动降噪技术(ANC)的前生今世--概念历史

    一 概念 假如使用一句通俗的语言来概述ANC的原理的话,那就是:通过发出与噪声相位相反,频率.振幅相同的声波与噪声干涉实现相位抵消. 使用比较正式的语言来解释就是:动降噪通过降噪系统产生与外界噪音相等 ...

  4. js 混合排序(类似中文手机操作系统中的通讯录排序)

    在阳光明媚最适合打盹的下午, 特意静音的手机竟然动起来了, 你没看错, 它震动了.... 上帝(顾客)来电, "报表查询系统左侧树状菜单中设备的中文名称不能排序", 要增加排序功能 ...

  5. List之ConcurrentModificationException异常

    一.前言 Java开发工作中,集合类ArrayList应该是使用非常频繁了.在使用过程中,可能会遇到迭代删除的需求场景,此时如果代码书写不当,就会抛出 java.util.ConcurrentModi ...

  6. 使用python asyncio+aiohttp做接口测试(TODO)

    线程是操作系统层面的“并行”, 协程是应用程序层面的“并行”. 协程本质上就是:提供一个环境,保存一些需要等待的任务,当这些任务可以执行(等待结束)的时候,能够执行.再等待的过程中,程序可以执行别的任 ...

  7. WinServer 2012 R2 安装python3.6时出现错误:0x80240017 导致安装失败

    解决方法: 依次检查并更新补丁:KB2919442,KB2919355,kb2999226 KB2919442:https://www.microsoft.com/zh-cn/download/det ...

  8. [python]python的异常处理

    异常处理:首先了解异常,程序出现逻辑错误或者用户输入不合法都会引发异常,而这些异常并不是致命的所以不会导致程序崩溃死掉.可以利用Python提供的异常处理机制在异常出现时及时捕获,并从内部自我消化. ...

  9. Scrapy项目 - 数据简析 - 实现豆瓣 Top250 电影信息爬取的爬虫设计

    一.数据分析截图(weka数据分析截图 ) 本例实验,使用Weka 3.7对豆瓣电影网页上所罗列的上映电影信息,如:标题.主要信息(年份.国家.类型)和评分等的信息进行数据分析,Weka 3.7数据分 ...

  10. Unity - 存读档机制简析

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