【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack
就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁。
用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1。相当于寻找最靠右的和>0的后缀。
也就是线段树区间加减,寻找最靠右侧的大于0的数的位置,记录个区间最大值就行了。
另,不知怎么回事,交到cf上RE1……
2 seconds
256 megabytes
standard input
standard output
Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer x on the top of the stack, and operation pop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operation pop() 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 the i-th step he remembers an operation he made pi-th. In other words, he remembers the operations in order of some permutation p1, 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. The i-th line starts with two integers pi and ti (1 ≤ pi ≤ m, ti = 0 or ti = 1) — the index of operation he remembers on the step i, and the type of the operation. ti equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer xi (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 from 1 to i. If the stack is empty after performing all these operations, print -1.
2
2 1 2
1 0
2
2
3
1 1 2
2 1 3
3 0
2
3
2
5
5 0
4 0
3 1 1
2 1 1
1 1 2
-1
-1
-1
-1
2
In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same.
In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed.
In the third example Nikita remembers the operations in the reversed order.
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100010
int n,a[N],maxv[N<<2],delta[N<<2];
void pushdown(int rt)
{
if(delta[rt])
{
delta[rt<<1]+=delta[rt];
delta[rt<<1|1]+=delta[rt];
maxv[rt<<1]+=delta[rt];
maxv[rt<<1|1]+=delta[rt];
delta[rt]=0;
}
}
void update(int ql,int qr,int v,int rt,int l,int r)
{
if(ql<=l&&r<=qr)
{
maxv[rt]+=v;
delta[rt]+=v;
return;
}
int m=(l+r>>1);
pushdown(rt);
if(ql<=m) update(ql,qr,v,rt<<1,l,m);
if(m<qr) update(ql,qr,v,rt<<1|1,m+1,r);
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
int Findp(int rt,int l,int r)
{
if(l==r) return l;
int m=(l+r>>1);
pushdown(rt);
if(maxv[rt<<1|1]>0) return Findp(rt<<1|1,m+1,r);
if(maxv[rt<<1]>0) return Findp(rt<<1,l,m);
return 0;
}
int main()
{
//freopen("c.in","r",stdin);
scanf("%d",&n);
int x,y;
bool op;
a[0]=-1;
for(int i=1;i<=n;++i)
{
scanf("%d%d",&x,&op);
if(op)
{
scanf("%d",&y);
a[x]=y;
update(1,x,1,1,1,n);
}
else
update(1,x,-1,1,1,n);
printf("%d\n",a[Findp(1,1,n)]);
}
return 0;
}
【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack的更多相关文章
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题
http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...
- Codeforces Round #393 (Div. 2)
A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Codeforces Round #877 (Div. 2) B. - Nikita and string
题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...
- 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E
http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...
- Codeforces Round #393 (Div. 2) - C
题目链接:http://codeforces.com/contest/760/problem/C 题意:有n个烤串,并且每个烤串起初都放在一个火盆上并且烤串都正面朝上,现在定义p序列,p[i]表示在i ...
- Codeforces Round #393 (Div. 2) - B
题目链接:http://codeforces.com/contest/760/problem/B 题意:给定n张床,m个枕头,然后给定某个特定的人(n个人中的其中一个)他睡第k张床,问这个人最多可以拿 ...
- Codeforces Round #393 (Div. 2) - A
题目链接:http://codeforces.com/contest/760/problem/A 题意:给定一个2017年的月份和该月的第一天的星期,问该月份的日历表中需要多少列.行有7列表示星期一~ ...
随机推荐
- 通过7zip压缩备份文件bat
for %%X in (*log20*) do "c:\Program Files\7-Zip\7z.exe" a "backups\%%X.zip" &quo ...
- white-space——处理元素内的空白
定义和用法 white-space 属性设置如何处理元素内的空白.这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认 ...
- AMD 和 CMD的区别
AMD 是 RequireJS 在推广过程中对模块定义的规范化产出.CMD 是 SeaJS 在推广过程中对模块定义的规范化产出.类似的还有 CommonJS Modules/2.0 规范,是 Brav ...
- LOJ tangjz的背包
题目大意 有 \(n\) 个物品, 第 \(i\) 个物品的体积为 \(i\) 令 \(f(x)\) 为 选择 \(m\) 个物品, 体积和为 \(x\) 的方案数 令 \(V = \sum_{i=1 ...
- [USACO1.3]虫洞
Luogu链接 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他 ...
- 【poj3415-Common Substrings】sam子串计数
题意: 给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...
- 【Foreign】异色弧 [树状数组]
异色弧 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 8 1 ...
- JS高级技巧(简洁版)
高级函数 由于在JS中,所有的函数都是对象,所以使用函数指针十分简单,也是这些东西使JS函数有趣且强大 安全的类型检测 JS内置的类型检测机制并不是完全可靠的 typeof 操作符返回一个字符串,表示 ...
- Local Authentication Using Challenge Response with Yubikey for CentOS 7
Connect Yubikey ,then initialize YubiKey slot 2: ykpersonalize -2 -ochal-resp -ochal-hmac -ohmac-lt ...
- maven多模块项目执行 deploy 时 忽略某些model (忽略war包)
maven deploy 时,通常需要忽略生成war的model,简单调整一下配置即可: <plugins> <plugin> <groupId>org.apach ...