TorCoder(CodeForces 240F 线段树)

TorCoder(CodeForces 240F 线段树)

题目描述
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.
输入
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.
输出
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.
样例输入
7 2
aabcbaa
1 3
5 7
样例输出
abacaba
样例输入
3 2
abc
1 2
2 3
样例输出
abc

题意:
你给你一个长度为n 的字符串, q 次操作:
每次操作将指定区间内拍成字典序最小的回文串。 如果不可能成回文串, 忽略这次操作。
思路:

思路
大开脑洞一个题, 没想到这题还能线段树搞。(挺巧妙的)
线段树统计一个区间内, 每个字母出现的次数。
那么对于每个区间操作:[x,y]
我们先看看这个区间长度是奇数还是偶数, 如果奇数, 必须只有一个出现次数为奇数的字母。 否则忽略。
如果偶数, 必须所有字母出现次数均为偶数。
然后要字典序最小的话, 直接从a 开始枚举即可,一个一个赋值。
那么问题就转换成了 线段树的区间赋值问题, 查询是查询每个字母出现次数。
原文链接:https://blog.csdn.net/aozil_yang/article/details/76861755

做法挺巧妙,区间查询+区间赋值

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include <cstdlib>
#include <functional>
#define mem(a,x) memset(a,x,sizeof(a))
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull; // %llu
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod=1e9+7;
const int INF = -1u>>1;
const int maxn = 1e5+5;
struct node
{
    int l,r,sum[30],add;    //sum为节点上每个字母的数量 add为lazy标记  //如果父区间存2个a lazy即为a 需要时在下放
} t[4*maxn];
int n,q;
int ans[30];
char s[maxn];
void cle(int o)
{
    for(int i=1; i<=26; i++)
        t[o].sum[i]=0;
}
void pushup(int o)
{
    for(int i=1; i<=26; i++)
        t[o].sum[i]=t[o<<1].sum[i]+t[o<<1|1].sum[i];
    
}
void pushdown(int o)
{
    if(t[o].add)
    {
        t[o<<1].add=t[o].add;
        t[o<<1|1].add=t[o].add;
        cle(o<<1);
        cle(o<<1|1);
        t[o<<1].sum[t[o].add]=t[o<<1].r-t[o<<1].l+1;
        t[o<<1|1].sum[t[o].add]=t[o<<1|1].r-t[o<<1|1].l+1;
        t[o].add=0;
    }
}
void build(int l,int r,int o)
{
    t[o].l=l;
    t[o].r=r;
    if(l==r)
    {
        t[o].sum[s[l]-'a'+1]=1;
        t[o].add=s[l]-'a'+1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,o<<1);
    build(mid+1,r,o<<1|1);
    pushup(o);
}
void qurey(int l,int r,int o)
{
    if(t[o].l==l&&r==t[o].r)
    {
        for(int i=1; i<=26; i++)
        {
            ans[i]+=t[o].sum[i];
        }
        return ;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(r<=mid)
    {
        qurey(l,r,o<<1);
    }
    else if(l>mid)
    {
        qurey(l,r,o<<1|1);
    }
    else
    {
        qurey(l,mid,o<<1);
        qurey(mid+1,r,o<<1|1);
    }
    pushup(o);
}
void update(int l,int r,int o,int x)
{
    if(t[o].l==l&&r==t[o].r)
    {
        cle(o);
        t[o].add=x;
        t[o].sum[x]=t[o].r-t[o].l+1;
        return ;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(r<=mid)
    {
        update(l,r,o<<1,x);
    }
    else if(l>mid)
    {
        update(l,r,o<<1|1,x);
    }
    else
    {
        update(l,mid,o<<1,x);
        update(mid+1,r,o<<1|1,x);
    }
    pushup(o);
}
void change(int l,int r)
{
    mem(ans,0);
    qurey(l,r,1);
    int sum=0;
    for(int i=1; i<=26; i++)
    {
        if(ans[i]&1)
            sum++;
        if(sum>1)
            return ;
    }
    for(int i=1; i<=26; i++)
    {
        if(ans[i])
        {
            if(ans[i]/2)
            {
                update(l,l+ans[i]/2-1,1,i);
                update(r-ans[i]/2+1,r,1,i);
                l+=ans[i]/2;
                r-=ans[i]/2;
            }
            if(ans[i]%2)
            {
                int mid=(l+r)>>1;
                update(mid,mid,1,i);
            }
        }
    }
}
void init()
{
    scanf("%d%d",&n,&q);
    scanf("%s",s+1);
    build(1,n,1);
}
void solve()
{
    int l,r;
    while(q--)
    {
        scanf("%d%d",&l,&r);
        change(l,r);
    }
}
void pri(int l,int r,int o)
{
    if(t[o].add)
    {
        for(int i=1; i<=t[o].r-t[o].l+1; i++)
        {   
            putchar(t[o].add-1+'a');
        }
        return ;
    }
    int mid=(l+r)>>1;
    pri(l,mid,o<<1);
    pri(mid+1,r,o<<1|1);
}
int main()
{       
    init();
    solve();
    pri(1,n,1);
    putchar(10);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值