题目链接

给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. 自定义标签体、MVC

    自定义标签 文件以tld结尾,放在webinfo中 标签名 引用类 标签体 继承SimpleTagSupport,复写doTag() getContext(); getjspBody()   invo ...

  2. 神奇的i=i++

    最近在看c语言,被神奇的i=i++吸引,其实感觉编程是一件有趣的事情(特别喜欢算法).下面是我的测试,linux 下 gcc编译 #include<stdio.h>  main () { ...

  3. How To Set Dark Theme in Visual Studio 2010

    Want to use the visual studio color theme editor to set the dark theme or other themes? Below shows ...

  4. web前端学习之路

    test 随着自己对于web前端知识了解的越多,越来越发现自己真的好菜 一脸茫然阶段 两年前大学接触网页设计,那时对于网页设计一窍不通,只是看了一本自己大学编的一本入门教材,我甚至不知道那些网页设计的 ...

  5. Objective-C中的SEL (转载)

     SEL 在Objective-C中,SEL是选择器(selector)的一个类型.选择器就是指向方法的一个指针,读者可以简单理解为程序运行到这里就会执行指定的方法,可以这样定义一个选择器: SEL  ...

  6. 测试heightlight

    var a = '综合型律师事务所'; if (a == '综合型律师事务所') { initradio('ls_classes', '综合型律师事务所'); } else { initradio(' ...

  7. IO与文件读写---使用Apache commons IO包提高读写效率

    觉得很不错,就转载了, 作者: Paul Lin 首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解:Commons IO is a library of ...

  8. 使用泛型对java数组扩容

    编写一个通用方法,其功能是将数组扩展到10%+10个元素(转载请注明出处) package cn.reflection; import java.lang.reflect.Array; public ...

  9. UVa 116 Unidirectional TSP (DP)

    该题是<算法竞赛入门经典(第二版)>的一道例题,难度不算大.我先在没看题解的情况下自己做了一遍,虽然最终通过了,思路与书上的也一样.但比书上的代码复杂了很多,可见自己对问题的处理还是有所欠 ...

  10. MySql级联操作

    转自:http://blog.csdn.net/codeforme/article/details/5539454 外键约束对子表的含义:       如果在父表中找不到候选键,则不允许在子表上进行i ...