Codeforces Round #632 (Div. 2) C. Eugene and an array 题解(前缀和+思维)

题目链接

题目大意

求判定为“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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值