Description

Petya has a string of length n consisting of small and large English letters and digits.

He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation.

Find how the string will look like after Petya performs all m operations.

solution

正解:线段树+set

我们先要将\([l,r]\)还原的原字符串上,\([l,r]\)对应着原序列的第 \(l,r\) 个不是空位的位置,线段树维护即可

对于删除,我们直接对每一个元素开一个set,找出区间后直接暴力删除即可,因为每一个元素只会被删一次,复杂度是 \(O(n*logn)\).

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#define RG register
#define iter iterator
#define ls (o<<1)
#define rs (o<<1|1)
using namespace std;
typedef long long ll;
const int N=200005;
int n,m,tr[N<<2],T=141;char s[N];
inline void build(int l,int r,int o){
if(l==r){tr[o]=1;return ;}
int mid=(l+r)>>1;
build(l,mid,ls);build(mid+1,r,rs);
tr[o]=tr[ls]+tr[rs];
}
set<int>S[142];
set<int>::iter it;
inline int qry(int l,int r,int o,int k){
if(l==r)return l;
int mid=(l+r)>>1,sum=tr[ls];
if(sum>=k)return qry(l,mid,ls,k);
return qry(mid+1,r,rs,k-sum);
}
inline void ins(int l,int r,int o,int sa){
tr[o]--;if(l==r)return ;
int mid=(l+r)>>1;
if(sa<=mid)ins(l,mid,ls,sa);
else ins(mid+1,r,rs,sa);
}
int st[N],top=0;bool d[N];
void work()
{
scanf("%d%d",&n,&m);
scanf("%s",s+1);
build(1,n,1);
for(int i=1;i<=n;i++)S[(int)s[i]].insert(i);
int l,r,c;char ch[2];
while(m--){
scanf("%d%d%s",&l,&r,ch);c=ch[0];
l=qry(1,n,1,l);r=qry(1,n,1,r);
it=S[c].lower_bound(l);
while(it!=S[c].end() && *it<=r)st[++top]=*it,++it;
while(top){
ins(1,n,1,st[top]);
S[c].erase(st[top]);
d[st[top--]]=1;
}
}
for(int i=1;i<=n;i++)if(!d[i])putchar(s[i]);
} int main()
{
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
work();
return 0;
}

Codeforces Round #452 F. Letters Removing的更多相关文章

  1. 【CodeForces】899 F. Letters Removing

    [题目]F. Letters Removing [题意]给定只含小写字母.大写字母和数字的字符串,每次给定一个范围要求删除[l,r]内的字符c(l和r具体位置随删除变动),求m次操作后的字符串.n&l ...

  2. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  3. Codeforces Round #452 (Div. 2) A B C

    Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...

  4. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  5. Codeforces 899 F. Letters Removing (二分、树状数组)

    题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...

  6. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

  7. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  8. Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)

    https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...

  9. Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)

    F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...

随机推荐

  1. C语言-学生博客汇总

    一.学生个人博客汇总 五班 学号 姓名 博客地址 4079 马天琦 http://www.cnblogs.com/simalang/ 4080 马宇欣 http://www.cnblogs.com/m ...

  2. networkx 学习

    import networkx as nx import pylab import numpy as np #自定义网络 row=np.array([,,,,,,]) col=np.array([,, ...

  3. JAVA_SE基础——18.方法的递归

    方法的递归是指在一个方法的内部调用自身的过程,递归必须要有结束条件,不然就会陷入无限递归的状态,永远无法结束调用,接下来用一个最简单的例子来体现下方法递归,使用递归算法计算自然数之和: public ...

  4. javascript原型链__proto__属性的理解

    在javascript中,按照惯例,构造函数始终都应该以一个大写字母开头,而非构造函数则应该以一个小写字母开头.一个方法使用new操作符创建,例如下面代码块中的Person1(可以吧Person1看做 ...

  5. LeetCode & Q38-Count and Say-Easy

    String Description: The count-and-say sequence is the sequence of integers with the first five terms ...

  6. 初学者如何查阅自然语言处理(NLP)领域学术资料

    1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP)在很大程度上与计算语言学(computational linguistics,CL ...

  7. GIT入门笔记(17)- 创建分支dev_lsq, 提交到代码

    git服务器上默认的已经有主干和test分支. 开发人员提交代码流程如下: 1.用switch to->new branch创建dev1分支 2.push branch提交到dev1分支 3.在 ...

  8. Spring Security 入门(1-1)Spring Security是什么?

    1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...

  9. SpringCloud的服务网关zuul

    演示如何使用api网关屏蔽各服务来源 一.概念和定义 1.zuul最终还是使用Ribbon的,顺便测试一下Hystrix断路保护2.zuul也是一个EurekaClient,访问服务注册中心,获取元数 ...

  10. 2017年Unity游戏开发视频教程(入门到精通)

    本文是我发布的一个Unity游戏开发的学习目录,以后我会持续发布一系列的游戏开发教程,都会更新在这个页面上,适合人群有下面的几种: 想要做独立游戏的人 想要找游戏开发相关工作的人 对游戏开发感兴趣的人 ...