原题链接:https://vjudge.net/problem/511814/origin

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.

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.

题意 :给你一串数组以及一串指令(每次破坏数组的第几个元素),输出每个指令后剩下的连通块的和最大是多少。

分析:有两种方法做,第一种用集合的插入和删除(强烈建议);第二种用并查集:题目给的操作数从第 1 个到第 N 个数是删除原数组中的一个数, 那么反过来从后往前就是增加原数组中的一个数, 每增加一个值, 那么就有四种情况: 第一种和前后都不连续, 即自成一个集合; 第二种:和前面的数连续, 和前一个数在一个集合; 第三种和之后一个数连续, 和之后的一个数在一个集合; 第四种既和前面一个数连续也和后面一个数连续,那么说明前后两个集合被这个数合并成一个集合. 然后合并的时候维护每个集合的元素和 sum, 利用 max 记录当前集合 sum 和新增集合的 sum 中的最大值.

AC代码如下:

①使用集合插入和删除连通块的和。

#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define _for(i,a,b) for(int i = a;i < b;i++)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define all(s) s.begin(), s.end() const int maxn = 100050;
int n,x;
ll a[maxn];
set<int>pos;
set<ll>sum;
map<ll, int>nsum; int main()
{
pos.clear();
sum.clear();
nsum.clear();
scanf("%d", &n);
a[0] = 0;
rep(i, 1, n)
{
scanf("%d", &x);
a[i] = a[i - 1] + x;
}
a[n + 1] = a[n];
sum.insert(a[n]);
nsum[a[n]]++;
pos.insert(0), pos.insert(n + 1);
set<int>::iterator itt;
set<ll>::iterator it;
int r, l;
while (n--)
{
scanf("%d", &x);
itt = pos.lower_bound(x);
r = *itt; --itt; l = *itt;
pos.insert(x);
ll len = a[r - 1] - a[l];
if (nsum[len] > 1)nsum[len]--;
else
{
nsum[len]--;
sum.erase(len);
}
nsum[a[r - 1] - a[x]]++;
nsum[a[x - 1] - a[l]]++;
sum.insert(a[r - 1] - a[x]);
sum.insert(a[x - 1] - a[l]);
it = sum.end();
it--;
printf("%I64d\n", *it);
}
return 0;
}

②使用并查集:

#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define _for(i,a,b) for(int i = a;i < b;i++)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define all(s) s.begin(), s.end() const int maxn = 1e5 + 10;
int n;
ll a[maxn], cmd[maxn], sum[maxn], par[maxn],ans[maxn];
bool vis[maxn]; int find_(ll x)
{
return (par[x] == x) ? x : par[x] = find_(par[x]);
}
void unite(ll x,ll y)
{
ll fx = find_(x);
ll fy = find_(y);
if (fx != fy) { par[fy] = fx; sum[fx] += sum[fy]; }//两连通块和相加
} int main()
{
scanf("%d", &n);
rep(i, 1, n)
{
scanf("%lld", &a[i]);
par[i] = i; vis[i] = 0;
}
rep(i, 1, n)scanf("%lld", &cmd[i]);
ll rans = 0;
for (int i = n; i >= 1; i--)
{
ll t = cmd[i];
sum[t] = a[cmd[i]], vis[t] = 1, ans[i] = rans;
//新加入一个元素后的四种情况
if (t != 1 && vis[t - 1])unite(t, t - 1);
if (t != n && vis[t + 1])unite(t, t + 1);
rans = max(rans, sum[find_(t)]);
}
rep(i, 1, n)printf("%lld\n", ans[i]);
return 0;
}

CodeForces - 722C Destroying Array (并查集/集合的插入和删除)的更多相关文章

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

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

  2. Codeforces 722C. Destroying Array

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

  3. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

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

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

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

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

  6. [并查集+逆向思维]Codeforces Round 722C Destroying Array

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

  7. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  8. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  9. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

随机推荐

  1. Beta阶段代码与规范

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta冲刺 这个作业的目标 团队进行Beta冲刺--代码规范与计划 作业正文 如下 其他参考文献 ... ...

  2. 关于HTTP 请求方式: GET和POST的比较的本质

    什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议. HTTP在客户端和服务器之间以request ...

  3. Android Studio出现:Cause: unable to find valid certification path to requested target问题解决

    Android Studio , Flutter , IDEA 工程报错 unable to find valid certification path to requested target 最新解 ...

  4. SSH网上商城一

    Java高级项目之SSH网上商城项目实战: 1.采用目前最主流的三大框架开发即Struts2+Spring+Hibernate框架整合开发.2.通过AJAX技术提供良好的用户体验.3.提供了邮箱激活的 ...

  5. 题解 - 【NOI2015】维修数列

    题面大意: 使用平衡树维护一个数列,支持插入,修改,删除,翻转,求和,求最大和这 \(6\) 个操作. 题意分析: Splay 裸题,几乎各种操作都有了,这个代码就发给大家当个模板吧. 最后求最大和的 ...

  6. docx.opc.exceptions.PackageNotFoundError: Package not found at '文件名.docx' 问题解决

    编译源程序时,提示:docx.opc.exceptions.PackageNotFoundError: Package not found at '文件名.docx' . 源文件明明存在啊,难道是用的 ...

  7. 洛谷 CF1012C Hills (动态规划)

    题目大意:有n个山丘 , 可以在山丘上建房子 , 建房子的要求是 : 该山丘的左右山丘严格的矮于该山丘 (如果有的话),你有一架挖掘机,每单位时间可以给一个山丘挖一个单位的高度,问你想要建造 1,2, ...

  8. 我用shell写了个mud游戏:武林群侠传

    零.前言 学习shell的时候,无聊的我,写了个简单版的文字mud,暂且叫武林群侠传吧.可能90后都不知道文字mud是什么了--哈哈 壹.效果 先看下效果吧,GIF图如下 文字效果如下: [root@ ...

  9. SpringBoot中Service实现类添加@Service却任然无法注入的问题

    最近一直在研究Spring Boot.从GitHub上下载了一个my-Blog源码,一边看,一边自己尝试去实现,结果掉在坑了,研究了近一周才爬出来,特地来这博客园记录下来,一是避免自己在放这样的错误, ...

  10. 前端走进机器学习生态,在 Node.js 中使用 Python

    这次给大家带来一个好东西,它的主要用途就是能让大家在 Node.js 中使用 Python 的接口和函数.可能你看到这里会好奇,会疑惑,会不解,我 Node.js 大法那么好,干嘛要用 Python ...