Codeforces Round #452 F. Letters Removing
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的更多相关文章
- 【CodeForces】899 F. Letters Removing
[题目]F. Letters Removing [题意]给定只含小写字母.大写字母和数字的字符串,每次给定一个范围要求删除[l,r]内的字符c(l和r具体位置随删除变动),求m次操作后的字符串.n&l ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Codeforces Round #452 (Div. 2) A B C
Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- Codeforces 899 F. Letters Removing (二分、树状数组)
题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...
- Codeforces Round #451 & Codeforces Round #452
Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
- 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 ...
随机推荐
- 实验四Java Android简易开发
实验准备 Android Studio下载 Android Studio安装 实验内容 Android Stuidio的安装测试 Android Stuidio的安装测试: 参考<Java和An ...
- 简单的C语言编译器--语义制导翻译
语法分析是最难写的,而这部分确实最伤脑的.大量的语义动作分析差点把我逼疯. 简而言之,这部分的作用就是在每次归约之后,都进行一些语义动作,最终让我们得到测试程序的三地址码,即中间代码. 1. ...
- Archlinux下i3wm与urxvt的配置
前段时间学习了GitHub的两位前辈:Airblader和wlh320.他们的相关教程在https://github.com/Airblader/i3和https://github.com/wlh32 ...
- Android实验报告
实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...
- MSSQL 2000 错误823恢复案例
一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...
- 火车头采集器对接织梦cms图集发布时, 采集网上图片超时的解决方法
背景介绍: 火车头采集器对接织梦cms图片集发布时, 对于多张(超过30张)大图片时, 经常会出现图集发布超时的情况. 问题分析: 因为php对于资源的处理有默认的超时时间30秒, 而我尝试了好多方 ...
- LeetCode & Q27-Remove Element-Easy
Array Two Pointers Description: Given an array and a value, remove all instances of that value in pl ...
- OAuth2.0学习(1-13)oauth2.0 的概念:资源、权限(角色)和scope
mkk 关于资源的解释 : https://andaily.com/blog/?cat=19 resource用于将系统提供的各类资源进行分组管理, 每一个resource对应一个resource-i ...
- Spring Security入门(3-1)Spring Security的登录页面定制
- java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...