[Disjoint-set of reverse thinking +] Codeforces Round 722C Destroying Array

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
Copy
4
1 3 2 5
3 4 1 2
Output
Copy
5
4
3
0
Input
Copy
5
1 2 3 4 5
4 2 3 5 1
Output
Copy
6
5
5
1
0
Input
Copy
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
Output
Copy
18
16
11
8
8
6
6
0
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.

Meaning of the questions: n to give you with a non-negative integer array, and gives a sequence so that they are removed, requiring each successive maximum intervals and that is how many
ideas: a simulation intended to embark on the question, really TLE , and a look at the sample found if the deleted sequence as it is easy to join sequence can be calculated continuum and is the largest number, and can be set to check and maintain the continuous intervals and
subject to the deletion order, and now we consider joining the order, the reverse reads the order
last no added elements, it is 0, the original deletion order to represent all delete finished
marked by adding elements
to find now the element's father
if the right of this element is added too, to see it belongs continuous collection, then this collection is currently set to join and check the
updated child (the father of the current element) father, and his father update continuum and
if this element is added to the left too, with the same reason
last check out this element belongs disjoint-set
answers on an answer and now compare and check the set, one of the largest election as the current answer

(Found written this title disjoint-set templates do not like the memory of so complicated ...)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=1e5+5;
 5 int n;
 6 ll sum[amn],ans[amn];
 7 bool used[amn];
 8 int pre[amn],a[amn],order[amn];
 9 void init(){
10     memset(used,0,sizeof used);
11     for(int i=1;i<=n;i++)pre[i]=i,sum[i]=a[i];  /// beginning thereof father themselves, and they are themselves continuum 
12 is  }
 13 is  int FD ( int I) {
 14      return I == pre [I] I:? Pre [I] = FD (pre [I]);
 15  }
 16  void the Add ( int FA, int CD) {
 . 17      pre [CD] = FA;              /// update father 
18 is      SUM [FA] + = SUM [CD];        /// update father and continuum 
. 19  }
 20 is  int main () {
 21 is      iOS :: sync_with_stdio ( 0 );
 22 is      CIN >> n-;
 23 is      a [0 ] = 0 ;
 24      for ( int I = . 1 ; I <= n-; I ++ ) {
 25          CIN >> A [I];
 26 is      }
 27      for ( int I = n-; I> = . 1 ; i-- ) {
 28          CIN >> order [I];           /// subject to deletion sequence, now consider the order of addition, the reverse order of reading 
29      }
 30      the init ();
 31 is      ANS [ 0 ] = 0 ;                    /// last no added elements, it is 0, in order to represent all of the original deleted deleted over 
32      int FA;
 33 is      for( Int I = . 1 ; I <n-; I ++ ) {
 34 is          Used [Order [I]] = . 1 ;        /// marker elements had added 
35          int OFA;                 /// Get Now the father element 
36          IF (Used [ Order [I] + . 1 ]) {    /// If this element is added to the right over, to see if it belongs to a set of continuous, then the current set of disjoint-set added 
37 [              OFA = FD (Order [I]) ;
 38 is              FA = FD (Order [I] + . 1 );
 39              the Add (FA, OFA);
 40          }
 41 is          IF (Used [Order [I] - . 1 ]) {    ///If this element is added left over, and the same token 
42 is              OFA = FD (Order [I]);
 43 is              FA = FD (Order [I] - . 1 );
 44 is              the Add (FA, OFA);
 45          }
 46 is          OFA FD = (Order [I]);        /// last look at the element belongs disjoint-set 
47          ANS [I] = max (ANS [I- . 1 ], SUM [OFA]);   /// comparison answer and now disjoint-set answer selected as the current answer to a maximum 
48      }
 49      for ( int I = N- . 1 ; I> = 0 ; i-- ) {
 50          the printf ( " % LLD \ n- " , ANS [ i]);
51      }
 52  }
 53  / * **
 54  give you a non-negative number having n to the array of integers, and so they are given a sequence to be deleted, and the requirements of each successive interval is the largest number that
 55  started by analog meaning of the questions, really TLE, and found the deleted sequence as if it is easy to join sequence can be calculated continuum and is the largest number, and can be set to check and maintain the continuum and
 56  subject to the deletion order, we now consider joining the order, so the reverse reading order
 57  last not joined elements, so to 0 in the original deletion order to represent all deleted over
 58  tag is added through the elements
 59  to find now the element's father
 60  If the right of this element is adding too, to see what it belongs to a continuous collection, then this collection is currently adding disjoint-set
 61  to update the child (father of the current element) father, and his father update continuum and
 62  if this element is added to the left too, and on the same token
 63  last look at this element belongs disjoint-set
 64  answers on an answer and now this relatively disjoint-set, and choose a maximum as the answer to the current
 65  ** * /

 

Guess you like

Origin www.cnblogs.com/brainm/p/11329356.html
Recommended