C. Destroying Array

题目连接:

http://codeforces.com/contest/722/problem/C

Description

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.

Sample Input

4

1 3 2 5

3 4 1 2

Sample Output

5

4

3

0

Hint

题意

有n个数,然后每个数的权值是a[i],现在按照顺序去摧毁n个元素,然后每次问你最大的连通块和是多少。

题解:

倒着做,然后用带权并查集去维护就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long fa[maxn],a[maxn],b[maxn],ans[maxn],vis[maxn],sum[maxn],n;
int fi(int x){return fa[x]==x?x:fa[x]=fi(fa[x]);}
void uni(int x,int y)
{
x=fi(x),y=fi(y);
fa[x]=y;sum[y]+=sum[x];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=a[i];
fa[i]=i;
}
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
long long tmp = 0;
for(int i=n;i>1;i--)
{
vis[b[i]]=1;
if(vis[b[i]-1])uni(b[i]-1,b[i]);
if(vis[b[i]+1])uni(b[i]+1,b[i]);
tmp=max(sum[fi(b[i])],tmp);
ans[i-1]=tmp;
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<endl;
}

Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集的更多相关文章

  1. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题

    B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)

    题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...

  6. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  7. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

  8. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  9. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题

    A. Broken Clock 题目连接: http://codeforces.com/contest/722/problem/A Description You are given a broken ...

随机推荐

  1. 网络_OSI模型_数据包传输

    2017年1月12日, 星期四 网络_OSI模型_数据包传输 1.  网络_源主机_局域网_交换机_路由器_目标主机 2. OSI7七层_TCP/IP精简 OSI 7层:       应用层     ...

  2. 把jQuery的类、插件封装成seajs的模块的方法

    这篇文章主要介绍了把jQuery的类.插件封装成seajs的模块的方法,需要的朋友可以参考下 注:本文使用的seajs版本是2.1.1 一.把Jquery封装成seajs的模块 define(func ...

  3. HDU 1176 免费馅饼 DP类似数塔题

    解题报告: 小明走在一条小路上,这条小路的长度是10米,从左到右依次是0到10一共十个点,现在天上会掉馅饼,给出馅饼掉落的坐标和时间,一开始小明的位置是在坐标为5的位置, 他每秒钟只能移动一米的距离, ...

  4. 通过JavaScript自由切换iframe

    我发现我有很大的强迫症,如果看到别人的文章没有最终的效果图,我会毫不犹豫关掉这个页面.真的很炸毛这种,让我有很不舒服的体验:所以纵使网上有类似的了,我还是写一篇给那些跟我有同样症状的人阅读. 首先来学 ...

  5. MySQL binlog导入失败

    一个同事问我,说他用innobackupex恢复数据后用mysqlbinlog导入增量数据时,发现数据没有导入进去并且也没有报错. mysqlbinlog /u01/mysql_py/database ...

  6. mysqldump只导出表结构或只导出数据的实现方法【转】

    mysql mysqldump 只导出表结构 不导出数据 mysqldump --opt -d 数据库名 -u root -p > xxx.sql 备份数据库 #mysqldump 数据库名 & ...

  7. scp拷贝文件报错-bash: scp: command not found

    今天用scp远程传输资料,报错如下: -bash: scp: command not found 在网上搜资料解决办法如下: 安装scp的软件包: # yum install openssh-clie ...

  8. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  9. (转载)mysql:设置mysql的远程访问

    1.登陆Mysqlmysql -u root -p2.允许任何IP访问,其中密码为admingrant all privileges on *.* to root@"%" iden ...

  10. react-native 报错

    报错信息: java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sur ...