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)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
随机推荐
- 解决Android中TextView首行缩进的问题
方式一:(推荐) setText("\u3000\u3000"+xxxxx); 方式二:这种方式不同分辨率会有问题 setText(""+xxxxx); 半角: ...
- ASP.NET MVC开发必看系列
一.关于HTTP协议的那些事 这可以说我们开发WEB程序的空气,推荐不断温故知新! HTTP协议 (一) HTTP协议详解 HTTP协议 (二) 基本认证 HTTP协议 (三) 压缩 HTTP协议 ( ...
- JQurey获取radio和checkbox的值
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web2.aspx.cs&q ...
- Fiddler [Fiddler] Connection to localhost. failed.
原文地址:http://blog.chinaunix.net/uid-20675015-id-1899931.html 在用Fiddler调试本机的网站时,即访问http://localhost,返回 ...
- Latex beamer
使用明体(gbsn)和楷体(gkai)两种字型.以下的编程是一开始的宣告,并自动生成第一张封面投影片. \documentclass[cjk]{beamer}\usepackage{CJKutf8}\ ...
- The method of using code coverage tool
Please look at the following blog: http://blog.csdn.net/superqa/article/details/9060521 Use ReportG ...
- DX9三角形顶点绕序的判断
今天亲自实践了下,终于搞清楚了. 多边形都有2个侧面,观察坐标系中正面朝向摄像机的为多边形为正面朝向多边形,正面偏离摄像机的为多边形为背面朝向多边形,d3d会把背面朝向多边形剔除,即背面消隐. 使用 ...
- SQL Server 中同时操作的例子:
在SQL 中同一逻辑阶段的操作是同时发生的. 先有一个例子做为带入: declare @x as int =1;declare @y as int =2;set @x=@y;set @y=@x;sel ...
- 编程器NAND Flash 技术入门
NAND Flash分类 SLC(Single-Level Cell)架构:单一储存单元(Cell)可储存1bit data MLC(Multi-Level Cell)架构:单一储存单元(Cell)可 ...
- php执行shell更新svn文件的方法
vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...