CodeForces - 558E.A Simple Task字符串區間排序(計數排序+26棵線段樹的維護)

題目鏈接http://codeforces.com/problemset/problem/558/E

Time limit :5000 ms
Memory limit : 524288 kB

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, ).(0<=k<=1)

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

Note

First sample test explanation:

abacdabcda->abacdadcba
abacdadcba->abacacddba
abacacddba->cbaaacddba
cbaaacddba->cbcaaaddba
cbcaaaddba->cbcaaaabdd


題目大意:給你一個字符串,編號從1到n,給q次操作,每次操作爲x,y,id,如果id爲0就將區間[x,y]降序排序,否則按升序排序。

我們可以先STL大法暴力一波,可以跑到test 9,is_sorted是個好東西:

#include <bits/stdc++.h>
using namespace std;

const int mac=1e5+10;

char s[mac];
int a[mac];

bool cmpde(int x,int y){return x>y;}
bool cmpin(int x,int y){return x<y;}

int main()
{
    int n,q;
    scanf ("%d%d",&n,&q);
    scanf ("%s",s+1);
    for (int i=1; i<=n; i++) a[i]=s[i]-'a';
    while (q--){
        int l,r,id;
        scanf ("%d%d%d",&l,&r,&id);
        if (id==0) {
            bool yes = is_sorted(a + l, a + 1 + r, cmpde);
            if (!yes) sort(a+l,a+1+r,cmpde);
        }
        else {
            bool yes=is_sorted(a+l,a+1+r,cmpin);
            if (!yes) sort(a+l,a+1+r,cmpin);
        }
    }
    for (int i=1; i<=n; i++)
            printf ("%c",a[i]+'a');
    printf ("\n");
    return 0;
}

然後想一想,我們可以用O(n)的計數排序,由於這裏的所需要的空間非常小(將26個字母-'a’只有26個數)我們只需要26個空間就夠了,但如果也暴力求的話顯然也跑不過去,那麼我們可以用線段樹維護每一個字母的空間位置。每顆線段樹維護一個字母,詢問和修改的時候就從0到25每個走一遍。最後的複雜度就是O(26 * q * logn)。爲了方便寫,我們可以將線段樹的操作和線段樹放到結構體裏面封裝。

以下是AC代碼:

#include <bits/stdc++.h>
using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
#define debug printf ("**")

const int mac=1e5+10;

char s[mac];
int a[mac];

struct Tree
{
    int sum,f;
};

struct node
{
    Tree tree[mac<<2];

    void build(int l,int r,int rt,int val)
    {
        tree[rt].f=-1;
        if (l==r){
            if (a[l]==val) tree[rt].sum=1;
            else tree[rt].sum=0;
            return;
        }
        int mid=(l+r)>>1;
        build(lson,val);build(rson,val);
        tree[rt].sum=tree[ls].sum+tree[rs].sum;
    }

    void pushdown(int l,int r,int rt)
    {
        int mid=(l+r)>>1;
        tree[ls].f=tree[rs].f=tree[rt].f;
        tree[ls].sum=(mid-l+1)*tree[rt].f;
        tree[rs].sum=(r-mid)*tree[rt].f;
        tree[rt].f=-1;
    }

    void update(int l,int r,int rt,int L,int R,int val)
    {
        if (l>=L && r<=R) {
            tree[rt].sum=(r-l+1)*val;
            tree[rt].f=val;
            return;
        }
        int mid=(l+r)>>1;
        if (tree[rt].f!=-1) pushdown(l,r,rt);
        if (mid>=L) update(lson,L,R,val);
        if (mid<R) update(rson,L,R,val);
        tree[rt].sum=tree[ls].sum+tree[rs].sum;
    }

    int query(int l,int r,int rt,int L,int R)
    {
        int sum=0;
        if (l>=L && r<=R) return tree[rt].sum;
        int mid=(l+r)>>1;
        if (tree[rt].f!=-1) pushdown(l,r,rt);
        if (mid>=L) sum+=query(lson,L,R);
        if (mid<R) sum+=query(rson,L,R);
        return sum;
    }
}sgtree[30];

int num[30];

int main()
{
    int n,q;
    scanf ("%d%d",&n,&q);
    scanf ("%s",s+1);
    for (int i=1; i<=n; i++) a[i]=s[i]-'a';
    for (int i=0; i<26; i++) sgtree[i].build(1,n,1,i);
    while (q--){
        int l,r,id;
        scanf("%d%d%d",&l,&r,&id);
        memset(num,0,sizeof num);
        for (int i=0; i<26; i++) {
            num[i] = sgtree[i].query(1, n, 1, l, r);//有多少個在區間裏面
            sgtree[i].update(1, n, 1, l, r, 0);//第i課線段樹清空區間[l,r]
        }
        if (id){//升序排序
            for (int i=0; i<26; i++){
                if (!num[i]) continue;
                sgtree[i].update(1,n,1,l,l+num[i]-1,1);
                l+=num[i];//對左端點的控制實現排序
            }
        }
        else {//降序排序
            for (int i=25; i>=0; i--){
                if (!num[i]) continue;
                sgtree[i].update(1,n,1,l,l+num[i]-1,1);
                l+=num[i];
            }
        }
    }
    for (int i=1; i<=n; i++){
        for (int j=0; j<26; j++){//對每棵樹詢問i位置是否存在數
            if (sgtree[j].query(1,n,1,i,i)) {
                printf ("%c",j+'a');
                break;
            }
        }
    }
    printf ("\n");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章