//http://codeforces.com/problemset/problem/722/C
//逆序离线
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=1e5+5;
int N,A[MAX],O[MAX],pre[MAX];
ll sum[MAX],Ans[MAX];
bool Used[MAX]={false};
int Find(int x)
{
return x==pre[x]?x:pre[x]=Find(pre[x]);
}
void mix(int x,int y)
{
int fx=Find(x),fy=Find(y);
if (fx!=fy)
pre[fy]=fx,sum[fx]+=sum[fy];
}
int main()
{
cin.sync_with_stdio(false);
cin>>N;
for (int i=1;i<=N;i++)
cin>>A[i];
for (int i=1;i<=N;i++)
cin>>O[i];
for (int i=1;i<=N;i++)
pre[i]=i,sum[i]=A[i];
ll temp=0;
for (int i=N;i>=1;i--)
{
Used[O[i]]=true,Ans[i]=temp;
if (O[i]!=1&&Used[O[i]-1])
mix(O[i],O[i]-1);
if (O[i]!=N&&Used[O[i]+1])
mix(O[i],O[i]+1);
temp=max(temp,sum[Find(O[i])]);
}
for (int i=1;i<=N;i++)
cout<<Ans[i]<<endl;
return 0;
}
CodeForces 722C DestroyingArray 并查集+思维题
最新推荐文章于 2023-03-10 10:38:49 发布