CF Destroying Array(容器的使用)

来源:互联网 发布:顾客旅程 绘图软件 编辑:程序博客网 时间:2024/06/05 06:22
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 integersa1, a2, ..., an.

You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from1 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 be0.

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 firsti 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 sum5 consists of one integer 5.
  2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum4 consists of two integers 1 3.
  3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum3 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 to0.


题意:给你一个数组,每次删除一个元素,求最大连续段值(不包括被删除的元素),直到所有都被删除了。


解题思路:个人觉得思路很简单,不过当时没用set容器的l自带ower_bound()函数,一直TLE在第十三个数据。用一个set   m存放所有段的和值,另外一个容器存放被删除的

位置。每次输入一个位置x,查询已经被删除位置的最前一个和最后面一个位置l、r,那么原来段(l,r)(  ()代表不包含边界的开区间)被分成了(l,x) 和 (x,r)这两个段,

那么就要将m内(l,r)删除,插入(l,x) 和 (x,r)。这里都是值这个段的值和。


底下有两个AC代码,一个是用set,不过得注意,中间结果有可能有多个相同的值,需要记录它,因为set一个元素只能出现一次。也可以用multiset,它可以有多个相同元素。


#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<set>#include<map>using namespace std;typedef long long ll;const int maxn=100010;ll a[maxn];set<ll> m;set<int> pos;map<ll,int> ma;int main(){    int n;    while(~scanf("%d",&n))    {        m.clear();        pos.clear();        ma.clear();        a[0]=0;        int x;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            a[i]=a[i-1]+x;        }        a[n+1]=a[n];        m.insert(a[n]);        ma[a[n]]++;        pos.insert(0); pos.insert(n+1);        set<int>::iterator it;        set<ll>::iterator itt;        for(int i=0;i<n;i++){            scanf("%d",&x);            int l,r;            ll mid;            it=pos.lower_bound(x);            r=*it; --it; l=*it;            pos.insert(x);            ///cout << r << " " << l << endl;            mid=a[r-1]-a[l];            if(ma[mid]>1) ma[mid]--;            else{                m.erase(mid);                ma[mid]--;            }            ///cout << a[r-1]-a[x] << " " << a[x-1]-a[l] << endl;            ///cout << mid << endl;            ma[a[r-1]-a[x]]++;            ma[a[x-1]-a[l]]++;            m.insert(a[r-1]-a[x]);            m.insert(a[x-1]-a[l]);            itt=m.end();            itt--;            printf("%I64d\n",*itt);        }    }    return 0;}



#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<set>#include<map>using namespace std;typedef long long ll;const int maxn=100010;ll a[maxn];multiset<ll> m; ///set<int> pos;int main(){    int n;    while(~scanf("%d",&n))    {        m.clear();        pos.clear();        a[0]=0;        int x;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            a[i]=a[i-1]+x;        }        a[n+1]=a[n];        m.insert(a[n]);        pos.insert(0); pos.insert(n+1);        set<int>::iterator it;        for(int i=0;i<n;i++){            scanf("%d",&x);            int l,r;            ll mid;            it=pos.lower_bound(x);  /// 如果用 it=lower_bound(pos.begin(), pos.end(), x) 这来查询会超时,因为set自带的lower_bound()比较快。            r=*it; --it; l=*it;            pos.insert(x);            ///cout << r << " " << l << endl;            mid=a[r-1]-a[l];            ///multiset<ll>::iterator itt;            ///for(itt=m.begin(); itt!=m.end(); itt++)            ///    cout << *itt << " ";            ///cout << endl;            m.erase(m.lower_bound(mid));            ///cout << mid << endl;            ///cout << a[r-1]-a[x] << " " << a[x-1]-a[l] << endl;            ///cout << mid << endl;            m.insert(a[r-1]-a[x]);            m.insert(a[x-1]-a[l]);            printf("%lld\n",*m.rbegin());        }    }    return 0;}



0 0
原创粉丝点击