CodeForces - 558E 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.

Input
The first line will contain two integers n, q (1 ≤ n ≤ 105, 0 ≤ q ≤ 50 000), the length of the string and the number of queries respectively.

Next line contains a string S itself. It contains only lowercase English letters.

Next q lines will contain three integers each i, j, k (1 ≤ i ≤ j ≤ n, ).

Output
Output one line, the string S after applying the queries.

Examples
Input
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
Output
cbcaaaabdd
Input
10 1
agjucbvdfk
1 10 1
Output
abcdfgjkuv
这里写图片描述

题意很简单,就是给你个字符串,然后又q次操作,每次操作有i,j,k三个值,如果k为0,就把i,j区间的字符降序排序,如果k = 1,就将i,j区间的字符串升序排序,最后输出这个字符串。下标从1开始。

直接暴力显然会超时,这个题因为只有26个小写字母,所以我们可以利用区间查询出这个区间每个字符的个数,先把这个区间所有字符的个数都更新为0,即相当于删去所有字符。然后再按照升或降的顺序从开头存放,即从新填放,这里面对于区间的操作我们就用到了线段树。

代码如下:

#include<bits/stdc++.h>

using namespace std;
#define lson (k*2)
#define rson (k*2+1)
const int MAX = 100010;
const int MAX_V = 28;
class Node{
public:
    int l,r,v[MAX_V];
    Node();
    int mid(){
        return (l+r)/2;
    }
    int len(){
        return r-l+1;
    }
};
int Lazy[MAX*4][MAX_V];
int N,Q;
char str[MAX];
Node tree[MAX*4];
void
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值