Description

有一个栈。每次告诉你已知在 \(p_i\) 时刻进行了某种操作,问在已知操作按顺序结束后栈顶元素是什么。操作数 \(10^5\) 。

Solution

每次 push 就在 \([1,p]\) 区间加一, pop 就是区间减一。询问就是最靠右的不为零的点。详见代码。

#include<bits/stdc++.h>
using namespace std;template <class T> inline void read(T &x) {x = 0; static char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar());for (; ch >= '0' && ch <= '9'; ch = getchar()) (x *= 10) += ch - '0';
}#define N 100001
#define rep(i, a, b) for (int i = a; i <= b; i++)int n, a[N], Max[N << 2], tag[N << 2];#define ls rt << 1
#define rs ls | 1
#define mid (l + r >> 1)inline void pushDown(int rt, int l, int r) {Max[ls] += tag[rt], tag[ls] += tag[rt];Max[rs] += tag[rt], tag[rs] += tag[rt];tag[rt] = 0;
}void update(int rt, int l, int r, int L, int R, int v) {if (L <= l && r <= R) { Max[rt] += v, tag[rt] += v; return; }pushDown(rt, l, r);if (L <= mid) update(ls, l, mid, L, R, v);if (R > mid) update(rs, mid + 1, r, L, R, v);Max[rt] = max(Max[ls] , Max[rs]);
}int query(int rt, int l, int r) {if (l == r) return l;pushDown(rt, l, r);if (Max[rs] > 0) return query(rs, mid + 1, r);else return query(ls, l, mid);
}int main() {read(n);rep(QwQ, 1, n) {int p, op; read(p), read(op);if (op) read(a[p]), update(1, 1, n, 1, p, 1);else update(1, 1, n, 1, p, -1);printf("%d\n", Max[1] > 0 ? a[query(1, 1, n)] : -1);}return 0;
}

转载于:https://www.cnblogs.com/aziint/p/9181581.html

CodeForces - 760E Nikita and stack相关推荐

  1. CodeForces - 993E Nikita and Order Statistics

    (暂时没有vjudge题面,CF的题面又太丑2333,直接口述了) 题目大意:给你一个长度为n的序列a[],和一个数x,对于每个i= 0~n ,求有多少个子区间满足恰好有i个数<x. sol: ...

  2. 有趣题目和认知合集(持续更新)

    写写对一些算法的理解,挂几个有意思的题,可能也会挂几个板子题 算法理解偏向于能懂即可,没有严格的证明 快乐几何 [1.2]Volatile Kite 点到直线 快乐搜与暴力 [2.4]Short Co ...

  3. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  4. Codeforces Round #300 A. Cutting Banner 水题

    A. Cutting Banner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pro ...

  5. Codeforces Round #466 (Div. 2)

    http://codeforces.com/contest/940 A水题 //#pragma comment(linker, "/stack:200000000") //#pra ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  8. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  9. Codeforces Round #645 (Div. 2)(D.The Best Vacation)

    题目链接:https://codeforces.com/contest/1358/problem/D 思路:双指针+前缀和 前缀和主要处理了两组数据:sum[]是某月到某月的天数,ans[] 代表某月 ...

最新文章

  1. Python—pandas中DataFrame类型数据操作函数
  2. linux c 守护进程创建原理及简易方法
  3. Spring 5.0 源码编译, 403, 404 依赖pom 无法下载问题
  4. 持续交付会如何影响测试
  5. 粒子群 多目标 matlab_matlab 粒子群求解三角形垂心位置
  6. unity中Animation与Animator的区别
  7. 【学习OpenCV4】如何操作图像中的像素?
  8. inDesign教程,如何设置名片?
  9. 16、1.6 常用Java开发工具
  10. python序列类型唯一的映射类型_什么是python中唯一的映射类型
  11. 移动端实现摇一摇并振动
  12. JAVA实现发短信功能
  13. Vue的8种通信方式
  14. [软件工程] UML 用例图详解
  15. 网页 浏览器内核 Web标准(七七)
  16. 如何制定项目PMO战略计划?
  17. Excel写入换行-JAVA
  18. 安装Ubuntu系统卡在载入界面,显示正在安装open vm tools
  19. 因果推理(causal inference)初步调研
  20. cardboard 效果_如何在iPhone上设置Google Cardboard

热门文章

  1. Ansible Playbook企业案例:利用 playbook 安装 nginx、安装和卸载 httpd、安装mysql
  2. Golang环境配置以及GOPATH与gomod的关系
  3. centos7下载地址
  4. Ubuntu18.04完全卸载vscode
  5. Scala apply()方法用于创建伴生类对象
  6. Python Django URL传参代码示例
  7. Eclipse中,多参列表光标跳转至第2个参数快捷键,及其他常用快捷键
  8. matlabpython建模_一直在用Matlab建模,现在Python很火,用学么?
  9. 给定的 columnmapping 与源或目标中的任意列均不匹配_闻歌研究 | 图文自动匹配任务研究调研...
  10. python进度条 pyqt_python如何通过pyqt5实现进度条