题目链接
题目大意
求判定为“good”的子区间个数。good : 在该区间内的所有子区间,不存在和为0的子区间。子序列:对原序列首位依次删除任意个形成的序列
题目思路
这个题目很巧妙,首先肯定是要用前缀和,如果pre[i]和pre[j]相等,那么[i+1,j]的和为0。这个题目最巧妙的是求区间时,枚举区间的右端点,去计算左端点的个数,我之前一直想的是枚举左端点,我还是太菜qwq
代码
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define debug printf("I am here\n");
using namespace std;
typedef long long ll;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=0x3f3f3f3f3f3f3f3f;
int t,n,a[maxn];
ll ans,pre[maxn],last=-1;
map<ll,ll> mp;
int main(){
mp[0]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=pre[i-1]+a[i];
if(mp.count(pre[i])){
last=max(last,mp[pre[i]]);
}
ans+=(i-last-1);
mp[pre[i]]=i;
}
printf("%lld\n",ans);
return 0;
}