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. 线程同步API及它们的属性

    头文件:<pthread.h> 编译记得加 -lpthread库 1:互斥锁(mutex) 1.1:互斥锁API 数据类型:pthread_mutex_t // 初始化一个互斥锁 int ...

  2. Jenkins CI Pipeline scripting

    Jenkins pipeline is a suite of Jenkins plugins. Pipelines can be seen as a sequence of stages to per ...

  3. 使用rabbitmq消息队列

    一.前言 在python中本身就是存在队列queue.一个是线程队列queue,另一个是进程multiprocessing中的队列Queue. 线程queue:只用于线程之间的数据交互 进程Queue ...

  4. pandans导出Excel并将数据保存到不同的Sheet表中

    数据存在mongodb中,按照类别导出到Excel文件,问题是想把同一类的数据放到一个sheet表中,最后只导出到一个excel文件中# coding=utf-8import pandas as pd ...

  5. [DeeplearningAI笔记]序列模型2.10词嵌入除偏

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.10词嵌入除偏 Debiasing word embeddings Bolukbasi T, Chang K W, Zo ...

  6. JQuery之validate入门

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 软件测试(三)—— 参数化测试用例(Nextday.java)

    import static org.junit.Assert.*; import java.lang.reflect.Array; import java.util.Arrays; import ja ...

  8. 自己封装的ASP.NET的MYSQL的数据库操作类

    /** * 作者:牛腩 * 创建时间:2010年3月7日17时35分 * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MyS ...

  9. 【BZOJ】1497: [NOI2006]最大获利 最大权闭合子图或最小割

    [题意]给定n个点,点权为pi.m条边,边权为ci.选择一个点集的收益是在[点集中的边权和]-[点集点权和],求最大获利.n<=5000,m<=50000,0<=ci,pi<= ...

  10. 【洛谷 P4437】 [HNOI/AHOI2018]排列(贪心,堆)

    题目链接 如果\(j<=k,a_{p[j]}!=p[k]\)可以理解为如果\(a_{p[j]}=p[k]\),那么\(k\)一定要放在\(j\)前面,也就是\(a_j\)在\(j\)前面. 于是 ...