题目链接

给m个数, n个操作, 一个数列, 初始为空。一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生。

求最后的数列。

用线段树, 因为最多只有n个操作, 也就是说最后的01串最大长度为n, 那么可以用一个变量now表示当前插入的话应该插入到哪个位置, 每插入一个数, now就加1,并且now最终不会超过n。

删除操作的话, 递归的进行, 如果sum[rt<<1]大于要删除的下标那么就往左儿子递归, 反之往右儿子, 递归到叶子节点的时候将sum[rt]置为0。 具体看代码。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e6+;
int sum[maxn<<], val[maxn], a[maxn], now = ;
void pushUp(int rt) {
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void update(int p, int x, int l, int r, int rt) {
if(l == r) {
sum[rt] = ;
val[l] = x;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, x, lson);
else
update(p, x, rson);
pushUp(rt);
}
void remove(int p, int l, int r, int rt) {
if(l == r) {
sum[rt] = ;
return ;
}
int m = l+r>>;
if(sum[rt<<]>=p) {
remove(p, lson);
} else {
remove(p-sum[rt<<], rson);
}
pushUp(rt);
}
void output(int l, int r, int rt) {
if(l == r) {
printf("%d", val[l]);
return ;
}
int m = l+r>>;
if(sum[rt<<])
output(lson);
if(sum[rt<<|])
output(rson);
}
int main()
{
int n, m, sign;
scanf("%d%d", &n, &m);
for(int i = ; i<m; i++) {
scanf("%d", &a[i]);
}
for(int j = ; j<n; j++) {
scanf("%d", &sign);
if(sign>=) {
update(now++, sign, , n+, );
} else {
for(int i = ; i<m; i++) {
if(a[i]-i>sum[]) //这里注意是a[i]-i, 因为之前已经删了i个数
break;
remove(a[i]-i, , n+, );
}
}
}
if(sum[]) {
output(, n+, );
} else {
puts("Poor stack!");
}
return ;
}

codeforces 374D. Inna and Sequence 线段树的更多相关文章

  1. Codeforces 374D Inna and Sequence 二分法+树状数组

    主题链接:点击打开链接 特定n一个操作,m长序列a 下列n的数量 if(co>=0)向字符串加入一个co (開始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞.简单模拟 # ...

  2. Codeforces 374D - Inna and Sequence

    374D - Inna and Sequence 思路: 树状数组+二分 因为被删的点最多N=1e6个,所以复杂度N*logN*logN 前段时间做过一道一样的题,这类题基本套路二分找没删除前的位置 ...

  3. Codeforces 486E LIS of Sequence(线段树+LIS)

    题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...

  4. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  5. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  6. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  7. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  8. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

  9. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

随机推荐

  1. javascript数组去重算法-----4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. nodejs的url模块中的resolve()的用法总结

    var url = require('url'); var a = url.resolve('/one/two/three', 'four') , b = url.resolve('http://ex ...

  3. python socket理论知识

    一.socket理论: 发现一个很好的文章,一个高手写的,我也就不再做搬运工了,直接连接吧,对理论感兴趣的可以去看看! http://www.cnblogs.com/dolphinX/p/346054 ...

  4. html5的在ie6,7,8兼容

    <script> //html5 标签 (function () { if (!/*@cc_on!@*/0) return; var html5 = "abbr,article, ...

  5. 关于使用WKWebViewJavascriptBridge报错的问题

    Error message: Undefined symbols for architecture arm64: "_OBJC_CLASS_$_WKWebViewJavascriptBrid ...

  6. 比较两个data日期之间的天数相差

    先将字符串转化为Date类型 转化方式参看:http://blog.csdn.net/caoyinghui1986/archive/2008/04/18/2303570.aspx 然后在调用Date. ...

  7. Java 中的resultset详解

    结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. 结果集读取数据 ...

  8. pushViewController自定义动画

    实现的主要代码如下: CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transiti ...

  9. Java 动态代理(转)

    一.代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与委托类之间通常会存在 ...

  10. Android ActionBar详解(一)--->显示和隐藏ActionBar

    MainActivity如下: package cc.testsimpleactionbar0; import android.os.Bundle; import android.view.View; ...