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 ...
随机推荐
- Beta版本敏捷冲刺每日报告——Day2
1.情况简述 Beta阶段第二次Scrum Meeting 敏捷开发起止时间 2017.11.3 08:00 -- 2017.11.3 22:00 讨论时间地点 2017.11.3晚9:00,软工所实 ...
- 搭建java环境——使用Sublime Text 3(windows环境)
实现sublime Text 3对Java编译执行 参考网址:http://tieba.baidu.com/p/2609515186 1.1直接在安装路径下找到*\Packages\Java.subl ...
- git(一)快速入门
1.设置用户名 git config --global user.name '你的用户名' 2.设置用户名邮箱 git config --global user.email '你的邮箱' 3. ...
- EasyUI中DataGrid隔行改变背景颜色。
<table id="dg" class="easyui-datagrid" style="width: 1000px; height: 300 ...
- 20170222==(MODBUS读取多个寄存器)
MODBUS读取多个寄存器(功能码04) 为了简单我这里只用4个寄存器,当让你也可以用125个寄存器,但是最多也只能用125个寄存器的.每个寄存器有上面的表知道为一个字的大小即2个字节或者叫16比特位 ...
- 一次“峰回路转”的troubleshooting经历
某天,用户现场人员找到我,说应用的某个功能一点就报错,在数据库上直接跑功能对应的SQL也报错,SQL大致如下: 后来向他们要了alert.log和trace files,通过分析,确定为用户数据库版本 ...
- 移动端300ms与点透总结
300ms,主要发生在mobile 为啥会出现300ms延迟现象 浏览器想知道用户是否dobule tap(双击缩放) 下列情况不会出现300ms延迟 桌面浏览器 meta的viewport设置了us ...
- css(1-1)样式表
CSS Id 和 Class id 和 class 选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. id ...
- python爬虫requests 下载图片
import requests # 这是一个图片的url url = 'http://yun.itheima.com/Upload/Images/20170614/594106ee6ace5.jpg' ...
- Python中使用hashlib进行加密的简单使用
import hashlib ''' 原文= '字符串' 哈希加密对象 = hashlib.加密算法( 原文.encode('utf-8') ) 密文 = 哈希加密对象.hexdigest() #密文 ...