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 ;
}

最新文章

  1. H5程序员如何利用cordova开发跨平台应用
  2. Sqlserver 语法总结
  3. [字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案
  4. 使用elk+redis搭建nginx日志分析平台
  5. .net自动生成数据库表的类
  6. nginx 设置 fastcgi缓存
  7. nginx(三)初步搭建nginx虚拟主机
  8. 阿里RDS备份恢复
  9. 【Python笔记】图片处理库PIL的源代码安装步骤
  10. windows访问linux共享
  11. ubuntu12.04下搭建ftpserver
  12. 【MYSQL】SQL 的join 区别
  13. asp.net core使用Swashbuckle.AspNetCore(swagger)生成接口文档
  14. strcpy和strcat易忽略点
  15. windows驱动程序wdf--KMDF获取应用程序数据缓冲区地址
  16. Mybatis 批量添加,批量更新
  17. 一起学HBase——简单介绍HBase各种组件
  18. GUI学习之八——QToolButton的学习总结
  19. HPU组队赛B:问题(二进制枚举)
  20. Android学习之——自己搭建Http框架(1)

热门文章

  1. CSS3:box-sizing:不再为盒子模型而烦恼
  2. jxl 导入导出Excel(有模板)
  3. vim copy termi
  4. bzoj4737: 组合数问题
  5. bzoj 1191 [ HNOI 2006 ] 超级英雄Hero —— 二分图匹配
  6. leetcode数组相关
  7. hdu5673-Robot
  8. bzoj 1503郁闷的出纳员(splay)
  9. EasyUI DataGrid组织事件冒泡
  10. 一个对象toString()方法如果没有被重写,那么默认调用它的父类Object的toString()方法,而Object的toString()方法是打印该对象的hashCode,一般hashCode就是此对象的内存地址