http://codeforces.com/contest/760/problem/E

题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶,

注意,已有操作要按它们的时间顺序进行。

思路:线段树好题啊啊,我们把push当成+1, pop当成-1,按操作的位置建立线段树,那么如何

寻找栈顶呢,我们计算每个点的后缀,栈顶就是下标最大的>0的后缀,我们拿后缀建立线段树,

剩下的就是区间加减法,和求区间最大值啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 1e5 + ;
const int M = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, lazy[N << ], mx[N << ], a[N]; void pushDown(int rt) {
if(!lazy[rt]) return;
lazy[rt << ] += lazy[rt];
lazy[rt << | ] += lazy[rt];
mx[rt << ] += lazy[rt];
mx[rt << | ] += lazy[rt];
lazy[rt] = ;
} void update(int L, int R, int v, int l, int r, int rt) {
if(l >= L && r <= R) {
mx[rt] += v;
lazy[rt] += v;
return;
} int mid = l + r >> ;
pushDown(rt); if(L <= mid) update(L, R, v, l, mid, rt << );
if(R > mid) update(L, R, v, mid + , r, rt << | );
mx[rt] = max(mx[rt << ], mx[rt << | ]);
} int query(int l, int r, int rt) { if(mx[rt] <= ) return -;
if(l == r) return l;
int mid = l + r >> ;
pushDown(rt);
if(mx[rt << | ] > ) return query(mid + , r, rt << | ); else return query(l, mid, rt << );
} int main(){
scanf("%d", &n);
int T = n;
while(T--) {
int op, id, x;
scanf("%d%d", &id, &op);
if(op == ) {
scanf("%d", &x);
a[id] = x;
update(, id, , , n, );
} else {
update(, id, -, , n, );
} int idx = query(, n, );
if(idx == -) puts("-1");
else printf("%d\n", a[idx]);
}
return ;
} /*
*/

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题的更多相关文章

  1. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

  2. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

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

  3. 8VC Venture Cup 2017 - Elimination Round

    传送门:http://codeforces.com/contest/755 A题题意是给你一个数字n,让你找到一个数字m,使得n*m+1为合数,范围比较小,直接线性筛出1e6的质数,然后暴力枚举一下就 ...

  4. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  8. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  9. 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

随机推荐

  1. Python --Redis Hash操作

    一.Redis Hash操作 Redis 数据库hash数据类型是一个string类型的key和value的映射表,适用于存储对象.Redis 中每个 hash 可以存储 232 - 1 键值对(40 ...

  2. Mac下安装SVN插件javaHL not available的解决方法

    在Mac下安装Eclipse插件svnEclipse插件后,每次打开Eclipse都会弹出如下弹出框: 提示你本机缺少JavaHL Library. 选择Eclipse→偏好设置(preference ...

  3. 用Tensorflow实现多层神经网络

    用Tensorflow实现多层神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow机器学习实战指南 源代码请点击下方链接欢迎加星 ReLU激活函数/L1范数 ...

  4. apache源码安装必须依赖的库apr----/etc/ld.so.conf 文件介绍

    Apache所依赖的库,封装了各个系统相关的API等.虽然都是Apache开发的,但是现在最新版本的Apache和APR源码是分开的.要编Apache就必须使用APR. /etc/ld.so.conf ...

  5. Redis 安装碰到问题

    一 make 报错 1. 出现如下错误 : cd src && make all make[1]: Entering directory '/xx/xx/redis-x.x.x/src ...

  6. Code::Blocks之自动打开上次未关闭工作空间

    问题:如何设置Code::Blocks,使每次打开软件时,自动打开上次未关闭的工作空间? 设置(S) -> 环境设置...(E) -> 常规设置: 勾选"在程序启动时" ...

  7. 分治思想 特别常用 Codeforces Beta Round #80 (Div. 1 Only) D

    D. Time to Raid Cowavans time limit per test 4 seconds memory limit per test 70 megabytes input stan ...

  8. opencv 高级拼接函数Stitcher

    Stitcher https://docs.opencv.org/trunk/d8/d19/tutorial_stitcher.html http://blog.csdn.net/czl389/art ...

  9. 移动端meta声明

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...

  10. 24、CSS定位

    CSS定位方法 driver.find_element_by_css_selector() 1.CSS定位常用策略(方式) 1.id选择器 说明:根据元素id属性来选择 格式:#id 如:#userA ...