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

Posted 惜取少年时

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于Codeforces Round #393 (Div. 2) E题Nikita and stack(线段树)解题报告的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #393 (Div. 2)

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

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card(示

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and st

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)