Subtract the minimum value from all the numbers first to prevent negative numbers from occurring.
$d =0$, direct $o (n) $ sweep over.
$d \neq 0$, the first two pointers to find out where each number as the right end of the left can be extended to where, the middle of any two number difference is a multiple of $d$ and do not repeat.
Then the right endpoint $i$ is enumerated from left to right, then the left endpoint $j$ needs to be satisfied:
$\lfloor\frac{\max (A[j]. A[i]}{d}\rfloor-\lfloor\frac{\min (A[j]). A[i])}{d}\rfloor+j\leq k+i$
The $\max$ and $\min$ are updated with the line segment tree + monotone stack, and the minimum value of this equation is maintained in the interval, then two points in the segment tree.
Time Complexity $o (n\log N) $.
#include <cstdio> #include <algorithm>const int n=200010,m=524300;int n,k,d,i,j,mi=~0u>>1,a[n],b[n ],c[n],l[n],ap[n],cnt,q0[n],t0,q1[n],t1,t,l=1,r;inline void Read (int&a) {char c;bool f=0;a=0; while (!) ( (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) | | (c== '-'))); if (c!= '-') a=c-' 0 '; else f=1; while ((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*=10) +=c-' 0 '; if (f) a=-a;} inline void Uans (int l,int r) {if (r-l>r-l| | r-l==r-l&&l<l) L=l,r=r;} inline int lower (int x) {int l=1,r=n,mid,t; while (l<=r) if (b[mid= (l+r) >>1]<=x) l= (t=mid) +1;else r=mid-1; return t;} inline int abs (int x) {return x>0?x:-x;} int ma[m],mb[m],mc[m],mac[m],mbc[m],mabc[m],ta[m],tb[m];inline int min (int a,int b) {return a<b?a:b;} inline void Taga (int x,int p) {ma[x]=ta[x]=p;mac[x]=mc[x]+p;mabc[x]=mbc[x]+p;} inline void tagb (int x,int p) {mb[x]=tb[x]=p;mbc[x]=mc[x]+p;mabc[x]=mac[x]+p;} inline void pb (int x) {if (~ta[x]) Taga (x<<1,ta[x]), Taga (X<<1|1,ta[x]), ta[x]=-1; if (tb[x]< =0) TAGB (X<<1,tb[x]), TAGB (X<<1|1,tb[x]), tb[x]=1;} inline void up (int x) {ma[x]=min (ma[x<<1],ma[x<<1|1]); Mb[x]=min (mb[x<<1],mb[x<<1|1]); Mac[x]=min (mac[x<<1],mac[x<<1|1]); Mbc[x]=min (mbc[x<<1],mbc[x<<1|1]); Mabc[x]=min (mabc[x<<1],mabc[x<<1|1]);} void build (int x,int A,int b) {mc[x]=mac[x]=mbc[x]=mabc[x]=a; Ta[x]=-1,tb[x]=1; if (a==b) return; int mid= (A+B) >>1; Build (X<<1,a,mid), build (X<<1|1,MID+1,B);} void Changea (int x,int a,int b,int c,int d,int p) {if (C<=a&&b<=d) {Taga (x,p); return;} PB (x); int mid= (A+B) >>1; if (c<=mid) Changea (x<<1,a,mid,c,d,p); if (d>mid) Changea (x<<1|1,mid+1,b,c,d,p); Up (x);} void Changeb (int x,int a,int b,int c,int d,int p) {if (C<=a&&b<=d) {TAGB (x,p); return;} PB (x); int mid= (A+B) >>1; if (c<=mid) Changeb (x<<1,a,mid,c,d,p); if (d>mid) Changeb (x<<1|1,mid+1,b,c,d,p); Up (x);} void Dfs (int x,int a,intB,int p) {if (a==b) {T=a;return;} PB (x); int mid= (A+B) >>1; if (mabc[x<<1]<=p) DFS (x<<1,a,mid,p); else Dfs (X<<1|1,MID+1,B,P); Up (x);} void Ask (int x,int a,int b,int c,int d,int p) {if (t) return; if (C<=a&&b<=d) {if (mabc[x]<=p) DFS (X,A,B,P); Return } PB (x); int mid= (A+B) >>1; if (C<=mid) ask (x<<1,a,mid,c,d,p); if (D>mid) ask (x<<1|1,mid+1,b,c,d,p); Up (x);} int main () {read (n), read (k), read (d); for (i=1;i<=n;i++) {read (a[i]); if (A[I]<MI) mi=a[i]; } if (!d) {for (i=j=1;i<=n;i++) {if (a[i]!=a[j]) j=i; Uans (J,i); } Uans (J,n); Return printf ("%d%d", l,r), 0; } for (i=1;i<=n;i++) a[i]-=mi,b[i]=a[i]; For (Std::sort (b+1,b+n+1), i=1;i<=n;i++) C[i]=lower (A[i]); for (i=j=1;i<=n;i++) {if (!ap[c[i]]) cnt++;ap[c[i]]++; while (cnt<i-j+1) {ap[c[j]]--;if (!ap[c[j]]) cnt--; j + +; } l[i]=j; } for (i=j=1;i<=n;i++) {if (ABS (A[I]-A[I-1))%d) j=i; if (l[i]<j) l[i]=j; } BuiLD (1,1,N); for (i=1;i<=n;i++) {while (t0&&a[q0[t0]]<a[i]) t0--; Changea (1,1,N,Q0[T0]+1,I,A[I]/D); Q0[++t0]=i; while (T1&&a[q1[t1]]>a[i]) t1--; Changeb (1,1,N,Q1[T1]+1,I,-A[I]/D); Q1[++t1]=i; T=0,ask (1,1,n,l[i],i,k+i); Uans (T,i); } return printf ("%d%d", l,r), 0;}
Bzoj4527:k-d-sequence