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.

题意:正整数序列,每次毁灭一个元素,每次求剩下元素不包含毁灭元素的线段的最大和


yy了各种方法,BIT,线段树,甚至各种STL,然而.....
 
删除不好处理,可以逆序加入点,用并查集维护区间连通
和JSOI2008星球大战一个想法,倒着处理
如果i根i+1连通,那么sum[i]+=sum[find(i+1)],再把find(i+1)合并到i里(注意必须是这个合并顺序)
i-1同理
//
// main.cpp
// c
//
// Created by Candy on 10/1/16.
// Copyright © 2016 Candy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,a[N],p[N],vis[N];
ll sum[N],mx=,st[N],top=;
int fa[N];
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} int main(){
n=read();
for(int i=;i<=n;i++) a[i]=read(),fa[i]=i;
for(int i=;i<=n;i++) p[i]=read();
for(int i=n;i>=;i--){
st[++top]=mx;
int cur=p[i],f1=,f2=;
vis[cur]=; sum[cur]+=a[cur];
if(vis[cur-]){f1=find(cur-);sum[cur]+=sum[f1];fa[f1]=cur;}
if(vis[cur+]){f2=find(cur+);sum[cur]+=sum[f2];fa[f2]=find(cur);}
mx=max(mx,sum[cur]);
}
while(top) printf("%I64d\n",st[top--]);
}
 

CF722C. Destroying Array[并查集 离线]的更多相关文章

  1. C. Destroying Array 并查集,逆向思维

    用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...

  2. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  3. CodeForces-722C Destroying Array 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/CodeForces-722C 题意 给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和 思 ...

  4. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. BZOJ5188: [Usaco2018 Jan]MooTube 并查集+离线处理

    BZOJ又不给题面... Luogu的翻译看不下去... 题意简述 有一个$n$个节点的树,边有权值,定义两个节点之间的距离为两点之间的路径上的最小边权 给你$Q$个询问,问你与点$v$的距离超过$k ...

  6. poj 2528 Mayor's posters 线段树 || 并查集 离线处理

    题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...

  7. ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)

    Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...

  8. [CF722C] Destroying Array

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

  9. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

随机推荐

  1. js引入php 用来加载静态页面 输出到页面中

    HTML页面中加入代码 <script type="text/javascript" src="http://www.域名.com/js.php?id=tjyd&q ...

  2. Android忘记密码功能实现

    连续好几天学习都没有什么进展,然而在今天这个烂漫的日子.突然有了学习的动力.想起来前几日老师给布置的android忘记密码的功能实现.今天也有了想法.就是按照老师的建议,简单的回答一个问题,实现此功能 ...

  3. 如何去定义一个jquery插件

    扩展jquery的时候.最核心的方法是以下两种: $.extend(object) 可以理解为jquery添加一个静态方法 $.fn.extend(object) 可以理解为jquery实例添加一个方 ...

  4. GCD应用及其他方法

    1.GCD应用 单例模式        static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{            ...

  5. IOS 杂笔-1(为什么不继承类簇?)

    答:首先,类簇是可以继承的,并不是不可以.例如,我们可以选择继承NSSting,但是此时你用你自己设定的类去调用NSSting的一些方法时,会存在无法实现的问题,这是为什么呢. 1.类簇里有很多私有的 ...

  6. art.dialog.art 中,将子页面窗口中的值传递给父框架中

    artDialog.open.origin.document.getElementById('父元素ID').value=document.getElementById('子页面元素ID').valu ...

  7. Windows操作系统优化(Windows优化大师版) - 进阶者系列 - 学习者系列文章

    Windows优化大师是一款不错的优化软件.笔者以前在使用XP的时候就使用该软件进行优化.下面就简要的介绍该软件优化的过程. 1.  下载该软件. http://dl.youhua.com/youhu ...

  8. git入门学习(一):github for windows上传本地项目到github

    Git是目前最先进的分布式版本控制系统,作为一个程序员,我们需要掌握其用法.Github发布了Github for Windows 则大大降低了学习成本和使用难度,他甚至比SVN都简单. 一.首先在g ...

  9. MongoDB查询重复记录并保存到文件csv

    客户1w用户记录,发现里面有小部分重复数据 需要查出,比对哪些信息不同 https://docs.mongodb.org/manual/reference/operator/aggregation/# ...

  10. Javascript 优化项目代码技巧之语言基础(一)

        Javascript的弱类型以及函数作用域等规则使用编写Javascript代码极为容易,但是编写可维护.高质量的代码却变得十分困难,这个系列的文章将总结在项目开发过程中,能够改善代码可读性. ...