线段树21分wa求调
查看原帖
线段树21分wa求调
1113009
xly915楼主2025/8/1 16:31
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ls d<<1
#define rs d<<1|1
const int N=5e4;
const int M=26;
ll n,m,b[N+5];
string s;
struct node
{
    ll v;
    bool lazy0,lazy1;
}tree[M+5][N+5];
void pu(ll d,ll opt)
{
    tree[opt][d].v=tree[opt][ls].v+tree[opt][rs].v;
}
void pd(ll d,ll l,ll r,ll opt)
{
    if(tree[opt][d].lazy0)
    {
        tree[opt][ls].v=0;
        tree[opt][ls].lazy0=1;
        tree[opt][ls].lazy1=0;
        tree[opt][rs].v=0;
        tree[opt][rs].lazy0=1;
        tree[opt][rs].lazy1=0;
        tree[opt][d].lazy0=0;
    }
    else if(tree[opt][d].lazy1)
    {
        ll mid=(l+r)>>1;
        tree[opt][ls].v=(mid-l+1);
        tree[opt][ls].lazy0=0;
        tree[opt][ls].lazy1=1;
        tree[opt][rs].v=(r-mid);
        tree[opt][rs].lazy0=0;
        tree[opt][rs].lazy1=1;
        tree[opt][d].lazy1=0;
    }
}
void build(ll d,ll l,ll r,ll opt)
{
    if(l==r)
    {
        tree[opt][d].v=(opt==(s[l]-'A'+1));
        return ;
    }
    ll mid=(l+r)>>1;
    build(ls,l,mid,opt);
    build(rs,mid+1,r,opt);
    pu(d,opt);
}
void renew(ll d,ll l,ll r,ll ql,ll qr,ll v,ll opt)
{
    if(ql>r||qr<l) return ;
    if(ql<=l&&qr>=r)
    {
        if(v==0)
        {
        	tree[opt][d].v=0;
            tree[opt][d].lazy0=1;
            tree[opt][d].lazy1=0;
        }
        else
        {
        	tree[opt][d].v=r-l+1;
            tree[opt][d].lazy1=1;
            tree[opt][d].lazy0=0;
        }
        return ;
    }
    pd(d,l,r,opt);
    ll mid=(l+r)>>1;
    renew(ls,l,mid,ql,qr,v,opt);
    renew(rs,mid+1,r,ql,qr,v,opt);
    pu(d,opt);
}
ll find(ll d,ll l,ll r,ll ql,ll qr,ll opt)
{
    if(ql>r||qr<l) return 0;
    if(ql<=l&&qr>=r) return tree[opt][d].v;
    pd(d,l,r,opt);
    ll mid=(l+r)>>1;
    return find(ls,l,mid,ql,qr,opt)+find(rs,mid+1,r,ql,qr,opt);
}
int main()
{
    //freopen("xly.in","r",stdin);
    //freopen("xly.out","w",stdout);
    cin>>n>>m>>s;
    for(ll i=0;i<s.size();i++)
    {
        if(s[i]>='a'&&s[i]<='z') s[i]=s[i]-('a'-'A');
    }
    s=" "+s;
    for(ll i=1;i<=26;i++) build(1,1,n,i);
    for(ll i=1;i<=m;i++)
    {
        ll opt,l,r;
        cin>>opt>>l>>r;
        if(opt==1)
        {
            char p;
            cin>>p;
            if(p>='a'&&p<='z') p=p-('a'-'A');
            cout<<find(1,1,n,l,r,p-'A'+1)<<endl;
        }
        if(opt==2)
        {
        	char p;
            cin>>p;
            if(p>='a'&&p<='z') p=p-('a'-'A');
        	for(ll j=1;j<=26;j++) renew(1,1,n,l,r,0,j);
        	renew(1,1,n,l,r,1,p-'A'+1);
        }
        if(opt==3)
        {
        	for(ll j=1;j<=26;j++) b[j]=find(1,1,n,l,r,j);
        	for(ll j=1;j<=26;j++) renew(1,1,n,l,r,0,j);
        	ll last=l;
        	for(ll j=1;j<=26;j++)
        	{
        		if(b[j]==0) continue;
        		renew(1,1,n,last,last+b[j]-1,1,j);
        		last+=b[j];
        	}
        }
    }
    return 0;
}

2025/8/1 16:31
加载中...