CodeForces 1333-C Eugene and an array(子区间和为0、前缀和)
CodeForces 1333-C Eugene and an array(子区间和为0、前缀和)
http://codeforces.com/contest/1333/problem/C


大致题意:
如果一个子区间,它的任何子区间和都不为0,那么它很good,求这样的子区间的个数
#include <bits/stdc++.h> typedef long long LL; const int INF=0x3f3f3f3f; const double eps =1e-8; const int mod=1e8; const int maxn=2e5+10; using namespace std; map<LL,LL> mp; int main() { #ifdef DEBUG freopen("sample.txt","r",stdin); #endif int n; scanf("%d",&n); LL pos=1;//记录当上一次出现区间和为0时的位置,初始为1(把数字0插到位置1) LL sum=0;//前缀和 LL ans=0; mp[0]=1;//数字0插到了位置1 for(int i=2;i<=n+1;i++)//相当于在最前面加了一个数字0,所以编号全+1 { int x; scanf("%d",&x); sum+=x; if(mp.count(sum)) { pos=max(pos,mp[sum]); ans+=i-(pos+1)+1-1;//-1是因为区间[pos+1, i]和为0,要减去这一区间 } else ans+=i-(pos+1)+1;//加上区间[pos+1,i],[pos+2,i],[pos+3,i]...[i,i] mp[sum]=i; } printf("%lld\n",ans); return 0; }
-

浙公网安备 33010602011771号