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
7 2
aabcbaa
1 3
5 7
output
abacaba
input
3 2
abc
1 2
2 3
output
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.

 

题意:

给定一个长为n的字符串。

有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行。

求m次操作后的字符串。

 

26颗线段树 维护区间 每个字母的出现次数

为了使得字典序最小,贪心的放进去。

要做的操作是:区间查询和,区间赋值为1,区间赋值为0。

 

做麻烦了,其实只需要一颗线段树就可以。。。

#include<cstdio>
#include<cstring>
#define N 100001
#define M N*50
using namespace std;
int cnt;
int root[26],tot[26],pre[26],suc[26];
struct node
{
    int lc,rc,sum1;
    bool all0,all1;
    bool f0,f1;
}tr[M];
void up(int k)
{
    tr[k].all0=tr[tr[k].lc].all0&tr[tr[k].rc].all0;
    tr[k].all1=tr[tr[k].lc].all1&tr[tr[k].rc].all1;
    tr[k].sum1=tr[tr[k].lc].sum1+tr[tr[k].rc].sum1;
}
void insert(int &k,int l,int r,int pos)
{
    if(!k) k=++cnt;
    tr[k].sum1++;
    if(l==r) 
    {
        tr[k].all1=true;
        return;
    }
    int mid=l+r>>1;
    if(pos<=mid) insert(tr[k].lc,l,mid,pos);
    else insert(tr[k].rc,mid+1,r,pos);
    up(k);
}
void down(int k,int l,int r)
{
    if(tr[k].f0)
    {
        if(!tr[k].lc) tr[k].lc=++cnt;
        if(!tr[k].rc) tr[k].rc=++cnt;
        tr[tr[k].lc].all0=tr[tr[k].lc].f0=true;
        tr[tr[k].lc].all1=tr[tr[k].lc].f1=false;
        tr[tr[k].rc].all0=tr[tr[k].rc].f0=true;
        tr[tr[k].rc].all1=tr[tr[k].rc].f1=false;
        tr[tr[k].lc].sum1=tr[tr[k].rc].sum1=0;
        tr[k].f0=false;
    }
    else
    {
        if(!tr[k].lc) tr[k].lc=++cnt;
        if(!tr[k].rc) tr[k].rc=++cnt;
        tr[tr[k].lc].all0=tr[tr[k].lc].f0=false;
        tr[tr[k].lc].all1=tr[tr[k].lc].f1=true;
        tr[tr[k].rc].all0=tr[tr[k].rc].f0=false;
        tr[tr[k].rc].all1=tr[tr[k].rc].f1=true;
        int mid=l+r>>1;
        tr[tr[k].lc].sum1=mid-l+1;
        tr[tr[k].rc].sum1=r-mid;
        tr[k].f1=false;
    }
}
void change0(int k,int l,int r,int opl,int opr)
{
    if(!k) k=++cnt;
    if(l>=opl && r<=opr)
    {
        tr[k].all0=tr[k].f0=true;
        tr[k].all1=tr[k].f1=false;
        tr[k].sum1=0;
        return;
    }
    if(tr[k].f0 || tr[k].f1) down(k,l,r);
    int mid=l+r>>1;
    if(opl<=mid ) change0(tr[k].lc,l,mid,opl,opr);
    if(opr>mid ) change0(tr[k].rc,mid+1,r,opl,opr);
    up(k);
}
void change1(int &k,int l,int r,int opl,int opr)
{
    if(!k) k=++cnt;
    if(tr[k].all1) return;
    if(l>=opl && r<=opr)
    {
        tr[k].all0=tr[k].f0=false;
        tr[k].all1=tr[k].f1=true;
        tr[k].sum1=r-l+1;
        return;
    }
    if(tr[k].f0 || tr[k].f1) down(k,l,r);
    int mid=l+r>>1;
    if(opl<=mid ) change1(tr[k].lc,l,mid,opl,opr);
    if(opr>mid ) change1(tr[k].rc,mid+1,r,opl,opr);
    up(k);
}
int query(int k,int l,int r,int opl,int opr)
{
    if(tr[k].all0) return 0;
    if(l>=opl && r<=opr) return tr[k].sum1;
    if(tr[k].f0 || tr[k].f1) down(k,l,r);
    int mid=l+r>>1,tmp=0;
    if(opl<=mid && tr[k].lc) tmp+=query(tr[k].lc,l,mid,opl,opr);
    if(opr>mid && tr[k].rc) tmp+=query(tr[k].rc,mid+1,r,opl,opr);
    return tmp;
}
bool point_query(int k,int l,int r,int pos)
{
    if(l==r) return tr[k].sum1;
    if(tr[k].f0 || tr[k].f1) down(k,l,r);
    int mid=l+r>>1;
    if(pos<=mid)
    {
        if(tr[k].lc) return point_query(tr[k].lc,l,mid,pos);
        return false;
    }
    else
    {
        if(tr[k].rc) return point_query(tr[k].rc,mid+1,r,pos);
        return false;
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,m; char s[N];
    scanf("%d%d",&n,&m);
    scanf("%s",s);
    tr[0].all0=true;
    for(int i=0;i<n;i++) insert(root[s[i]-'a'],1,n,i+1);
    int opl,opr; bool odd,fail;
    int od,mid;
    while(m--)
    {
        scanf("%d%d",&opl,&opr);
        odd=fail=false; memset(tot,0,sizeof(tot)); od=-1;
        for(int i=0;i<26;i++) 
        {
            tot[i]=query(root[i],1,n,opl,opr);
            if(tot[i]%2) 
             if(!odd) odd=true,od=i,tot[i]--;
             else { fail=true; break; }
        }
        if(fail) continue;
        for(int i=0;i<26;i++) 
        if(tot[i] || od==i) change0(root[i],1,n,opl,opr);
        
        pre[0]=tot[0]; 
        for(int i=1;i<26;i++) pre[i]=pre[i-1]+tot[i];
        
        suc[0]=tot[25];
        for(int i=1;i<26;i++) suc[i]=suc[i-1]+tot[25-i];
        
        if(tot[0]) change1(root[0],1,n,opl,opl+pre[0]/2-1);
        for(int i=1;i<26;i++) 
        if(tot[i]) change1(root[i],1,n,opl+pre[i-1]/2,opl+pre[i]/2-1);
        
        if(odd) mid=opl+pre[25]/2,change1(root[od],1,n,opl+pre[25]/2,opl+pre[25]/2);
        else mid=opl+pre[25]/2-1;
        
        if(tot[25]) change1(root[25],1,n,mid+1,mid+suc[0]/2);
        for(int i=1;i<26;i++) 
        if(tot[25-i]) change1(root[25-i],1,n,mid+suc[i-1]/2+1,mid+suc[i]/2);
        /*for(int i=1;i<=n;i++)
         for(int j=0;j<26;j++)
          if(point_query(root[j],1,n,i)) 
              {
                   putchar('a'+j);
                   break;
              }
         puts("");*/
    }
    for(int i=1;i<=n;i++)
     for(int j=0;j<26;j++)
      if(point_query(root[j],1,n,i)) 
      {
           putchar('a'+j);
           break;
      }
} 
作者:xxy
本文版权归作者所有,转载请用链接,请勿原文转载,Thanks♪(・ω・)ノ。