codeforces 760E or 759C 【线段树维护后缀和】
来源:互联网 发布:监控app软件排行 编辑:程序博客网 时间:2024/06/06 08:46
Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operationpush(x) puts an integerx on the top of the stack, and operationpop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operationpop() 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 madepi-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!
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. Thei-th line starts with two integerspi andti (1 ≤ pi ≤ m,ti = 0 orti = 1) — the index of operation he remembers on the stepi, and the type of the operation.ti equals0, if the operation is pop(), and 1, is the operation is push(x). If the operation ispush(x), the line also contains the integerxi (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.
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 from1 toi. If the stack is empty after performing all these operations, print-1.
22 1 21 0
22
31 1 22 1 33 0
232
55 04 03 1 12 1 11 1 2
-1-1-1-12
题意:
大致是说有个脑残,他忘了自己对一个栈的操作顺序了。现在他只能想起他第几步的操作是什么 (1 : push, 0: pop)。现在问你在他想起来的这前 i 步操做后,栈顶元素是几,栈空输出-1;
题解:
从后往前来维护一个操作数的后缀,push +1, pop -1。那么每次从后往前的第一个后缀和 > 0的位置,就应该是当前的答案。用线段树来维护后缀和(区间最大值),每次更新 1 ~ pos (+1 , -1);判断区间最大值是否 > 0,以及获得第一个 > 0的后缀的pos;那么就是区间更新维护最大值的裸题了。
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <bitset>#include <vector>#include <queue>#include <stack>#include <cmath>#include <list>#include <set>#include <map>#define rep(i,a,b) for(int i = a;i <= b;++ i)#define per(i,a,b) for(int i = a;i >= b;-- i)#define mem(a,b) memset((a),(b),sizeof((a)))#define FIN freopen("in.txt","r",stdin)#define FOUT freopen("out.txt","w",stdout)#define IO ios_base::sync_with_stdio(0),cin.tie(0)#define mid ((l+r)>>1)#define ls (id<<1)#define rs ((id<<1)|1)#define N 100005#define INF 0x3f3f3f3f#define INFF ((1LL<<62)-1)typedef long long LL;using namespace std;int n, step, op, x, ans[N];struct Node{int lazy, maxn;}node[N*4];void pushUp(int id, int l, int r){node[id].maxn = max(node[ls].maxn, node[rs].maxn);}void pushDown(int id, int l, int r){node[ls].lazy += node[id].lazy;node[rs].lazy += node[id].lazy;node[ls].maxn += node[id].lazy;node[rs].maxn += node[id].lazy;node[id].lazy = 0;}void build(int id, int l, int r){if(l == r){node[id].lazy = 0;node[id].maxn = 0;return ;}build(ls, l, mid);build(rs, mid+1, r);node[id].lazy = 0;pushUp(id, l, r);}void update(int id, int l, int r, int ql, int qr, int p){if(ql == l && qr == r){node[id].lazy += p;node[id].maxn += p;return ;}if(node[id].lazy)pushDown(id, l, r);if(qr <= mid)update(ls, l, mid, ql, qr, p);else if(ql > mid)update(rs, mid+1, r, ql, qr, p);else{update(ls, l, mid, ql, mid, p);update(rs, mid+1, r, mid+1, qr, p);}pushUp(id, l, r);}int query(int id, int l, int r){if(l == r)return l;if(node[id].lazy)pushDown(id, l, r);if(node[rs].maxn > 0)query(rs, mid+1, r);elsequery(ls, l, mid);}int main(){IO;//FIN;while(cin >> n){mem(ans, 0);build(1, 1, n);rep(i, 1, n){cin >> step >> op;if(op == 1){cin >> x;ans[step] = x;update(1, 1, n, 1, step, 1);}else{update(1, 1, n, 1, step, -1);}if(node[1].maxn > 0){int pos = query(1, 1, n);cout << ans[pos] << endl;}else{cout << -1 << endl;}}}return 0;}
- codeforces 760E or 759C 【线段树维护后缀和】
- codeforces 266E 多颗线段树维护
- Codeforces 700E Cool Slogans 后缀数组+线段树
- cf/Codeforces Round #373 div1-C/div2-E Sasha and Array 线段树 + 维护矩阵快速幂
- CF 6E 线段树 or Multiset or 双端队列维护区间最值
- 【codeforces】Codeforces Round #305 (Div. 1)E. Mike and Friends【后缀数组+线段树】
- Codeforces Round 718C(维护矩阵的线段树)
- Codeforces 718C Sasha and Array(线段树维护矩阵)
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
- Codeforces Round #305 (Div. 1)E. Mike and Friends 后缀数组+RMQ+线段树
- Codeforces Round #305 (Div. 1)E. Mike and Friends【后缀数组+线段树】
- CodeForces - 292E 线段树
- codeforces 400e 线段树
- 线段树 CodeForces 580E
- codeforces 413E 线段树
- codeforces 242E(线段树)
- Codeforces 284C Cows and sequence 构造 or 线段树
- Android-Matrix矩阵
- Unity简单触屏判断
- SDWebImage源码解读 之SDWebImageDownloaderOperation
- python 界面编程 并执行sh命令
- 存储过程与触发器的区别
- codeforces 760E or 759C 【线段树维护后缀和】
- 三 . css语法、样式表
- PAT甲级1032
- 重载与重写
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
- 指针数组&数组指针
- 你的管理,为什么没带出优秀的团队?
- 最近使用频率较高的linux命令
- (六)play之yabe项目【验证码】