并查集 + 乱搞
Codeforces 722C Destroying Array题解
题目描述
You are given an array consisting of n n non-negative integers $a_{1},a_{2},…,a_{n}$
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 1 to n 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$.
输入格式
The first line of the input contains a single integer n ( 1<=n<=100000 1<=n<=100000 ) — the length of the array.
The second line contains $ n $ integers $a_{1},a_{2},…,a_{n}$
The third line contains a permutation of integers from 1 to n — the order used to destroy elements.
输出格式
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.
INPUT & OUTPUT’s examples
Input’s e.g. #1
1 | 4 |
Output’s e.g. #1
1 | 5 |
分析
看题之后,应该想起来某JSOI2008星球大战上考过的类似思路吧。
就是倒着做,把毁灭改成创造(和平就是好)
我们每次从全是 X 的序列里创造一个数,然后求最大字段和。
很显然可以并查集,用并查集来维护字段和,然后用一个sum[i]
数组来维护以i为父节点的集合的和。
因为每添加一个点,某些字段就可能会扩展,所以我们在加入的时候把这个点的左右两端的点所在的集合合并,并且把他们的和加起来。
因为是倒着处理,那我们就倒着输出。因为最后一个答案是此数列全部元素的和,无意义,所以舍弃。注意最后输出0.
代码
1 | /* Headers */ |