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 thei-th step he remembers an operation he made pi-th. In other words, he remembers the operations in order of some permutationp1, 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 is2. 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.

分析:对于已知操作,答案为最右的使得之后push次数=pop次数的push的位置;

   若push=1,pop=-1,即求最右的后缀和为1的位置;

   所以线段树区间修改+最值查询;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,val[maxn];
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
struct Node
{
int Max, lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].Max = max(T[rt<<].Max, T[rt<<|].Max);
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
int t = T[rt].lazy;
T[rt<<].Max += t;
T[rt<<|].Max += t;
T[rt<<].lazy +=t;
T[rt<<|].lazy += t;
T[rt].lazy = ;
} void Update(int l, int r, int v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy += v;
T[rt].Max += v;
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
} int Query(int l, int r, int rt)
{
if(l==r)
{
return T[rt].Max >? val[l]:-;
}
int mid = (l + r) >> ;
if(T[rt].lazy) PushDown(l, r, rt);
if(T[rt<<|].Max>) return Query(mid+, r ,rt<<|);
else return Query(l, mid, rt<<);
} int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)
{
scanf("%d%d",&j,&k);
if(k==)
{
Update(,j,-,,n,);
}
else
{
scanf("%d",&k);
val[j]=k;
Update(,j,,,n,);
}
printf("%d\n",Query(,n,));
}
return ;
}

Nikita and stack的更多相关文章

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

    就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...

  2. Codeforces 756C Nikita and stack

    Codeforces 756C Nikita and stack 题目大意: 给定一个对栈进行操作的操作序列,初始时序列上没有任何操作,每一次将一个本来没有操作的位置变为某一操作(push(x),po ...

  3. Nikita and stack CodeForces - 756C (栈,线段树二分)

    大意: 给定m个栈操作push(x)或pop(), 栈空时pop()无作用, 每个操作有执行的时间$t$, 对于每个$0 \le i \le m$, 输出[1,i]的栈操作按时间顺序执行后栈顶元素. ...

  4. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  5. Codeforces Round #393 (Div. 2)

    A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  6. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  7. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  8. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  9. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

随机推荐

  1. list.subList

    import java.util.ArrayList;import java.util.List; public class Test2 {    public static void main(St ...

  2. @Transaction 无效

    上班的时候碰到这个问题,看了一些博客写的,都试了一遍解决方案,发现结果还是不行, 最后突然发现我的配置顺序和网上的有些许不同,就改了下,发现成功了,特此打桩纪念一下. 一.先说一下基本用法: 1. @ ...

  3. MySQL:常见错误01

    ylbtech-MySQL:常见错误01 1.返回顶部 1. [SQL]select * from product_product_tag aLEFT JOIN system_tag b on b.i ...

  4. c++之——————各种变量

    对我们程序员来讲,“变量”和“对象”是可以相互互换使用的.-------------开篇之词. 变量:提供一个具有名字的可供程序操作的存储空间.由类型说明符和其后紧跟的数个列表组成,其中变量名之间使用 ...

  5. bzoj1143(2718)[CTSC2008]祭祀river(最长反链)

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2781  Solved: 1420[Submit][S ...

  6. Unity引擎GUI之Button

    UGUI Button,可以说是真正的使用最广泛.功能最全面.几乎涵盖任何模块无所不用无所不能的组件,掌握了它的灵巧使用,你就几乎掌握了大半个UGUI! 一.Button组件: Interactabl ...

  7. ie8及其以下版本兼容性问题之input file隐藏上传文件

    文件上传时,默认的file标签很难看,而且每个浏览器下都有很大差距.因此我们基本都把真正的file标签给隐藏,然后创建一个标签来替代它.但是由于IE出于安全方面的考虑上传文件时必须点击file的浏览按 ...

  8. 配置 Sybase数据源

    1.Start-- > All Programs -- > Sybase - -> Connectivity --> click ‘Open Client Directory ...

  9. 安装pywinauto的步骤

    team准备搞自动化测试(桌面WPF系统),这几天一直在找自动化测试工具.发现了pywinauto这款工具,许多网友反应很好用,于是下载下来试用.不得不说遇到的坑真不少,记录下来以备不时之需. 前段时 ...

  10. Caffe2:ubuntuKylin17.04使用Caffe2.LSTM

    一早发现caffe2的较成熟的release版发布了(the first production-ready release),那么深度学习平台在之后一段时间也是会出现其与tensorflow相互竞争的 ...