~{→看不见LaTex格式的Dalao们请刷新本页Thanks♪(・ω・)ノ←}~
2019寒假集训题
E.A Simple Task
题面
This task is very simple. Given a string $S$ of length $n$ and $q$ queries each query is on the format $i\ j\ k$ which means sort the substring consisting of the characters from $i$ to $j$ in non-decreasing order if $k=1$ or in non-increasing order if $k=0$ .
Output the final string after applying the queries.
简要翻译
给定一个长度不超过$10^5$的字符串(小写英文字母),和不超过$5×10^4$个操作。
每个操作 $L\ R\ K$ 表示给区间$[L,R]$的字符串排序,$K=1$为升序,$K=0$为降序。
最后输出最终的字符串。
Input
第一行包含两个整数$n,q$,$n$代表字符串的长度,$q$代表询问的个数。
第二行包含一个字符串$S$。$S$只包含小写英文字母。
以下$q$行每行包含三个整数$i\ j\ k$。
Output
输出一行表示经过$q$次操作之后的字符串$S$。
Sample Input 1
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
Sample Output 1
cbcaaaabdd
Sample Input 2
10 1
agjucbvdfk
1 10 1
Sample Output 2
abcdfgjkuv
Hint
$1\le n\le 10^5$,$0\le q\le 50,000$。
$1\le i\le j\le n$,$k∈\{0,1\}$。
样例解释1
abacdabcda → abacdadcba
abacdadcba → abacacddba
abacacddba → cbaaacddba
cbaaacddba → cbcaaaddba
cbcaaaddba → cbcaaaabdd
我们用线段树来维护’$a$’-‘$c$’,我们可以用数字$1$~$26$代替。
我们每次改变序列时,先查询该区间内$1$~$26$每个数出现的次数,排完序后,我们可以知道该区间一定是连续的一段区间。
如果是升序,我们就对整个区间从小到大覆盖。
如果是降序,那就从大到小覆盖。
最后遍历一遍线段树输出即可。
总时间复杂度为$O(26nlogn)$
Code
1 |
|