线段树 CFRound #145F TORCODER(线段树+区间赋值)

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

substring (li, ri1 ≤ 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.

 

对于每个字母开一个线段树维护区间

对区间进行赋值啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

 

 

code:

 
// 
#include<bits/stdc++.h> 
using namespace std; 
#define maxnn 600000 
 
struct node 
{ 
    int tot,a,b,laz; 
}tree[maxnn][27]; 
int cnt[40]; 
int n,m; 
void pushdown(int p,int k) 
{ 
    tree[p<<1][k].laz=tree[p][k].laz; 
    tree[p<<1][k].tot=(tree[p<<1][k].b-tree[p<<1][k].a+1)*tree[p][k].laz; 
    tree[p<<1|1][k].laz=tree[p][k].laz; 
    tree[p<<1|1][k].tot=(tree[p<<1|1][k].b-tree[p<<1|1][k].a+1)*tree[p][k].laz; 
    tree[p][k].laz=-1; 
} 
void pushdown1(int p,int k) 
{ 
    tree[p][k].laz=-1; 
    tree[p<<1][k].laz=0; 
    tree[p<<1][k].tot=0; 
    tree[p<<1|1][k].laz=0; 
    tree[p<<1|1][k].tot=0; 
    tree[p][k].laz=-1; 
     
} 
void query(int p,int x,int y,int k,int d) 
{ 
    if(tree[p][k].laz==0) 
    pushdown1(p,k); 
    if(tree[p][k].laz!=0&&tree[p][k].laz!=-1) 
    pushdown(p,k); 
    if(tree[p][k].a>=x&&tree[p][k].b<=y) 
    { 
        tree[p][k].laz=d;     
        tree[p][k].tot=(tree[p][k].b-tree[p][k].a+1)*d; 
    } 
    else 
    { 
        int mid=(tree[p][k].a+tree[p][k].b)>>1; 
        if(x<=mid&&y>=tree[p][k].a) 
        { 
            query(p<<1,x,y,k,d); 
        } 
        { 
            if(x<=tree[p][k].b&&y>mid) 
                query(p<<1|1,x,y,k,d); 
        } 
        tree[p][k].tot=tree[p<<1][k].tot+tree[p<<1|1][k].tot; 
    } 
} 
 
int sum(int p,int x,int y,int k) 
{ 
    if(tree[p][k].laz==0) 
    pushdown1(p,k); 
    if(tree[p][k].laz!=0&&tree[p][k].laz!=-1) 
    pushdown(p,k); 
    if(tree[p][k].a>=x&&tree[p][k].b<=y) 
    { 
        return tree[p][k].tot; 
    } 
    else 
    { 
        int mid=(tree[p][k].a+tree[p][k].b)>>1; 
        int l=0,r=0; 
        if(x<=mid&&y>=tree[p][k].a) 
        { 
            l=sum(p<<1,x,y,k); 
        } 
        { 
            if(x<=tree[p][k].b&&y>mid) 
                r=sum(p<<1|1,x,y,k); 
        } 
    int tot=l+r; 
    return tot; 
    } 
} 
int ccc[40],tot;  
void qing(int p,int x,int y,int k,int d) 
{ 
    if(tree[p][k].laz!=0&&tree[p][k].laz!=-1) 
    pushdown(p,k); 
    if(tree[p][k].laz==0) 
    pushdown1(p,k);
    if(tree[p][k].a>=x&&tree[p][k].b<=y)
    { 
           tree[p][k].tot=0;
           tree[p][k].laz=0;
    }
    else
    {
    
    int mid=(tree[p][k].a+tree[p][k].b)/2;
    if(x<=mid&&y>=tree[p][k].a)
    {
        qing(p<<1,x,y,k,d);
            
    }
    if(x<=tree[p][k].b&&mid<y)
    {
        qing(p<<1|1,x,y,k,d);
    }
    tree[p][k].tot=tree[p<<1][k].tot+tree[p<<1|1][k].tot;
    }
} 
void build(int x,int y,int p,int k) 
{ 
    tree[p][k].a=x; 
    tree[p][k].b=y; 
    tree[p][k].laz=-1; 
    if(x<y) 
    { 
        build(x,(x+y)>>1,p<<1,k); 
        build((x+y)/2+1,y,p<<1|1,k); 
    } 
    else 
    tree[p][k].tot=0; 
    return ; 
} 
int main() 
{ 
    freopen("input.txt","r",stdin); 
    freopen("output.txt","w",stdout); 
    cin>>n>>m; 
    char T; 
    int x,y; 
    for(int i=1;i<=26;i++) 
    { 
        build(1,n,1,i); 
    } 
    scanf("%c",&T); 
    for(int i=1;i<=n;i++) 
    { 
        scanf("%c",&T); 
        query(1,i,i,int(T-'a'+1),1); 
    } 
    while(m--) 
    {     
        memset(cnt,0,sizeof(cnt)); 
        int tott=0; 
        int ji=0; 
        int jichu=0; 
        cin>>x>>y;tot=0; 
        for(int i=1;i<=26;i++) 
        { 
            cnt[i]=sum(1,x,y,i); 
            if(cnt[i]&1) 
            {     
                ji++; 
                jichu=i; 
            } 
            tott+=cnt[i]; 
        } 
        if(ji>1) 
        { 
            continue; 
        } 
        else 
        { 
            for(int j=1;j<=26;j++) 
            qing(1,x,y,j,0); 
            if(ji==1) 
            { 
                int l=x,r=y; 
                cnt[jichu]--; 
                query(1,x+(tott)/2,x+(tott)/2,jichu,1); 
                for(int i=1;i<=26;i++) 
                {     
                        if(cnt[i]) 
                        { 
                        query(1,l,l+cnt[i]/2-1,i,1); 
                        query(1,r-cnt[i]/2+1,r,i,1);     
                        l=l+cnt[i]/2; 
                        r=r-cnt[i]/2; 
                        } 
                } 
            } 
            if(ji==0) 
            { 
                int tmp=0; 
                int l=x,r=y; 
                for(int i=1;i<=26;i++) 
                {    if(cnt[i]) 
                    { 
                    query(1,l,l+cnt[i]/2-1,i,1); 
                    query(1,r-cnt[i]/2+1,r,i,1); 
                    l=l+cnt[i]/2; 
                    r=r-cnt[i]/2; 
                    } 
                } 
            } 
        } 
    } 
    for(int i=1;i<=n;i++) 
    { 
        for(int j=1;j<=26;j++) 
        { 
            if(sum(1,i,i,j)) 
            { 
                printf("%c",(char)(j+'a'-1)); 
            } 
        } 
    } 
} 

 

 

转载于:https://www.cnblogs.com/OIEREDSION/p/11308490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值