Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
傳送門
文章目錄
- 題意:
- 思路:
題意:
給定一個長度為nnn的序列aaa,定義一段區(qū)間為好區(qū)間是這段區(qū)間的所有連續(xù)子區(qū)間的和都不為000,求好區(qū)間的個數(shù)。
思路:
套路題,定義aia_iai?的前綴和bi=∑j=1iaib_i=\sum _{j=1}^ia_ibi?=∑j=1i?ai?,一段區(qū)間(l,r](l,r](l,r]合法當(dāng)且僅當(dāng)不存在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; } /**/ 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #46
- 下一篇: Codeforces Round #63