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. HoverTree菜单0.1.3新增效果

    HoverTree菜单0.1.3增加弹出菜单的动态效果,可以是动态下拉,也可以是动态淡入. 效果请看:http://keleyi.com/jq/hovertree/demo/demo.0.1.3.ht ...

  2. go语言 rune切片

    go语言 rune切片 示例 package main import ( "fmt" ) //http://www.cnblogs.com/osfipin/ func main() ...

  3. HTML <select> 标签 创建单选或多选菜单

    所有主流浏览器都支持 <select> 标签. select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中 ...

  4. 实现a标签中的各种点击(onclick)事件的方法

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();"这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题, ...

  5. 关于WPF中文件夹浏览对话框的方式

    文件夹浏览时dialogresult要写全引用路径 string path=null; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd ...

  6. JEPF 3.1.2 发布,我们的软件机床(软件快速开发平台)

    JEPF新一代软件快速开发平台(Java Elephant Platform)是一款优秀的平台产品,它本着灵活.快捷开发.高性能.高协作性.高稳定性.高可用性.人性化的操作体验为设计宗旨历经2年研发成 ...

  7. 基于 VLC 的 Android 多媒体解决方案

    前段时间项目中需要在 Android 中播放视频.流媒体.查看监控,就研究了一下 Android 多媒体解决方案. 查找了一下,大致有如下几种: Android MediaPlayer FFmpeg ...

  8. html 出现的错误

    1.HTML页面为什么设置了UTF-8仍然中文乱码 用记事本写,保存后在网页上运行出现了乱码,换成GB2312能正确显示中文 代码没有问题,问题就出记事本身上. <meta charste=&q ...

  9. 监听SD卡状态

     最近在做项目时遇到需要处理SD卡拔出时的监听,在网上找了很多资料.总结了一下, 用接收广播处理最有效率     sd卡拔插时会发送广播,具体如下(摘自一位大虾的博客  来自:http://blog. ...

  10. Android 判断SIM卡属于哪个移动运营商

    第一种方法:获取手机的IMSI码,并判断是中国移动\中国联通\中国电信 TelephonyManager telManager = (TelephonyManager) getSystemServic ...