存code
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 100006; #define LL long long int T,n,m,head1,head2,tail1,tail2; int a[maxn]; int q1[maxn][2],q2[maxn][2];LL ans; inline int read() { int x=0; char c=getchar(); while(c>'9'||c<'0')c=getchar(); while(c>='0'&&c<='9')x=x*x+c-'0',c=getchar(); return x; } int main() { T=read(); while(T--) { n=read(); for(int i=1;i<=n;i++)a[i]=read(); for(int k=1;k<=n;k++) { ans=0;int l=1; head1=head2=1;tail1=tail2=0; memset(q1,0,sizeof(q1)); memset(q2,0,sizeof(q2)); for(int i=1;i<=n;i++) { while(head1<=tail1&&q1[tail1][0]>=a[i]) tail1--; tail1++; q1[tail1][0]=a[i],q1[tail1][1]=i; while(head2<=tail2&&q2[tail2][0]<=a[i]) tail2--; tail2++; q2[tail2][0]=a[i],q2[tail2][1]=i; while(head1<=tail1 && head2<=tail2&&q2[head2][0]-q1[head1][0]>=k) { l=min(q2[head2][1],q1[head1][1])+1; if(q2[head2][1]<q1[head1][1]) { head2++; } else if(q2[head2][1]>q1[head1][1]) { head1++; } else { head1++;head2++; } } ans+=i-l+1; } printf("%lld\n",ans); } } return 0; }