题目大意:有一个n个数组成的数组,如果一个子串中所有元素a[i]>=i,则其被成为一个好子串,求这个数组中的好子串的个数
1<=n<=1e5
思路:对于一个像1,2,3,4,5这样的好子串,我们可以发现他的好子串的数量为长度为1的子串的数量+长度为2的子串的数量+...=5+4+3+2+1。那么这样的话我们可以从第一个数开始,先让右指针移动到第一个不属于好子串的位置,然后逐个右移左指针,然后每一步移之前答案加上r-l,l=n时结束统计
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
int a[N];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
int l = 1, r = 1;
ll ans = 0;//答案最大可以达到1e10
while (l<=n)
{
while (r<=n&&a[r] >= r-l+1)
{//右端符合好子串条件时右移右指针
r++;
}
ans += r - l;//维护这段区间内的答案
l++;//每右移一格左指针要重新检查右端能不能往右扩展
}
printf("%lld\n", ans);
}
return 0;
}