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)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ... 
随机推荐
- collection系列用法-deque双向队列
			deque双向队列 Deque可以从两端添加和删除元素.常用的结构,是它的简化版本. Deque支持序列的常用操作,现在举一个简单例子,你会发现其实跟平成的list没啥区别: import colle ... 
- 怎样通过css的media属性,适配不同分辨率的终端设备?
			怎样通过css的media属性,适配不同分辨率的终端设备,示比例如以下: <!DOCTYPE html> <html> <head> <title>首页 ... 
- 漏掉的账目(用C语言去重)
			问题描述: 某财务部门结账时发现总金额不对头.很可能是从明细上漏掉了某1笔或几笔.如果已知明细账目清单,能通过编程找到漏掉的是哪1笔或几笔吗? 如果有多种可能,则输出所有可能的情况. 我们规定:用户输 ... 
- IOS系列——NStimer
			Timer经常使用的一些东西 1. 初始化 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@select ... 
- AppDelegate关于应用程序挂起、复原与终止的代理方法
			AppDelegate关于应用程序挂起.复原与终止的代理方法: 首次运行: - (BOOL)application:(UIApplication *)application didFinishLaun ... 
- git 使用随笔
			/*将远端库git@github.com:myrepo/base.git从远端clone到本地*/git clone git@github.com:myrepo/base.git /*克隆版本库的时候 ... 
- for循环计算某个数的阶乘、阶乘和及其倒数的阶乘和
			//4的阶乘 int jc = 4; //定义一个变量用来代表要计算的数值 long jd =1; //定义最终输出的阶乘 for(int i = 1; i <= jc;i++) //定义循环加 ... 
- Java socket字节流传输的示例
			服务端server端: package com.yuan.socket; import java.io.*; import java.net.ServerSocket; import java.net ... 
- 【转载】谈谈Cookie
			0×00 引言 在Web技术的发展史上,Cookie技术的出现是一次重大的 变革.但是, Cookie技术又是一项非常有争议的技术,从它诞生之日起就成了广大网络用户和Web开发人员的一个争论焦点,原因 ... 
- 让IE6也能智能控制图片最大宽、高度
			当一个图片的宽度或高度超出了容器时,我们一般会用max-width或max-height来设置其最大宽.高度,让图片不会超出容器,但是如果同时设置了最大高度和最大宽度时,有可能会造成图片最终显示会有些 ... 
