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. openssl生成https证书、转换证书格式的各种相关操作

    第一步:生成 private key.csr等文件 我们可能需要输入以下信息(交互式): --- Country Name (2 letter code) [AU]:US State or Provi ...

  2. 2.UiSelector API 详细介绍

    一.UiSelector类介绍 //通过各种属性与节点关系定位组件 简单实例: public void testDemo2() throws UiObjectNotFoundException{ Ui ...

  3. array_unique后,数组本身的值并不会变

    <?php $arr = [ ' ]; print_r($arr); print_r(array_unique($arr)); print_r($arr); //array_unique后,数组 ...

  4. 「Python」19个python编写技巧

    1. 交换赋值 2. Unpacking 3. 使用操作符in 4. 字符串操作 5. 字典键值列表 6. 字典键值判断 7. 字典 get 和 setdefault 方法 8. 判断真伪 9. 遍历 ...

  5. [转]memmove、memcpy和memccpy

    原文地址:http://www.cppblog.com/kang/archive/2009/04/05/78984.html 在原文基础上进行了一些小修改~ memmove.memcpy和memccp ...

  6. LintCode 156: Merge Interval

    LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...

  7. 【BZOJ4552】【HEOI2016】排序 [二分答案][线段树]

    排序 Time Limit: 60 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 在2016年,佳媛姐姐喜欢上了数字序列 ...

  8. hihocoder1415 后缀数组三·重复旋律3

    传送门:http://hihocoder.com/problemset/problem/1415 [题解] 考虑求出两串合在一起(中间加分隔符)后缀数组,就是要求任意在两个串中的$i, j$,$\mi ...

  9. 巧用margin/padding的百分比值实现高度自适应

    原文:https://segmentfault.com/a/1190000004231995 一个基础却又容易混淆的css知识点 本文依赖于一个基础却又容易混淆的css知识点:当margin/padd ...

  10. HDU 5936 朋友

    题意为给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根. 当一方操作时,他们需要先选择一个不为根的点,满足该点到其父亲的边权为1; 然后 ...