C. Eugene and an array ----------------------------- Thinking (Contribution)

Insert picture description here
Insert picture description here
Topic:
Given an array of a, ask how many intervals are not equal to 0.
Parsing:
Reverse thinking, we find out how many intervals are
equal to 0. The idea sum [i] means prefix and

If sum [i] = sum [j], it means that the sum of the interval from i + 1 to j must be 0. So we find the position where it appears for the first time (assuming i), then the contribution is i + 1. Because all the intervals before i will include the interval of 0, +1 is because the sum of the current interval is 0.
Use the map to record

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10000;
int a[N];
ll sum;
int n;

ll res,x;
map<ll ,int > v;
int main()
{
	scanf("%d",&n);
	v[0]=0;
	int r=-1;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		res+=x;
		if(v.count(res))  r=max(r,v[res]);
		sum=sum+r+1;
		v[res]=i;
	}
	cout<<(n+1ll)*n/2-sum<<endl;
 } 
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105417884