cf1333C 思维题+前缀和

cf1333C 1700的题有点久了
题意:给你一列数组,你可以删去任意的前缀和后缀,使得数组内没有一段和为0的序列,问最多有多少这样的数组。
思路:前缀和,如果有相等的那么就意味着,这段内的和一定为0,同时要对0进行初始化,和1398C有点像。
代码如下:

#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, a, n) for(int i = n; i >= a; i--)
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);
#define fclose fclose(stdin);fclose(stdout);
const int inf = 1e9;
const ll onf = 1e18;
const int maxn = 2e5+10;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
	return x*f;
}
int a[maxn];
int main(){
    int n =read();
    for(int i = 1; i <= n; i++) a[i]=read();
    map<ll, ll> m;
    m[0]=0;
    ll ans = 0, sum = 0, tmp=0;
    for(int i = 1; i <= n; i++){
        sum += a[i];
        if(m.count(sum)){
            tmp = max(tmp, m[sum]+1);
        }
        m[sum] = i;
        ans += i-tmp;
    }
    printf("%lld\n", ans);
    return 0;
}

你可能感兴趣的:(codeforces,思维题)