http://codeforces.com/contest/1632/problem/D
"d can be any positive integer" is the key: since any number can be selected, a prime number greater than 1e9 can be selected.
The line segment tree is used for interval query, and the AC code complexity is n*logn*logn.
Among them, logn*logn is a bisection*interval query. You can omit the bisection by modifying the interval query (the line segment tree is constantly bisection), reducing the complexity to n*logn.
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int tree[4*maxn];
int ary[maxn];
int n;
void pushup(int cur)
{
tree[cur]=__gcd(tree[2*cur],tree[2*cur+1]);
}
void build(int l,int r,int cur)
{
int mid;
if(l==r){
tree[cur]=ary[l];
return;
}
mid=(l+r)/2;
build(l,mid,2*cur);
build(mid+1,r,2*cur+1);
pushup(cur);
}
int query(int pl,int pr,int l,int r,int cur)
{
int mid,resl,resr;
if(pl<=l&&r<=pr){
return tree[cur];
}
mid=(l+r)/2,resl=-1,resr=-1;
if(pl<=mid) resl=query(pl,pr,l,mid,2*cur);
if(pr>mid) resr=query(pl,pr,mid+1,r,2*cur+1);
if(resl==-1) return resr;
else if(resr==-1) return resl;
else return __gcd(resl,resr);
}
int main()
{
int i,p,res,ans;
int l,r,mid,tar;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&ary[i]);
}
build(1,n,1);
p=0,ans=0;
for(i=1;i<=n;i++){
l=p+1,r=i,tar=-1;
while(l<=r){
mid=(l+r)/2;
res=query(mid,i,1,n,1);
if(res>i-mid+1){
r=mid-1;
}
else if(res<i-mid+1){
l=mid+1;
}
else{
tar=mid;
break;
}
}
if(tar!=-1){
p=i,ans++;
}
printf("%d ",ans);
}
printf("\n");
return 0;
}