codeforces 899F Letters Removing set+树状数组
2 seconds
256 megabytes
standard input
standard output
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.
The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations.
The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1.
Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation.
Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line.
4 2
abac
1 3 a
2 2 c
b
3 2
A0z
1 3 0
1 1 z
Az
10 4
agtFrgF4aF
2 5 g
4 9 F
1 5 4
1 7 a
tFrg4
9 5
aAAaBBccD
1 4 a
5 6 c
2 3 B
4 4 D
2 3 A
AB
In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b".
In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change.
大意:给出一个有数字和字符组成的字符串,M个操作,每次操作给定[L,R]区间,要求删除其中所有的某种字符。
题解:
看到这道题我想用 树状数组+链表解决,但是由于链表顺序查找太慢,会被极端数据卡掉,可以用siz(字符集大小)个set代替(明明就是楼主脑洞错了嘛)。
下面开始正经的正解
正解: 树状数组+平衡树(set)
用平衡树可以轻而易举的找到对应区间的字符(upper_bound 或 lower_bound)。
但是由于删除,区间是在移动的,如何维护?
树状数组即可,起始前 i 个的累加和是 i ,如果删除 j 就把 j 位置上的 1 减去。
那么前 i 个的累加和就是第 i 个字符现在的位置。但是我们的需要是找到现在第 i 个字符原来的位置 ,这个操作树状数组也可以做而且是log级的。
详见代码。
那么问题就圆满解决了,对了,最后别忘了吧set里面剩下的所有字符都拿出来排序输出。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<set>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
set<int>s[];
set<int>::iterator it,iter;
const int maxn=;
int N,M,temp;
struct BIT{
int b[maxn];
void clear()
{memset(b,,sizeof(b));}
inline int lowbit(int x)
{return x&(-x);}
void upd(int x,int p){
while(x<=N){
b[x]+=p;
x+=lowbit(x);
}
}
int query(int x){
int re=;
while(x){
re+=b[x];
x-=lowbit(x);
}
return re;
}
int lower_bound(int v){
int x=,sum=;
for(int i=(<<);i;i>>=)
if(i+x<=N&&sum+b[i+x]<v)
sum+=b[x+=i];
return x+;
}
}bit;
struct my{
int p;
char c;
bool friend operator<(const my&A,const my&B)
{return A.p<B.p;}
}m[maxn];
int cnt=;
int main(){
//freopen("in.txt","r",stdin);
N=read(),M=read();
char ch;int t1,t2;
for(int i=;i<=N;i++){
ch=getchar();
s[ch].insert(i);
bit.upd(i,);
}
while(M--){
t1=read(),t2=read();ch=getchar();
t1=bit.lower_bound(t1),t2=bit.lower_bound(t2);
it=s[ch].lower_bound(t1);
while(it!=s[ch].end()&&(*it)<=t2){
bit.upd(*it,-);
s[ch].erase(it++);
}
}
for(char i='';i<='';i++)
for(it=s[i].begin();it!=s[i].end();it++)
m[++cnt].c=i,m[cnt].p=*it;
for(char i='a';i<='z';i++)
for(it=s[i].begin();it!=s[i].end();it++)
m[++cnt].c=i,m[cnt].p=*it;
for(char i='A';i<='Z';i++)
for(it=s[i].begin();it!=s[i].end();it++)
m[++cnt].c=i,m[cnt].p=*it;
sort(m+,m++cnt);
for(int i=;i<=cnt;i++)
printf("%c",m[i].c);
return ;
}
codeforces 899F Letters Removing set+树状数组的更多相关文章
- Codeforces 899F Letters Removing 线段树/树状数组
虽然每次给一个区间,但是可以看作在区间内进行数个点操作,同样数列下标是动态变化的,如果我们将每个字符出现看作1,被删除看作0,则通过统计前缀和就能轻松计算出两个端点的位置了!这正是经典的树状数组操作 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- Codeforces 635D Factory Repairs【树状数组】
又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...
- codeforces E. DNA Evolution(树状数组)
题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...
- Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]
题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...
- codeforces#1167F. Scalar Queries(树状数组+求贡献)
题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...
随机推荐
- dubbo之延迟暴露
延迟暴露 如果你的服务需要预热时间,比如初始化缓存,等待相关资源就位等,可以使用 delay 进行延迟暴露. 延迟 5 秒暴露服务 <dubbo:service delay="5000 ...
- java实例1
1.满天星星 import java.awt.*; public class xinxin { public static void main(String[] args) { Frame w = n ...
- eclipse版本和jdk的版本兼容问题
eclipse也是有版本的,当版本过低时,无法兼容高版本的jdk 项目中用的是jdk1.8,但是低版本的eclipse只能选到jdk1.7,导致java文件在编译的过程中,不识别1.8版本jdk的语法 ...
- Win10电脑如何更改开机启动项
https://jingyan.baidu.com/article/5970355284f0458fc1074049.html
- SQL几种常用的函数
函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...
- nodejs 文件操作模块 fs
const fs=require("fs"); //文件操作 //创建目录 ./ 代表当前目录 ../ 代表上级目录fs.mkdir('./test',function(err){ ...
- Python学习【第7篇】:Python之常用模块2
hashlib,configparser,logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...
- Django REST framework 渲染器、版本
渲染器.版本: # settings.py REST_FRAMEWORK = { "DEFAULT_RENDERER_CLASSES": [ "rest_framewor ...
- 30.IK分词器配置文件讲解以及自定义词库
主要知识点: 知道IK默认的配置文件信息 自定义词库 一.ik配置文件 ik配置文件地址:es/plugins/ik/config目录 IKAnalyzer.cfg.xml:用 ...
- deepin下使用python遇到的一些情况
1.系统自带python2.7和python3.5,直接运行python默认的是2.7版本的 当然直接运行python的默认版本是可以修改的,参考这里 然后可能需要3.6版本的话,就直接在命令行输入 ...