time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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:

Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.

Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.

First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.

Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.

【题解】



线段树区间合并

每次删掉一个节点;

类似hotel那题,不同的是变成求连续的块的最大和。

可以在求连续块的基础上做一些加法运算求和。

每次操作完更新完之后输出mlx[1]即可(根节点代表整个区间);

#include <cstdio>
#include <algorithm> using namespace std; const int MAXN = 109000; int n, m;
int llianxusum[MAXN<<2], rlianxusum[MAXN<<2];
long long mllx[MAXN<<2], mrlx[MAXN<<2], mlx[MAXN<<2]; void build(int l, int r, int rt)
{
llianxusum[rt] = rlianxusum[rt] = r - l + 1;
if (l == r)
{
scanf("%I64d", &mlx[rt]);
mllx[rt] = mrlx[rt] = mlx[rt];
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
mlx[rt] = mllx[rt] = mrlx[rt] = mlx[rt << 1] + mlx[rt << 1 | 1];
} void push_up(int rt, int len)
{
llianxusum[rt] = llianxusum[rt << 1];
mllx[rt] = mllx[rt << 1];
if (llianxusum[rt] == len - (len >> 1))
{
llianxusum[rt] += llianxusum[rt << 1 | 1];
mllx[rt] += mllx[rt << 1 | 1];//连续的序列和也可以递增
}
rlianxusum[rt] = rlianxusum[rt << 1 | 1];
mrlx[rt] = mrlx[rt << 1 | 1];
if (rlianxusum[rt] == len >> 1)
{
rlianxusum[rt] += rlianxusum[rt << 1];
mrlx[rt] += mrlx[rt << 1];//同理
}
mlx[rt] = max(mlx[rt << 1], mlx[rt << 1 | 1]);
mlx[rt] = max(mlx[rt], mrlx[rt << 1] + mllx[rt << 1 | 1]);
//整个区间的连续空格子数就没必要记录了。
//直接整个区间的最大和就好。
} void updata(int pos, int begin, int end, int rt)
{
if (begin == end)
{
llianxusum[rt] = rlianxusum[rt] = 0;
mlx[rt] = mllx[rt] = mrlx[rt] = 0;
return;
}
int m = (begin + end) >> 1;
if (pos <= m)
updata(pos, begin, m, rt << 1);
else
updata(pos, m + 1, end, rt << 1 | 1);
push_up(rt, end - begin + 1);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
//freopen("F:\\rush_out.txt", "w", stdout);
scanf("%d", &n);
build(1, n, 1);
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
updata(x, 1, n, 1);
printf("%I64d\n", mlx[1]);
}
return 0;
}

【37.38%】【codeforces 722C】Destroying Array的更多相关文章

  1. 【Codeforces 722C】Destroying Array (数据结构、set)

    题意 输入一个含有 n(1≤n≤100000) 个非负整数的 a 数组和一个 1-n 的排列 p 数组,求每次删除 a[p[i]] 后,最大连续子段和(不能跨越被删除的)是多少? 分析 因为都是非负整 ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【38.02%】【codeforces 625B】War of the Corporations

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  8. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  9. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

随机推荐

  1. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  2. Tuple<int, int> Dictionary<string, object>妙用

    Tuple<int, int> Dictionary<string, object>妙用

  3. Python操作MySQL数据库完成简易的增删改查功能

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...

  4. Git安装及密钥的生成并上传本地文件到GitHub上

    之前用的GitHub,不太熟练,一直在上传的过程中遇到了一些问题,看了网上诸多教程,总觉得很乱,特参考一些资料,总结了一篇完整的操作步骤,从下载安装到上传文件,亲测有效 1.下载Git软件:https ...

  5. Shiro学习总结(4)——Shrio登陆验证实例详细解读

    最终效果如下: 工程整体的目录如下: Java代码如下: 配置文件如下: 页面资源如下: 好了,下面来简单说下过程吧! 准备工作: 先建表: [sql] view plain copy drop ta ...

  6. 又一次认识java(一) ---- 万物皆对象

    假设你现实中没有对象.至少你在java世界里会有茫茫多的对象,听起来是不是非常激动呢? 对象,引用,类与现实世界 现实世界里有许很多多的生物,非生物,跑的跳的飞的,过去的如今的未来的,令人眼花缭乱.我 ...

  7. Android Unable to execute dex: method ID not in [0, 0xffff]: 65536 问题解决方法

    开始一个新项目的时候,Build工程的时候一直报这个错误: 控制台报错误:Conversion to Dalvik format failed: Unable to execute dex: meth ...

  8. Node.js笔记 http fs

    const http=require('http'); const fs=require('fs'); var server = http.createServer(function(req, res ...

  9. 【例题 7-10 UVA - 11212】Editing a Book

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜. 很容易想到,最多只要搜8层就可以得到答案了 ->最多8下肯定可以还原. 则枚举一下最大层数.然后做个深搜就好. ...

  10. Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)

    题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...