codeforces 240F TorCoder 【线段树】

本文介绍了一种高效算法,用于解决TorCoder竞赛中的一道字符串处理难题,该问题要求通过一系列查询操作使字符串子串变为回文串,并尽可能地保持字典序最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F. TorCoder

time limit per test

3 seconds

memory limit per test

256 megabytes

input

input.txt

output

output.txt

A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string s, consisting of n lowercase English letters, and m queries. Each query is characterised by a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).

We'll consider the letters in the string numbered from 1 to n from left to right, that is, s = s1s2... sn.

After each query he must swap letters with indexes from li to ri inclusive in string s so as to make substring (li, ri) a palindrome. If there are multiple such letter permutations, you should choose the one where string (li, ri) will be lexicographically minimum. If no such permutation exists, you should ignore the query (that is, not change string s).

Everybody knows that on TorCoder rounds input line and array size limits never exceed 60, so Leo solved this problem easily. Your task is to solve the problem on a little bit larger limits. Given string s and m queries, print the string that results after applying all m queries to string s.

Input

The first input line contains two integers n and m (1 ≤ n, m ≤ 105) — the string length and the number of the queries.

The second line contains string s, consisting of n lowercase Latin letters.

Each of the next m lines contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n) — a query to apply to the string.

Output

In a single line print the result of applying m queries to string s. Print the queries in the order in which they are given in the input.

Examples

Input

Copy

7 2
aabcbaa
1 3
5 7

Output

Copy

abacaba

Input

Copy

3 2
abc
1 2
2 3

Output

Copy

abc

Note

A substring (li, ri) 1 ≤ li ≤ ri ≤ n) of string s = s1s2... sn of length n is a sequence of characters slisli + 1...sri.

A string is a palindrome, if it reads the same from left to right and from right to left.

String x1x2... xp is lexicographically smaller than string y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.

#include<bits/stdc++.h>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int MAX = 1e5 + 7;
int lz[MAX * 4][26], a[MAX * 4][26], cnt[26];
char s[MAX];
int n, m, num, l, r;
void pushup(int rt, int i){
    a[rt][i] = a[rt << 1][i] + a[rt << 1 | 1][i];
}
void pushdown(int rt, int l, int r, int i){
    if(lz[rt][i] != -1){
        int mid = (l + r) >> 1;
        lz[rt << 1][i] = lz[rt << 1 | 1][i] = lz[rt][i];
        a[rt << 1][i] = lz[rt][i] * (mid - l + 1);
        a[rt << 1 | 1][i] = lz[rt][i] * (r - mid);
        lz[rt][i] = -1;
    }
}
void build(int rt, int l, int r){
    if(l == r){
        a[rt][s[l] - 'a'] = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    for(int i = 0; i < 26; i++) pushup(rt, i);
}
void update(int rt, int l, int r, int x, int y, int v, int i){
    if(x <= l && r <= y){
        lz[rt][i] = v;
        a[rt][i] = v * (r - l + 1);
        return;
    }
    pushdown(rt, l, r, i);
    int mid = (l + r) >> 1;
    if(x <= mid) update(lson, x, y, v, i);
    if(mid < y) update(rson, x, y, v, i);
    pushup(rt, i);
}
void query(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y){
        for(int i = 0; i < 26; i++) cnt[i] += a[rt][i];
        return;
    }
    for(int i = 0; i < 26; i++) pushdown(rt, l, r, i);
    int mid = (l + r) >> 1;
    if(x <= mid) query(lson, x, y);
    if(mid < y) query(rson, x, y);
}
void ans(int rt, int l, int r){
    if(l == r){
        for(int i = 0; i < 26; i++)
            if(a[rt][i]){
                printf("%c", i + 'a');
                break;
            }
        return;
    }
    for(int i = 0; i < 26; i++) pushdown(rt, l, r, i);
    int mid = (l + r) >> 1;
    ans(lson);
    ans(rson);
}
int main(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &m);
    scanf("%s", s + 1);
    memset(lz, -1, sizeof lz);
    memset(a, 0, sizeof a);
    build(1, 1, n);
    while(m--){
        scanf("%d%d", &l ,&r);
        memset(cnt, 0, sizeof cnt);
        int num = 0, id;
        query(1, 1, n, l, r);
        for(int i = 0; i < 26; i++) if(cnt[i] & 1)
            num++, id = i;
        if(num > 1) continue;
        for(int i = 0; i < 26; i++) if(cnt[i]) update(1, 1, n, l, r, 0, i);
        if(num) cnt[id]--;
        for(int i = 0; i < 26; i++) if(cnt[i]){
            r = l + cnt[i] / 2 - 1;
            cnt[i] /= 2;
            update(1, 1, n, l, r, 1, i);
            l = r + 1;
        }
        if(num) update(1, 1, n, l, l, 1, id), l++;
        for(int i = 25; i >= 0; i--) if(cnt[i]){
            r = l + cnt[i] - 1;
            update(1, 1, n, l, r, 1, i);
            l = r + 1;
        }
    }
    ans(1, 1, n);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值