Codeforces Round #393 (Div. 2) E题Nikita and stack(线段树)解题报告

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.

Example

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.

 

解题思路:

将pop操作认为是-1,push操作认为是+1。每次从第n个操作的值向左求和,第一个出现的和大于0的位置即为最后在栈顶的元素。对于此的维护采用线段树。

参考代码:

 1 #include<bits/stdc++.h>
 2 #include <iostream>
 3 #include <queue>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 const int MAX=1e5+5;
11 int m,mm;
12 int a[MAX];
13 int lo,opt,num;
14 struct he
15 {
16     int re;
17     int sub;
18     void update(int d)
19     {
20         re+=d;
21         sub+=d;
22     }
23 }fe[1<<19];
24 void pushdown(int k)
25 {
26     if(fe[k].sub!=0)
27     {
28         fe[2*k].update(fe[k].sub);
29         fe[2*k+1].update(fe[k].sub);
30         fe[k].sub=0;
31     }
32 }
33 void update(int l,int r,int a,int b,int k,int v)
34 {
35     if(a<=l&&b>=r)
36     {
37         fe[k].update(v);
38         return;
39     }
40     if(b<=l||a>=r)
41         return ;
42     pushdown(k);
43     int mid=(l+r)/2;
44     update(l,mid,a,b,2*k,v);
45     update(mid,r,a,b,2*k+1,v);
46     fe[k].re=max(fe[2*k].re,fe[2*k+1].re);
47     return;
48 }
49 int query(int l,int r,int k)
50 {
51     if(fe[k].re<=0)
52         return 0;
53     if(r-l<=1)
54         return l;
55     pushdown(k);
56     int mid=(l+r)/2;
57     if(fe[2*k+1].re>0)
58         return query(mid,r,2*k+1);
59     else
60         return query(l,mid,2*k);
61 }
62 int main()
63 {
64     scanf("%d",&m);
65     mm=m;
66     a[0]=-1;
67     while(mm--)
68     {
69         scanf("%d %d",&lo,&opt);
70         if(opt)
71         {
72             scanf("%d",&num);
73             a[lo]=num;
74             update(1,m+1,1,lo+1,1,1);
75         }
76         else
77         {
78             update(1,m+1,1,lo+1,1,-1);
79         }
80         printf("%d\n",a[query(1,m+1,1)]);
81     }
82 }

 

复习时重写的代码

 1 #include <iostream>
 2 #include<bits/stdc++.h>
 3 #include <stack>
 4 #include <queue>
 5 #include <map>
 6 #include <set>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned long long ull;
14 const int MAX=1e5+5;
15 int m;
16 int sum[MAX<<2],lazy[MAX<<2];
17 int an[MAX];
18 int num,opt,tem;
19 void pushdown(int k)
20 {
21     if(lazy[k])
22     {
23         sum[2*k]+=lazy[k];
24         lazy[2*k]+=lazy[k];
25         sum[2*k+1]+=lazy[k];
26         lazy[2*k+1]+=lazy[k];
27         lazy[k]=0;
28     }
29 }
30 void update(int ul,int ur,int l,int r,int k,int val)
31 {
32     if(l>=ul&&r<=ur)
33     {
34         sum[k]+=val;
35         lazy[k]+=val;
36         return;
37     }
38     int mid=l+r>>1;
39     if(l==r)
40         return;
41     pushdown(k);
42     if(ul<=mid)
43         update(ul,ur,l,mid,2*k,val);
44     if(ur>mid)
45         update(ul,ur,mid+1,r,2*k+1,val);
46     sum[k]=max(sum[2*k],sum[2*k+1]);
47 }
48 int query(int l,int r,int k)
49 {
50     if(l==r)
51         return (sum[k]>0?an[l]:-1);
52     int mid=l+r>>1;
53     pushdown(k);
54     if(sum[2*k+1]>0)
55         return query(mid+1,r,2*k+1);
56     if(sum[2*k]>0)
57         return query(l,mid,2*k);
58     return -1;
59 }
60 int main()
61 {
62     scanf("%d",&m);
63     for(int i=0;i<m;i++)
64     {
65         scanf("%d %d",&num,&opt);
66         if(opt)
67         {
68             scanf("%d",&tem);
69             an[num]=tem;
70             update(1,num,1,m,1,1);
71         }
72         else
73             update(1,num,1,m,1,-1);
74         printf("%d\n",query(1,m,1));
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/quintessence/p/6392073.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值