传送门
文章目录
- 题意:
- 思路:
题意:
给定一个长度为nnn的序列aaa,定义一段区间为好区间是这段区间的所有连续子区间的和都不为000,求好区间的个数。
思路:
套路题,定义aia_iai的前缀和bi=∑j=1iaib_i=\sum _{j=1}^ia_ibi=∑j=1iai,一段区间(l,r](l,r](l,r]合法当且仅当不存在l+1<i,j≤rl+1< i,j \le rl+1<i,j≤r且bi=bjb_i=b_jbi=bj。所以我们对于每个bib_ibi求前面与他相等的相距最近的位置last[i]last[i]last[i],之后我们就可以枚举右端点rrr了,lll的话对rrr以及前面的位置lastlastlast取一个maxmaxmax即可,这样就可以保证(l,r](l,r](l,r]中没有相等的bbb,我们让答案加上其长度即可。
// Problem: C. Eugene and an array
// Contest: Codeforces - Codeforces Round #632 (Div. 2)
// URL: https://codeforces.com/contest/1333/problem/C
// Memory Limit: 256 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<assert.h>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N],last[N];
map<LL,int>mp;int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cin>>n;for(int i=1;i<=n;i++) scanf("%d",&a[i]);LL ans=0,pre=0;for(int i=0;i<=n;i++) {pre+=a[i];if(!mp.count(pre)) last[i]=-1,mp[pre]=i;else last[i]=mp[pre],mp[pre]=i;}int l=-1;for(int i=1;i<=n;i++) {l=max(l,last[i]);ans+=i-l-1;}cout<<ans<<endl;return 0;
}
/**/