/*
题目描述:给出n个数的序列a[1] , a[2] , ... ,a[n] ,每次将其中的一个数破坏掉,每一次破坏掉以后问连续数
的最大值是多少
方法:利用线段树维护,线段树的每一个节点(l , r , x)包含4个信息:(l , r)中的总和,(l , r)中的最大值,(l , r)中最大前缀,
(l , r)中最大后缀,每次修改后输出t[1].summax(1节点的最大值)即可,每次更新的pushup具体操作见代码
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const LL INF = 1e15 + 5 ;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 1e5 + 5 ;
struct node
{
LL sum , summax , lmax , rmax ;
}t[4 * maxn];
LL a[maxn] ;
int op[maxn] ;
LL Max(const LL a , const LL b)
{
return a > b ? a : b ;
}
void pushup(int l , int r , int x)
{
t[x].sum = t[x<<1].sum + t[x << 1 | 1].sum ; //总和等于左右两节点总和
t[x].summax = Max(t[x<<1].summax , Max(t[x<<1|1].summax , t[x<<1].rmax + t[x <<1 | 1].lmax) ) ; //最大值的三种可能:左子节点最大值,右子节点最大值,左子节点最大后缀+右子节点最大前缀
t[x].lmax = Max(t[x<<1].sum + t[x<<1|1].lmax , t[x<<1].lmax ) ; //最大前缀二种可能:左子节点最大前缀 , 左子节点总和+右子节点最大前缀
t[x].rmax = Max( t[x<<1].rmax + t[x<<1|1].sum , t[x<<1|1].rmax ) ; //最大后缀二种可能:右子节点最大后缀 , 右子节点总和+左子节点最大后缀
if(t[x].sum < 0) t[x].sum = -INF; //<0就置于-INF是因为害怕-INF加多了会爆LL型
if(t[x].summax < 0) t[x].summax = -INF;
if(t[x].lmax < 0) t[x].lmax = -INF;
if(t[x].rmax < 0) t[x].rmax = -INF;
}
void build(int l , int r , int x)
{
if(l == r){
t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = a[l] ;
return ;
}
int m = l + (r - l ) / 2 ;
build(lson);
build(rson);
pushup(l , r , x);
}
void modify(int pos , LL val , int l , int r , int x)
{
if(pos==l && l == r){
t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = val ;
return ;
}
int m = l + (r - l) / 2;
if(pos <= m)
modify(pos , val , lson);
else
modify(pos , val , rson);
pushup(l , r , x) ;
}
int main()
{
int n ;
scanf("%d",&n);
for(int i = 1 ; i<= n ; i++)
scanf("%lld",&a[i]);
for(int i = 1 ; i<= n ; i++)
scanf("%d",&op[i]);
build(1 , n , 1 );
for(int i = 1 ; i <= n ; i++){
modify( op[i] , -INF , 1 , n , 1 ) ;
LL ans = t[1].summax ;
if(ans > 0)
printf("%lld\n",ans);
else
printf("0\n");
}
return 0;
}
codeforces Destroying Array 线段树维护区间
最新推荐文章于 2020-10-22 23:18:26 发布