【37.38%】【codeforces 722C】Destroying Array
来源:互联网 发布:附加遗产网络剧百度云 编辑:程序博客网 时间:2024/05/17 00:55
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
- codeforces 722C Destroying Array
- codeforces(722c)-Destroying Array
- Codeforces 722C. Destroying Array
- codeforces 722C. Destroying Array
- codeforces 722C Destroying Array 【线段树好题】
- codeforces 722C. Destroying Array map+multiset
- Codeforces 722C - Destroying Array By Assassin
- CodeForces - 722C Destroying Array (set)
- Codeforces #722C: Destroying Array 题解
- codeforces 722C Destroying Array 并查集带权
- codeforces-772C-Destroying Array
- Codeforces Round 722C:Destroying Array(离线)
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- codeforces 722C. Destroying Array(并查集||set)
- CodeForces 722C Destroying Array(数组删除区间最大值)
- CodeForces 722C. Destroying Array(逆向思维)
- 【Codeforces 722 C Destroying Array】+ 并查集
- 区块链开发技术路线的思考(二)
- Activeandroid对数据库的操作
- Android中shape使用详解
- 全军尽墨的Android应用:社会化授权登录及分享安全漏洞
- DRBD 及 DRBD配置
- 【37.38%】【codeforces 722C】Destroying Array
- 台媒猜大陆歼20明年初服役:首批列装数量或达两位数
- 价值1400美元的CEH(道德黑客)认证培训课程长啥样?(3)工具集
- How to use Facebook iOS SDK on iOS 10
- 类和对象的基础6——类的作用域和生存周期
- 【leetcode】4. Median of Two Sorted Arrays
- 数字排列组合
- 稀疏矩阵快速转置算法理解
- python中classmethod和staticmethod的区别