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$ ...
随机推荐
- webstorm进行VisualSVN配置及上传项目到项目库
以前建站一直都是自己一个人,最近要做一个比较大的网站,寻思着利用svn在整个开发过程中会比较快,于是摸索着配置了一下. 首先,下载VisualSVN这个软件,官网链接 https://www.visu ...
- Angular——tab切换案例
基本介绍 angular框架下的tab切换,相比较于之前的纯js写的代码,有一个很大的特点就是以数据为驱动,基本上不用搜索dom元素就可以实现效果 基本使用 (1)导航部分使用的是的状态使用的是ng- ...
- Forum/viewthread.php代码备份
<!--{eval $lzthread = DB::fetch_all("SELECT `tid`,`subject` FROM ".DB::table('forum_thr ...
- C# 获取当年的周六周日
public void GetWMDay() { List<string> list = new List<string>(); "; DateTime counYe ...
- Day10 图形用户界面和游戏开发
基于tkinter模块的GUI GUI是图形用户界面的缩写,图形化的用户界面对使用过计算机的人来说应该都不陌生,在此也无需进行赘述.Python默认的GUI开发模块是tkinter(在Python 3 ...
- SqlServer IsNull 与 NullIf
ISNULL(check_expression, replacement_value) check_expression 与 replacement_value 数据类型必须一致,如果 check_e ...
- bat 读取当前目录指定文件信息并拼接
bat 读取指定文件的信息并拼接成指定格式
- scrapy实例matplotlib脚本下载
利用scrapy框架实现matplotlib实例脚本批量下载至本地并进行文件夹分类:话不多说上代码: 首先是爬虫代码: import scrapy from scrapy.linkextractors ...
- kafka监控工具kafka-manager
1.几个kafka监控工具 Kafka Web Console:监控功能较为全面,可以预览消息,监控Offset.Lag等信息,但存在bug,不建议在生产环境中使用. Kafka Manager:偏向 ...
- 中国省市区地址三级联动插件---jQuery Distpicker
插件描述:distpicker是一款可以实现中国省市区地址三级联动jQuery插件.它使用简单,简单设置即可完成中国省市区地址联动效果. [官网]https://fengyuanchen.github ...