F. Letters Removing
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line.

Examples
input
4 2
abac
1 3 a
2 2 c
output
b
input
3 2
A0z
1 3 0
1 1 z
output
Az
input
10 4
agtFrgF4aF
2 5 g
4 9 F
1 5 4
1 7 a
output
tFrg4
input
9 5
aAAaBBccD
1 4 a
5 6 c
2 3 B
4 4 D
2 3 A
output
AB
Note

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+树状数组的更多相关文章

  1. Codeforces 899F Letters Removing 线段树/树状数组

    虽然每次给一个区间,但是可以看作在区间内进行数个点操作,同样数列下标是动态变化的,如果我们将每个字符出现看作1,被删除看作0,则通过统计前缀和就能轻松计算出两个端点的位置了!这正是经典的树状数组操作 ...

  2. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  3. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  4. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  5. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  6. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  7. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

  8. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

  9. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

随机推荐

  1. 项目管理01--使用Maven构建项目(纯干货)

    目录 1. Maven基础知识 2. Maven实战.开发.测试.打包.部署一个Web项目 一.Maven基础知识 Maven坐标 Maven提供了一个中央仓库,里面包含了大量的开源软件的jar包,只 ...

  2. Tcl之Lab1

    Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...

  3. 一个有趣的 ”Validation of viewstate MAC failed” 错误的发现和解决

    在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户端 收到服务器对PostBack的响应后,仍然可以展现在PostBack之前设定 ...

  4. Windows 8 常见教程

    http://www.codeproject.com/Articles/439874/Web-service-on-Windows-Phone http://www.c-sharpcorner.com ...

  5. Django中 基于form的注册,基于ajax的登录

    1 form.py中写register的的form组件 from django import forms class Register(forms.Form): # 注册的form username ...

  6. matlab数值数据的表示方法,输出数据以及相关函数

    数据类型的分类: 1.整型 无符号整型和带符号整形 带符号整形的最大值是127 >>x=int8(129) 输出结果是x=127 >>x=unit8(129) 输出结果是x=1 ...

  7. jQuery如何追加tr到table中 添加到头或者尾

    jQuery 添加新内容有以下四个方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() ...

  8. sqlalchemy子查询

    使用subquery() 要使用c来定位上一个子句的属性 s1 = session.query(m.a,m.b).filter().subquery() s2 = session.query(s1.c ...

  9. 亚马逊免费服务器搭建Discuz!论坛过程(四)

    上述命令还可能因缺少包引发其他错误: 如果出错则安装对应的包即可. 以下供参考: yum install libxml2 yum install libxml2-devel -y yum instal ...

  10. python json结构

    =====================================================json==============================import reques ...