[codeforces]C. Destroying Array——并查集

来源:互联网 发布:代号美洲豹 知乎 编辑:程序博客网 时间:2024/06/08 19:55

C. Destroying Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
41 3 2 53 4 1 2
output
5430
input
51 2 3 4 54 2 3 5 1
output
65510
input
85 5 4 4 6 6 5 55 2 8 7 1 3 4 6
output
18161188660
Note

Consider the first sample:

  1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
  2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
  3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
  4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.

利用并查集,从后面状态往前求解

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define PI acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;typedef  long long ll;typedef  unsigned long long  ull; inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}#define maxn 100005int a[maxn];int pos[maxn];int fa[maxn];bool ok[maxn];ll s[maxn];ll ans[maxn];int find(int u){    if(u!=fa[u])        fa[u] = find(fa[u]);    return fa[u];}int merge(int u, int v){    int fu = find(u),fv = find(v);    fa[fv] = fu;   //将原来的树挂靠到当前节点上    s[fu] += s[fv];  //将原来的根节点上的值加到当前根节点上}int main(){    //freopen("test.txt","r",stdin);    ios_base::sync_with_stdio(false);    cin.tie(0);    int n;    cin>>n;    for(int i = 1; i<=n; i++)    {        cin>>a[i];    }    for(int i = 1; i<=n; i++)    {        cin>>pos[i];        s[pos[i]] = a[pos[i]];        fa[i] = i;    }        ll rans = 0;    for(int i = n; i>=2; i--)    {        if(pos[i]<n && ok[pos[i]+1])//判断是否有右边的元素        {            merge(pos[i],pos[i]+1);        }        if(pos[i]>1 && ok[pos[i]-1])//判断是否有左边的元素        {            merge(pos[i],pos[i]-1);        }        rans = s[pos[i]];//其一定包含新开的数         ok[pos[i]] = true;        ans[i] = max(ans[i+1],rans);//判断其是否比他大     }    for(int i = 2; i<=n+1; i++)        cout << ans[i] << endl;;    return 0;}






0 0