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

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

 

 

push看做1, pop看做-1, 线段维护后缀和, 栈顶即为第一个后缀和>0的位置

#include <iostream>
#include <algorithm>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
using namespace std;

const int N = 1e5+10, INF = 0x3f3f3f3f;
int m, ans, suf, a[N];
int s[N<<2], mx[N<<2];

void update(int o, int l, int r, int pos, int v) {
	if (l==r) return s[o]=mx[o]=v,void();
	if (mid>=pos) update(ls,pos,v);
	else update(rs,pos,v);
	s[o] = s[lc]+s[rc];
	mx[o] = max(mx[lc]+s[rc],mx[rc]);
}

void query(int o, int l, int r) {
	if (ans>0) return;
	if (suf+mx[o]<=0) suf += s[o];
	else if (l==r) ans = a[l];
	else query(rs), query(ls);
}

int main() {
	scanf("%d", &m);
	REP(i,1,m) {
		int op, t, x;
		scanf("%d%d", &t, &op);
		if (op) {
			scanf("%d", &x);
			a[t] = x;
			update(1,1,m,t,1);
		} else update(1,1,m,t,-1);
		ans = -1, suf = 0, query(1,1,m);
		printf("%d\n", ans);
	}
}

 

转载于:https://www.cnblogs.com/uid001/p/10439921.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值