[codeforces722C]Destroying Array

试题描述

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 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 ≤ 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.

输出

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.

输入示例


输出示例


数据规模及约定

见“输入

题解

倒着做,用并查集。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 100010
#define LL long long
int val[maxn], ord[maxn];
LL ans[maxn]; int fa[maxn];
LL sum[maxn];
int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } int main() {
int n = read();
for(int i = 1; i <= n; i++) val[i] = read();
for(int i = 1; i <= n; i++) ord[i] = read(); for(int i = 1; i <= n; i++) sum[i] = val[i], fa[i] = 0;
LL mx = 0;
for(int i = n; i; i--) {
ans[i] = mx;
int u = ord[i];
fa[u] = u;
if(fa[u-1]) {
int v = findset(u - 1);
fa[v] = u; sum[u] += sum[v];
}
if(fa[u+1]) {
int v = findset(u + 1);
fa[v] = u; sum[u] += sum[v];
}
mx = max(mx, sum[u]);
} for(int i = 1; i <= n; i++) printf("%I64d\n", ans[i]); return 0;
}

[codeforces722C]Destroying Array的更多相关文章

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

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

  2. CodeForces722C Destroying Array【瞎搞】

    题意: 先给你一个序列,然后给你n个1-n的一个数,让你求前i个元素销毁的时候,区间字段和区间最大: 思路: 离线处理,维护新区间首尾位置的起点和终点,倒着处理: #include <bits/ ...

  3. Codeforces 722C. Destroying Array

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

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

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

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array

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

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  8. [CF722C] Destroying Array

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

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

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

随机推荐

  1. [已读]基于MVC的Javascript Web 富应用开发

    这本书是12年出版,我买的时间应该是13年,书架上唯一一本盗版→ → 但是看完是在今年. 因为刚拿到的时候,读起来很是磕磕绊绊,就搁置了蛮久.到第二次拿起来的时候,发现已经有部分内容过时,但我还是觉得 ...

  2. HDU 1220 B - Cube

    http://acm.hdu.edu.cn/showproblem.php?pid=1220 一开始的做法是,先暴力算出一个面,就是n * n的面,能有多少对.记作face 然后从上开始算下来,最上一 ...

  3. spark block读写流程分析

    之前分析了spark任务提交以及计算的流程,本文将分析在计算过程中数据的读写过程.我们知道:spark抽象出了RDD,在物理上RDD通常由多个Partition组成,一个partition对应一个bl ...

  4. spring mvc URL忽略大小写

    @Configuration public class SpringWebConfig extends WebMvcConfigurationSupport { @Override public vo ...

  5. Java长存!12个Java长久占居主要地位的原因

    Java长存!12个Java长久占居主要地位的原因 我们很容易就会遗忘那些曾经在猿群中大热而又被各种新技术掩盖直至堙灭的技术的价值.就拿COBOL这个老猿们当年所用的神器来说,就跟条死鱼一样被现代猿基 ...

  6. apache配置多域名

    环境:mac,其他环境也可做参考 hosts配置 eg:sudo vim /etc/hosts 127.0.0.1 www.testphalcon.com apache配置 找到apache对应安装目 ...

  7. chart 图片组件 生成后不能动态更新,需要销毁dom,从新载入 用 v-if 和 this.$nextTick(() => {

    <chart-box v-if="cbData1Bool" cb-text="基本概况" chartBoxSele="饼状图" :cb ...

  8. tabsGif

    tabsGif

  9. C-基础:关于预编译以及宏

    这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...

  10. 记一次Linux系统被入侵的过程

    记一次Linux系统被入侵的过程 1. 前期现象 前期现象,宋组那边反应开发环境192.161.14.98这台机器通过公网下载文件,很慢,ping百度丢包严重.因为这台机器是通过楼下adsl拨号上网, ...