Codeforces 240F. TorCoder 線段樹,codeforces240f

來源:互聯網
上載者:User

Codeforces 240F. TorCoder 線段樹,codeforces240f


線段樹統計和維護某一區間內的字母個數。。。。


F. TorCodertime limit per test3 secondsmemory limit per test256 megabytesinputinput.txtoutputoutput.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 strings.

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.

Sample test(s)input
7 2aabcbaa1 35 7
output
abacaba
input
3 2abc1 22 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.




/* ***********************************************Author        :CKbossCreated Time  :2015年07月20日 星期一 07時29分48秒File Name     :CF240F_2.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=100100;int n,m;int sum[28][maxn<<2];int add[maxn<<2]; /// lazychar str[maxn];int cs[28];#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1void cls(int rt){add[rt]=-1;for(int i=0;i<26;i++) sum[i][rt]=0;}void push_up(int rt){for(int i=0;i<26;i++)sum[i][rt]=sum[i][rt<<1]+sum[i][rt<<1|1];}void push_down(int l,int r,int rt){if(add[rt]!=-1){int m=(l+r)/2;int id = add[rt];cls(rt<<1); cls(rt<<1|1);add[rt<<1]=add[rt<<1|1]=add[rt];sum[id][rt<<1]=m-l+1; sum[id][rt<<1|1]=r-m;add[rt]=-1;}}void build(int l,int r,int rt){add[rt]=-1;if(l==r){int id=str[l]-'a'; sum[id][rt]++; add[rt]=id;return ;}int m=(l+r)/2;build(lson); build(rson);push_up(rt);}void query(int L,int R,int l,int r,int rt){if(L<=l&&r<=R){for(int i=0;i<26;i++) cs[i]+=sum[i][rt];return ;}push_down(l,r,rt);int m=(l+r)/2;if(L<=m) query(L,R,lson);if(R>m) query(L,R,rson);push_up(rt);}void update(int id,int L,int R,int l,int r,int rt){if(L<=l&&r<=R){cls(rt);add[rt]=id;sum[id][rt]=r-l+1;return ;}push_down(l,r,rt);int m=(l+r)/2;if(L<=m) update(id,L,R,lson);if(R>m) update(id,L,R,rson);push_up(rt);}char res[maxn];void solve(int L,int R){int op=-1,odd=0;memset(cs,0,sizeof(cs));query(L,R,1,n,1);for(int i=0;i<26;i++){if(cs[i]%2) { odd++; op=i; }if(odd>1) return ;}int st=L,en=R;int MID=L+(R-L+1)/2;for(int i=0;i<26;i++){if(cs[i]){if(cs[i]/2){update(i,st,st+cs[i]/2-1,1,n,1);update(i,en-cs[i]/2+1,en,1,n,1);st=st+cs[i]/2;en=en-cs[i]/2;}if(cs[i]%2==1){update(i,MID,MID,1,n,1);}}}}int main(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);scanf("%d%d",&n,&m);scanf("%s",str+1);/// buildbuild(1,n,1);int L,R;while(m--){scanf("%d%d",&L,&R); solve(L,R);}for(int i=1;i<=n;i++){memset(cs,0,sizeof(cs));query(i,i,1,n,1);for(int j=0;j<26;j++)if(cs[j]){putchar('a'+j); break;}}putchar(10);    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.