首页 > 其他 > 详细

[codeforces722C]Destroying Array

时间:2017-01-25 13:31:36      阅读:380      评论:0      收藏:0      [点我收藏+]

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

输入示例

8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6

输出示例

18
16
11
8
8
6
6
0

数据规模及约定

见“输入

题解

倒着做,用并查集。

#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

原文:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6349338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!