Codeforces 240 F. TorCoder 线段树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 240 F. TorCoder 线段树相关的知识,希望对你有一定的参考价值。


A boy named Leo doesnt 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).

Well 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 < qand 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棵线段树,每一个线段树保存这对应下标字母的在字符串的相应位置。

直接暴力+贪心确定每一段区间的字典序的回文串。

如果有奇数个的字母大于两个,则肯定不存在回文串。

如果有一个奇数个的字母,则区间必为奇数

2364ms

#include<bits/stdc++.h>
using namespace std;
#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

int ans_sum;
char s[N];
int num[26];
//注意一定要找一个临时变量记录下ans_***的答案,不然会覆盖
struct Node

int l,r;
int sum;
int set;
;

struct SegTree

char c;
Node tree[N<<2];
void pushdown(int i)//标记下传

if(tree[i].set!=-1)

tree[lson].set = tree[rson].set = tree[i].set;
tree[lson].sum = (tree[lson].r-tree[lson].l+1)*tree[i].set;
tree[rson].sum = (tree[rson].r-tree[rson].l+1)*tree[i].set;
tree[i].set = -1;



void pushup(int i)

tree[i].sum=tree[lson].sum+tree[rson].sum;


//建立线段树
void build(int l,int r,int i)

tree[i].l = l;
tree[i].r = r;

tree[i].set = -1;
if(l == r)

tree[i].sum = (s[l]==c);
return;

int mid = (l+r)>>1;
build(LS);
build(RS);
pushup(i);

//tree[l,r]都变为val
void set_data(int l,int r,int i,int val)

if(tree[i].l>=l&&tree[i].r<=r)

tree[i].sum = val*(tree[i].r-tree[i].l+1);

tree[i].set = val;

return;

pushdown(i); //标记下传
int mid = (tree[i].l+tree[i].r)>>1;
if(l<=mid)
set_data(l,r,lson,val);
if(r>mid)
set_data(l,r,rson,val);
pushup(i);



void query(int l,int r,int i)

//cout<<l<<" "<<r<<" "<<i<<endl;
if(l <= tree[i].l && tree[i].r <= r)

ans_sum += tree[i].sum;
return ;

pushdown(i);
int mid = (tree[i].l+tree[i].r)>>1;
if(l<=mid)
query(l,r,lson);
if(r>mid)
query(l,r,rson);
pushup(i);


segTree[26];
int n,m;
/*void out_ans()

for(int i=0; i<n; i++)

for(int j=0; j<26; j++)

ans_sum=0;
segTree[j].query(i+1,i+1,1);
if(ans_sum==1)

printf("%c",j+a);
break;



cout<<endl;
*/
int b[30];

int main()

// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

scanf("%d%d",&n,&m);
scanf(" %s",s+1);

for(int i=0; i<26; i++)

segTree[i].c=a+i;
segTree[i].build(1,n,1);


while(m--)

int l,r;
scanf("%d%d",&l,&r);

//memset(num,0,sizeof(num));
int odd_num=0;

int cnt=0;
for(int i=0; i<26; i++)

ans_sum=0;
segTree[i].query(l,r,1);
if(ans_sum%2==1)

odd_num++;

num[cnt++]=ans_sum;

if(odd_num>=2)
continue;
if((r-l+1)%2==0&&odd_num==1)
continue;


for(int i=0; i<26; i++)

segTree[i].set_data(l,r,1,0);

//out_ans();

int mid=(l+r)/2;
for(int i=0; i<cnt; i++)

if(num[i]==0)
continue;
if(num[i]%2==1)

segTree[i].set_data(mid,mid,1,1);
num[i]-=1;


segTree[i].set_data(l,l+num[i]/2-1,1,1);
segTree[i].set_data(r-num[i]/2+1,r,1,1);
l+=num[i]/2;
r-=num[i]/2;


// out_ans();


for(int i=1; i<=n; i++)

for(int j=0; j<26; j++)

ans_sum=0;
segTree[j].query(i,i,1);
if(ans_sum==1)

printf("%c",j+a);
break;






return 0;

1844ms

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

#define N 100010
#define mid (l + r >> 1)
#define ls rt << 1
#define rs ls | 1
struct segmentTree

int sum[N << 2], tag[N << 2], a[N];

void pushUp(int rt)

sum[rt] = sum[ls] + sum[rs];

void build(int rt, int l, int r)

tag[rt] = -1;
if (l == r)

sum[rt] = a[l];
return;

build(ls, l, mid), build(rs, mid + 1, r);
pushUp(rt);

inline void pushDown(int rt, int l, int r)

if (tag[rt] != -1)

tag[ls] = tag[rs] = tag[rt];
sum[ls] = (mid - l + 1) * tag[ls], sum[rs] = (r - mid) * tag[rs];
tag[rt] = -1;


int query(int rt, int l, int r, int L, int R)

if (L <= l && r <= R)
return sum[rt];
pushDown(rt, l, r);
int ret = 0;
if (L <= mid)
ret += query(ls, l, mid, L, R);
if (R > mid)
ret += query(rs, mid + 1, r, L, R);
return ret;

void update(int rt, int l, int r, int L, int R, int v)

if (L <= l && r <= R)

tag[rt] = v, sum[rt] = (r - l + 1) * v;
return;

pushDown(rt, l, r);
if (L <= mid)
update(ls, l, mid, L, R, v);
if (R > mid)
update(rs, mid + 1, r, L, R, v);
pushUp(rt);

T[26];

char s[N];
int occurCnt[26];

int main()

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n,m;
scanf("%d%d",&n,&m);
scanf("%s", s + 1);
for(int i=1; i<=n; i++)
T[s[i] - a].a[i] = 1;
for(int i=0; i<26; i++)
T[i].build(1, 1, n);
while (m--)

int l, r, oddCnt = 0;
scanf("%d%d",&l,&r);
for(int i=0; i<26; i++)

occurCnt[i] = T[i].query(1, 1, n, l, r);
if (occurCnt[i] & 1)
oddCnt++;

if ((r - l + 1) % 2 == 0 && oddCnt)
continue;
if ((r - l + 1) % 2 == 1 && oddCnt > 1)
continue;
for(int i=0; i<26; i++)
T[i].update(1, 1, n, l, r, 0);
int cur = 0;
for(int i=0; i<26; i++)
if (occurCnt[i])

T[i].update(1, 1, n, l, l + occurCnt[i] / 2 - 1, 1);
T[i].update(1, 1, n, r - occurCnt[i] / 2 + 1, r, 1);
l += occurCnt[i] / 2, r -= occurCnt[i] / 2;
if (occurCnt[i] & 1)
T[i].update(1, 1, n, mid, mid, 1);


for(int p=1; p<=n; p++)

for(int i=0; i<26; i++)

if (T[i].query(1, 1, n, p, p))

putchar(a + i);
break;


return 0;

 

以上是关于Codeforces 240 F. TorCoder 线段树的主要内容,如果未能解决你的问题,请参考以下文章

[codeforces]Codeforces Global Round 1 F. Nearest Leaf

Codeforces 835 F. Roads in the Kingdom

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #501 (Div. 3) F. Bracket Substring

Codeforces Round #491 (Div. 2) F. Concise and clear

Codeforces 908 F. New Year and Rainbow Roads(思维)