https://www.bnuoj.com/v3/problem_show.php?pid=52781
【AC】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
char str[maxn];
int l;
int a[maxn];
int b[maxn];
int n,m;
void init()
{
memset(a,-,sizeof(a));
memset(b,-,sizeof(b));
int len=;
for(int i=;i<l;i++)
{
if(str[i]==' ')
{
b[i-]=i-len;
a[i-]=len;
len=;
}
else
{
len++;
}
}
if(len)
{
b[l-]=l-len;
a[l-]=len;
}
for(int i=l-;i>=;i--)
{
if(b[i]==-)
{
b[i]=b[i+];
}
}
for(int i=l-;i>=;i--)
{
if(a[i]==-)
{
a[i]=a[i+];
}
}
} int solve(int x)
{
int ans=;
int cur=;
while(cur<l)
{
ans+=a[cur]+;
if(cur+x>=l) break;
cur=b[cur+x];
}
return ans-;
}
int main()
{
gets(str);
l=strlen(str);
scanf("%d%d",&n,&m);
init();
for(int i=n;i<=m;i++)
{
int ans=solve(i);
printf("%d\n",ans);
}
return ;
}