Codeforces Round #452 (Div. 2) F. Letters Removing 线段树

F. Letters Removing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意:给出一个字符串,每次把给定区间的给定字符删除,然后构造新的字符串,问最后剩余的串是什么,
做法:构建63颗线段树,62颗代表这个树某个字符剩余多少,然后lazy表示这区间要清空,然后再构建一个颗树代表当前区间还剩多少个字符,每次通过第63颗树查询要处理的区间在原来的坐标,然后更新这个字符所在的数,ret要减去每个区间的变化量。

#include<bits/stdc++.h>
using namespace std;
const int N =2e5+100;
int sum[27*2+10][N<<2];
bool lazy[27*2+10][N<<2];
int ret[N<<2];
char st[N];
void pushdown(int id,int l,int r,int rt){
    ret[rt] -= sum[id][rt];
    lazy[id][rt] = true;
    sum[id][rt] = 0;
    if(l==r){return;}
    int mid = l+r>>1;
    if(lazy[id][rt<<1] == false) pushdown(id,l,mid,rt<<1);
    if(lazy[id][rt<<1|1] == false) pushdown(id,mid+1,r,rt<<1|1);
}

int check(char now){
    if(now >= 'a' && now <= 'z') return now-'a';
    else if(now >= 'A' && now <= 'Z')return now-'A'+26;
    else return 52+now-'0';
}
void build(int id,int l,int r,int rt){
    if(l == r){
        if(check(st[l]) == id) sum[id][rt] = 1;
        return ;
    }
    int mid = l+r>>1;
    build(id,l,mid,rt<<1);
    build(id,mid+1,r,rt<<1|1);
    sum[id][rt] = sum[id][rt<<1]+sum[id][rt<<1|1];
}

void update(int id,int L,int R,int l,int r,int rt){
    if(L <= l &&R >= r){
        pushdown(id,l,r,rt);
        return ;
    }
    int mid = l+r>>1;
    if(mid >= L) update(id,L,R,l,mid,rt<<1);
    if(mid < R) update(id,L,R,mid+1,r,rt<<1|1);
    int nex = sum[id][rt<<1]+sum[id][rt<<1|1];
    ret[rt] -= sum[id][rt]-nex;
    sum[id][rt] = nex;
}
int query(int id,int x,int l,int r,int rt){
    if(l == r){
        return sum[id][rt];
    }
    int mid = l+r>>1;
    if(mid >= x) return query(id,x,l,mid,rt<<1);
    else return query(id,x,mid+1,r,rt<<1|1);
}

int search(int x,int l,int r,int rt){
    if(l == r) return l;
    int mid = l+r>>1;
    int cnt = ret[rt<<1];
    if(cnt >= x) return search(x,l,mid,rt<<1);
    else return search(x-cnt,mid+1,r,rt<<1|1);
}
void retbuild(int l,int r,int rt){
    ret[rt] = r-l+1;
    if(l == r) return ;
    int mid = l+r>>1;
    retbuild(l,mid,rt<<1);
    retbuild(mid+1,r,rt<<1|1);
}

int main(){
    int n,m;
    cin >> n >> m;
    scanf("%s",st+1);
    for(int i = 0;i < 62;i ++) build(i,1,n,1);
    retbuild(1,n,1);
    for(int i = 1;i <= m;i ++){
        int l,r;
        char now[10];
        scanf("%d %d %s",&l,&r,now);
        l = search(l,1,n,1);
        r = search(r,1,n,1);
        //cout << l << ' '<< r << endl;;
        update(check(now[0]),l,r,1,n,1);
    }
    for(int i = 1;i <= n;i ++){
        if(query(check(st[i]),i,1,n,1)){
            printf("%c",st[i]);
        }
    }
    puts("");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/zstu_zy/article/details/78832171