codeforces 138E. Hellish Constraints

26 篇文章 0 订阅

先考虑只有一个限制时,右端点扫过去,维护每个左端点是否满足限制,
A[i]=0表示i开头的后缀不满足,A[i]=1表示i开头的后缀满足
那么当新加入一个字母到后缀时,若他不是当前限制的字母,则对所有左端点的答案都无影响,若他是,它会使那些之前含L-1个限制字母的后缀变得合法,含R个的变得不合法,
记录一下这个字母w第i次出现的位置las[w][i],
将这个字母加入,就是对las[now-L]+1~las[now-L]赋为合法,las[now-R-1]+1~las[now-R-1]赋为不合法,
注意到每个位置至多被赋值合法1次,不合法一次,对于一个限制,扫过去是O(n)的
再把k个限制一起考虑就行了,总复杂度O(nk)

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e9
using namespace std;

const int maxn = 110000;
const int maxk = 1100;
const int maxv = 26;

char str[maxn],s[110];
int n,m,L,R;
int las[maxv][maxn],col[maxv];
int f[maxn];
struct node
{
    int l,r;
    node(){}
    node(const int _l,const int _r){l=_l;r=_r;}
}a[maxv][maxk]; int cnt[maxv];

ll ans;

int main()
{
    scanf("%s",str); n=strlen(str);
    scanf("%d%d%d",&m,&L,&R);
    int ev=0;
    for(int i=1;i<=m;i++)
    {
        int l,r; scanf("%s%d%d",s,&l,&r);
        const int w=s[0]-'a';
        a[w][++cnt[w]]=node(l,r);
        if(l==0) ev++;
    }
    for(int i=0;i<26;i++) las[i][col[i]=0]=-1;

    int now=0;
    for(int i=0;i<n;i++)
    {
        f[i]=ev; if(f[i]>=L&&f[i]<=R) now++;
        const int w=str[i]-'a';
        las[w][++col[w]]=i;
        for(int j=1;j<=cnt[w];j++)
        {
            if(col[w]>=a[w][j].l)
            {
                for(int k=las[w][max(0,col[w]-a[w][j].l)]+1;k<=las[w][col[w]-a[w][j].l+1];k++)
                {
                    f[k]++;
                    if(f[k]==L) now++;
                    if(f[k]==R+1) now--;
                }
            }
            if(col[w]>a[w][j].r)
            {
                for(int k=las[w][col[w]-a[w][j].r-1]+1;k<=las[w][col[w]-a[w][j].r];k++)
                {
                    f[k]--;
                    if(f[k]==R) now++;
                    if(f[k]==L-1) now--;
                }
            }
        }

        ans+=(ll)now;
    }
    printf("%I64d\n",ans);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值