题意:
你给你一个长度为n 的字符串, q 次操作:
每次操作将指定区间内拍成字典序最小的回文串。 如果不可能成回文串, 忽略这次操作。
思路:
大开脑洞一个题, 没想到这题还能线段树搞。(挺巧妙的)
线段树统计一个区间内, 每个字母出现的次数。
那么对于每个区间操作:[x,y]
我们先看看这个区间长度是奇数还是偶数, 如果奇数, 必须只有一个出现次数为奇数的字母。 否则忽略。
如果偶数, 必须所有字母出现次数均为偶数。
然后要字典序最小的话, 直接从a 开始枚举即可,一个一个赋值。
那么问题就转换成了 线段树的区间赋值问题, 查询是查询每个字母出现次数。
吐槽:
这题是要文件读入读出的, VJ上貌似没说, 白白RE test1 两发。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
char s[maxn];
int cnt[30], n, q;
struct node{
int l, r;
int setv;
int sum[26];
}nod[maxn<<2];
void pushup(int o){
for (int i = 0; i < 26; ++i){
nod[o].sum[i] = nod[o<<1].sum[i] + nod[o<<1|1].sum[i];
}
}
void build(int l,int r,int o){
nod[o].l = l;
nod[o].r = r;
nod[o].setv = -1;
if (l == r){
int id = s[l] - 'a';
nod[o].sum[id] = 1;
return;
}
int m = l + r >> 1;
build(l, m, o<<1);
build(m+1, r, o<<1|1);
pushup(o);
}
void pushdown(int o){
if (nod[o].setv != -1){
int l = nod[o].l;
int r = nod[o].r;
int m = l + r >> 1;
int lson = o << 1;
int rson = o << 1 | 1;
nod[lson].setv = nod[rson].setv = nod[o].setv;
memset(nod[lson].sum, 0, sizeof nod[lson].sum);
memset(nod[rson].sum, 0, sizeof nod[rson].sum);
nod[lson].sum[nod[o].setv ] = m - l + 1;
nod[rson].sum[nod[o].setv ] = r - m;
nod[o].setv = -1;
}
}
void update(int L,int R,int c,int l,int r,int o){
if (L > R) return;
if (L <= l && r <= R){
nod[o].setv = c;
memset(nod[o].sum,0,sizeof nod[o].sum);
nod[o].sum[c] = r-l+1;
return;
}
pushdown(o);
int m = l + r >> 1;
if (m >= L){
update(L, R, c, l, m, o<<1);
}
if (m < R){
update(L, R, c, m+1, r, o<<1|1);
}
pushup(o);
}
void query(int L,int R,int l,int r,int o){
if (L > R) return;
if (L <= l && r <= R){
for (int i = 0; i < 26; ++i){
cnt[i] += nod[o].sum[i];
}
return;
}
pushdown(o);
int m = l + r >> 1;
if (m >= L){
query(L, R, l, m, o<<1);
}
if (m < R){
query(L, R, m+1, r, o<<1|1);
}
}
void solve(int x,int y){
if (x > y)return;
memset(cnt,0,sizeof cnt);
query(x, y, 1, n, 1);
int odd = 0;
int pos;
for (int i = 0; i < 26; ++i){
if (cnt[i] & 1) {
++odd;
pos = i;
}
}
int len = y - x + 1;
if (len & 1){
if (odd != 1) return;
int cur = 1;
for (int i = 0; i < 26; ++i){
if (!cnt[i]) continue;
update(x + cur - 1, x + cur - 1 + cnt[i] / 2 - 1, i, 1, n, 1);
update(y - cur + 1 - cnt[i] / 2 + 1, y - cur + 1, i, 1, n, 1);
// printf("[%d,%d] -> [%d,%d]\n", x+cur-1, x+cur-1+cnt[i]/2-1, y-cur+1-cnt[i]/2+1,y-cur+1);
cur += cnt[i] / 2;
}
update(x + cur - 1, x + cur - 1, pos, 1, n, 1);
// printf("[%d,%d]\n", x+cur-1, x+cur-1);
}
else {
if (odd) return;
int cur = 1;
for (int i = 0; i < 26; ++i){
if (!cnt[i]) continue;
update(x + cur - 1, x + cur - 1 + cnt[i] / 2 - 1, i, 1, n, 1);
update(y - cur + 1 - cnt[i] / 2 + 1, y - cur + 1, i, 1, n, 1);
cur += cnt[i] / 2;
}
}
}
void dfs(int l,int r,int o){
if (l == r){
for (int i = 0; i < 26; ++i){
if (nod[o].sum[i]){
printf("%c", i + 'a');
break;
}
}
return;
}
if (nod[o].setv != -1){
for (int i = l; i <= r; ++i){
printf("%c", nod[o].setv + 'a');
}
return;
}
int m = l + r >> 1;
dfs(l, m ,o<<1);
dfs(m+1,r,o<<1|1);
}
/**
7 1
aabcbaa
5 7
**/
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d%d",&n, &q);
scanf("%s", s + 1);
build(1, n, 1);
while(q--){
int x,y;
scanf("%d%d",&x, &y);
solve(x,y);
}
dfs(1, n, 1);
putchar('\n');
return 0;
}
F. TorCoder
time limit per test
memory limit per test
input
output
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).
n from left to right, that is, s = s1s2... sn.
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).
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
n and m (1 ≤ n, m ≤ 105)
s, consisting of n
m lines contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n) — a query to apply to the string.
Output
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
substring (li, ri) 1 ≤ li ≤ ri ≤ n) of string s = s1s2... sn of length n is a sequence of characters slisli + 1...sri.
palindrome, if it reads the same from left to right and from right to left.
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.