题目:http://codeforces.com/problemset/problem/722/C
题意:每次在序列中删去一个数,求每次删去后序列中最大片段和。
思路:线段树合并
#include <bits/stdc++.h>
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn = 1e5+5;
struct Tree{
int mark;
LL ls, rs, ms;
}T[maxn<<2];
int n, x; LL a[maxn];
void pushup(int rt){
T[rt].ls = T[lson].ls;
if(!T[lson].mark) T[rt].ls += T[rson].ls;
T[rt].rs = T[rson].rs;
if(!T[rson].mark) T[rt].rs += T[lson].rs;
T[rt].ms = max(max(T[lson].ms, T[rson].ms), T[lson].rs + T[rson].ls);
if(T[lson].mark || T[rson].mark) T[rt].mark = 1;
}
void build(int rt, int l, int r){
T[rt].mark = 0;
if(l == r) {
T[rt].ls = T[rt].rs = T[rt].ms = a[l];
return ;
}
int mid = l+r >> 1;
build(lson, l, mid); build(rson, mid+1, r);
pushup(rt);
}
void update(int rt, int l, int r, int x){
if(l == r){
T[rt].ls = T[rt].rs = T[rt].ms = 0;
T[rt].mark = 1;
return ;
}
int mid = l+r >> 1;
if(x <= mid) update(lson, l, mid, x);
else update(rson, mid+1, r, x);
pushup(rt);
}
int main()
{
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
build(1, 1, n);
for(int i=1; i<=n; i++){
cin >> x;
update(1, 1, n, x);
cout << T[1].ms << endl;
}
}