【CodeForces】759C Nikita and stack
来源:互联网 发布:手机虚拟美女软件 编辑:程序博客网 时间:2024/05/11 01:44
很容易想到线段树,把插入看成1,弹出看成-1,做后缀和后用线段树维护
这题就是一个区间加减和区间最大值问题了
(区间最大值大于0表示栈顶在此)
#include<stdio.h>#include<algorithm>#define cint const int &#define M 131072int n,aim,type,x[M];bool flag;struct node{int tag,max;}t[M+5<<1];inline void maintain(cint k){ t[k].max+=t[k].tag; if (k<M) { t[k<<1].tag+=t[k].tag; t[k<<1|1].tag+=t[k].tag; } t[k].tag=0;}void change(cint k,cint l,cint r){ if (r<=aim) { t[k].tag+=type; maintain(k); return; } if (t[k].tag) maintain(k); int mid=l+r>>1; if (mid<aim) change(k<<1|1,mid+1,r); change(k<<1,l,mid); maintain(k<<1|1); t[k].max=std::max(t[k<<1].max,t[k<<1|1].max);}void top(cint k){ if (t[k].tag) maintain(k); if (t[k].max<=0) return; if (M<=k) { printf("%d\n",x[k-M+1]); flag=1; return; } if (!flag) top(k<<1|1); if (!flag) top(k<<1);}int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d%d",&aim,&type); if (type==0) type=-1; else scanf("%d",x+aim); change(1,1,M); flag=0; top(1); if (!flag) puts("-1"); }}
0 1
- 【CodeForces】759C Nikita and stack
- Codeforces 877 B Nikita and string
- Codeforces Round #430 (Div. 2) E. Nikita and game
- Codeforces 877(442 Div.2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string 题解
- Codeforces 877 B. Nikita and string (技巧)
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #442 (Div. 2) B. Nikita and string
- Codeforces Round #430 (Div. 2) E. Nikita and game
- Nikita and string
- Nikita and string
- B. Nikita and string
- Codeforces 442C Artem and Array(stack+贪心)
- 【Codeforces Round #442 (Div. 2)】 B. Nikita and string (线性状态dp)
- Codeforces Round #442 (Div. 2) B. Nikita and string (记忆化搜索)
- Codeforces Round #442(Div.2) B.Nikita and string(线性DP)
- codeforces 877 problem B Nikita and string 【思维 + 前后缀思想】
- 南华新生训练2017- (思维题)codeforces Round 442 B Nikita and string
- HDU1059 Dividing (多重背包)
- 确定windows系统是32bit还是64bit
- java设计模式之单例模式
- 博为峰JavaEE技术文章 ——MyBatis Mapper $与#的区别
- Objective-C @protected @private @public @package
- 【CodeForces】759C Nikita and stack
- Leetcode 403 - Frog Jump(dp or bfs)
- 时序分析之BCWC、OCV、AOCV模式
- 初识Nginx(三):反向代理和负载均衡
- 签offer VS 签三方
- 写一个不会动的时钟
- Ubuntu16.04下使用wine安装软件时汉字显示为方块的解决办法
- PHP 精度计算引发的灾难性Bug
- 写一个通用的事件监听函数