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

本文探讨了如何通过一系列操作来模拟字符串的增删过程,包括向字符串添加字符和删除特定位置的字符,通过简单的模拟实现字符串的动态变化。

题目链接:点击打开链接

给定n个操作,m长的序列a
下面n个数

if(co>=0)则向字符串添加一个co (开始是空字符串)

else 删除字符串中有a的下标的字符

直接在序列上搞,简单模拟

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 1000005
bool use[N], b[N];
int top;
int n, m;
int a[N];
int c[N], maxn;
inline int lowbit(int x){return x&(-x);}
void change(int pos, int val){
	while(pos<=maxn){
		c[pos]+=val;
		pos+=lowbit(pos);
	}
}
int sum(int pos){
	int ans = 0;
	while(pos)ans+=c[pos], pos-=lowbit(pos);
	return ans;
}
set<int>myset;
set<int>::iterator p;
void Erase(int pos, int r){
	int l = 1;
	while(l<=r) {
		int mid = (l+r)>>1;
		int tmp = sum(mid);
		if(tmp==pos) {
			p = myset.upper_bound(mid);
			p--;
			mid = *p;
			change(mid, -1);
			use[mid] = 1;
			myset.erase(p);
			return ;
		}
		if(tmp>pos) r = mid-1;
		else l = mid+1;
	}
}
void init(){myset.clear(); memset(c, 0, sizeof c); maxn = n+10; top = 0;}
int main(){
	int i,j,co;
	while(~scanf("%d %d",&n,&m)){
		init();
		for(i=0;i<m;i++)scanf("%d",&a[i]);
		int len = 0;
		for(i = 1; i <= n; i++) {
			scanf("%d",&co);
			if(co>=0)
				use[i] = 0, b[i] = co, change(i,1), len++, myset.insert(i);
			else {
				use[i] = 1;
				int j = lower_bound(a, a+m, len) - a;
				if(j==0 && len<a[0])continue;
				if(len!=a[j])j--;
				while(j>=0) {
					Erase(a[j], i);
					j--;
					len--;
				}
			}
		}

		if(!len)puts("Poor stack!");
		else {
			for(i = 1; i <= n; i++) if(!use[i])
				printf("%d",b[i]);
			puts("");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值