【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack

就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁。

用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1。相当于寻找最靠右的和>0的后缀。

也就是线段树区间加减,寻找最靠右侧的大于0的数的位置,记录个区间最大值就行了。

另,不知怎么回事,交到cf上RE1……

C. Nikita and stack
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer x on the top of the stack, and operation pop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operation pop() does nothing.

Nikita made m operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on the i-th step he remembers an operation he made pi-th. In other words, he remembers the operations in order of some permutation p1, p2, ..., pm. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him!

Input

The first line contains the integer m (1 ≤ m ≤ 105) — the number of operations Nikita made.

The next m lines contain the operations Nikita remembers. The i-th line starts with two integers pi and ti (1 ≤ pi ≤ mti = 0 or ti = 1) — the index of operation he remembers on the step i, and the type of the operation. ti equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer xi (1 ≤ xi ≤ 106) — the integer added to the stack.

It is guaranteed that each integer from 1 to m is present exactly once among integers pi.

Output

Print m integers. The integer i should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to i. If the stack is empty after performing all these operations, print -1.

Examples
input
2
2 1 2
1 0
output
2
2
input
3
1 1 2
2 1 3
3 0
output
2
3
2
input
5
5 0
4 0
3 1 1
2 1 1
1 1 2
output
-1
-1
-1
-1
2
Note

In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same.

In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed.

In the third example Nikita remembers the operations in the reversed order.

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100010
int n,a[N],maxv[N<<2],delta[N<<2];
void pushdown(int rt)
{
	if(delta[rt])
	  {
	  	delta[rt<<1]+=delta[rt];
	  	delta[rt<<1|1]+=delta[rt];
	  	maxv[rt<<1]+=delta[rt];
	  	maxv[rt<<1|1]+=delta[rt];
	  	delta[rt]=0;
	  }
}
void update(int ql,int qr,int v,int rt,int l,int r)
{
	if(ql<=l&&r<=qr)
	  {
	  	maxv[rt]+=v;
	  	delta[rt]+=v;
	  	return;
	  }
	int m=(l+r>>1);
	pushdown(rt);
	if(ql<=m) update(ql,qr,v,rt<<1,l,m);
	if(m<qr) update(ql,qr,v,rt<<1|1,m+1,r);
	maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
int Findp(int rt,int l,int r)
{
	if(l==r) return l;
	int m=(l+r>>1);
	pushdown(rt);
	if(maxv[rt<<1|1]>0) return Findp(rt<<1|1,m+1,r);
	if(maxv[rt<<1]>0) return Findp(rt<<1,l,m);
	return 0;
}
int main()
{
	//freopen("c.in","r",stdin);
	scanf("%d",&n);
	int x,y;
	bool op;
	a[0]=-1;
	for(int i=1;i<=n;++i)
	  {
	  	scanf("%d%d",&x,&op);
	  	if(op)
	  	  {
	  	  	scanf("%d",&y);
	  	  	a[x]=y;
	  	  	update(1,x,1,1,1,n);
	  	  }
	  	else
	  	  update(1,x,-1,1,1,n);
	  	printf("%d\n",a[Findp(1,1,n)]);
	  }
	return 0;
}

转载于:https://www.cnblogs.com/autsky-jadek/p/6343485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值