codeforces 374D. Inna and Sequence 线段树
给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 线段树的更多相关文章
- Codeforces 374D Inna and Sequence 二分法+树状数组
主题链接:点击打开链接 特定n一个操作,m长序列a 下列n的数量 if(co>=0)向字符串加入一个co (開始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞.简单模拟 # ...
- Codeforces 374D - Inna and Sequence
374D - Inna and Sequence 思路: 树状数组+二分 因为被删的点最多N=1e6个,所以复杂度N*logN*logN 前段时间做过一道一样的题,这类题基本套路二分找没删除前的位置 ...
- Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
随机推荐
- MySQL数据处理函数
数据处理函数 有时从数据库表中获取到的数据须要进行一些处理. 如将小写字母替换为对应的大写字母.这个处理过程能够在客户机上进行.也能够在数据库上进行. 数据库上进行会更高效.数据库中有对应的数据处理函 ...
- MyBatis使用Generator自动生成代码
MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...
- js 取消listbox选中的项
<input type="button" id="cel" value="取消选择" onclick="clearListB ...
- 手机root初体验
看到别人写的一些自己想知道的东西,顿时感到很有兴趣也很强大,固然做一个牛人有很多小粉丝是无比崇高的,可去往牛人的路上也不能少了自己~加油! 一 我来解释一下什么是ROOT以及原理 是不是要ROOT,是 ...
- pl sql项目演练--B2C商城项目
项目学习视频下载地址:点击下载 1.注册会员及找回密码模块 }该模块主要功能有注册会员和找回密码 }注册会员:所需信息主要有:登录号.密码.真实姓名.性别.密码问题.密码答案.Email.地址.电 ...
- ASP.NET MVC 必备知识点杂谈
一 工程结构4个程序集 Microsoft.Web.Mvc --一些可以使用的,不确定的程序包System.Web.Mvc --主程序库下面两个列入3.5的Net框架了System.Web.Abs ...
- cocostudio导出plist文件
今天在用Armature类时用到cocostudio导出文件,由于美术的原因他使用的是中文命名法(这你敢相信),后面在导入程序中跟了下代码发现是解析plist文件有误,我就来比较正常功能文件和有错文件 ...
- Struts学习之类型转换
* 从页面中获取对应的内容 * 在动作类action中,声明与页面中表单name属性的值同名的属性 * 提供get和set方法 * struts2框架就会通过 ...
- Hadoop学习之Hadoop集群搭建
1.检查网络状况 Dos命令:ping ip地址,同时,在Linux下通过命令:ifconfig可以查看ip信息2.修改虚拟机的ip地址 打开linux网络连接,在桌面右上角,然后编辑ip地址, ...
- spring 入门篇
spring 入门篇 相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...