ACM暑期集训24

G - Okabe and Boxes

 

Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.

Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.

That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.

Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.

Input

The first line of input contains the integer n (1 ≤ n ≤ 3·105) — the number of boxes.

Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1 ≤ x ≤ n) follows, indicating that Daru should add the box with number x to the top of the stack.

It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed.

Output

Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.

Examples

Input

3
add 1
remove
add 2
add 3
remove
remove

Output

1

Input

7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

Output

2

分析:

用vector模拟,如果数组最后一个元素不是第i个remove,那么就肯定要排序依次,此时就可以把数组清空,因为如果数组还有其他元素,那么其一定比i大,排完序一定在栈底,那么下次要出来也要排序,如果相等就可以直接把这个元素弹出

#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;

vector<int > q;
char s[10];

int main()
{
	int n;
	int cnt=1,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=2*n;i++)
	{
		scanf("%s",s);
		if(s[0]=='a')
		{
			int a;
			scanf("%d",&a);
			q.push_back(a);
		}
		else {
			if(q.empty()) ;
			else if (q.back()==cnt) q.pop_back();
			else ans++,q.clear();
			cnt++;
		}
	}
	printf("%d\n",ans);
	return 0;
}

H - Destroying Array

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

4
1 3 2 5
3 4 1 2

Output

5
4
3
0

Input

5
1 2 3 4 5
4 2 3 5 1

Output

6
5
5
1
0

Input

8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6

Output

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 5consists 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<stdio.h>
#include<string>
#include<map>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;

long long a[100010],ans[100010],sum[100010];
int n,vis[100010],fa[100010],b[100010];

int fin(int i)
{
	if(fa[i]==i) return i;
	else return fa[i]=fin(fa[i]);
}

int main()
{
	scanf("%d",&n);
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	for(int i=0;i<=n;i++) fa[i]=i;
	for(int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	ans[n]=0;
	for(int i=n;i>0;i--)
	{
		int an=b[i];
		vis[an]=1;
		sum[an]=a[an];
		if(vis[ an+1 ]) {
			int x=fin(an),y=fin(an+1);
			fa[x]=y;
			sum[y]+=sum[x];
		}
		if( vis[ an-1 ])
		{
			int x=fin(an),y=fin(an-1);
			fa[x]=y;
			sum[y]+=sum[x];
		}
		ans[i-1]=max(ans[i],sum[fin(an)]);
	}
	for(int i=1;i<=n;i++)
		printf("%I64d\n",ans[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41383801/article/details/81778103