description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
t = 1 for _ in range(t): n = int(input()) a = list(map(int, input().split())) dict = {} sum = 0 dict[0] = -1 ans = 0 nearest = -1 for i in range(n): sum += a[i] if dict.get(sum) is None: ans += i - nearest else: p = dict[sum] p = max(dict[sum] + 1, nearest) ans += i - p nearest = p dict[sum] = i print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NONE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = [int(n) for n in input().split()] hash_map = {(0): 0} total = 0 bad = 0 good = 0 for i in range(1, n + 1): total += arr[i - 1] if total in hash_map: bad = max(bad, hash_map[total] + 1) hash_map[total] = i good += i - bad print(good)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) x = list(map(int, input().split())) m = {} count = 0 v = [] last, ans = -1, 0 m[0] = -1 for i in range(n): count += x[i] if count in m: if m[count] + 1 > last: v.append([m[count] + 1, i]) last = m[count] + 1 m[count] = i v.sort() last = -1 for i in range(len(v)): ans += (v[i][0] - last) * (n - v[i][1]) last = v[i][0] print(n * (n + 1) // 2 - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) A = list(map(int, input().split())) rs = [] cur = 0 for a in A: cur += a rs.append(cur) last_seen = {} max_r = n out = 0 for i in range(n - 1, -1, -1): s = rs[i] max_r = min(max_r, last_seen.get(s, n)) out += max_r - (i + 1) last_seen[s] = i max_r = min(max_r, last_seen.get(0, n)) out += max_r print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(" "))) def solve(): n = read_int() arr = read_ints() prefix = 0 G = {(0): -1} segments = [] for i, a in enumerate(arr): prefix += a if prefix in G: segments.append((i, G[prefix] + 1)) G[prefix] = i segments.sort() earliest_start = -1 T = 0 j = -1 for i in range(n): while j + 1 < len(segments) and segments[j + 1][0] <= i: j += 1 end, start = segments[j] if start > earliest_start: earliest_start = start T += i - earliest_start return T print(solve())
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from sys import stdin n = int(stdin.readline()) sm = 0 pre = {(0): -1} last_start = -1 ans = 0 for i, a in enumerate(map(int, stdin.readline().split())): sm += a if sm in pre: last_start = max(last_start, pre[sm] + 1) pre[sm] = i ans += i - last_start print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [0] + list(map(int, input().split())) for i in range(1, n + 1): a[i] += a[i - 1] mp = {(0): 0} last, ans = 0, 0 for i in range(1, n + 1): if a[i] in mp and mp[a[i]] >= last: ans -= (mp[a[i]] - last + 1) * (n - i + 1) last = mp[a[i]] + 1 mp[a[i]] = i print(ans + n * (n + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) res = n * (n - 1) // 2 + n last = {(0): -1} s = 0 j = -1 for i in range(n): s += a[i] if s in last and last[s] >= j: res -= (last[s] + 1 - j) * (n - i) j = last[s] + 1 last[s] = i print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = list(map(int, input().strip().split())) arr = arr h, hh, s, ans = {(0): 0}, -1, 0, 0 for i in range(n): s += arr[i] if s in h: hh = max(hh, h[s]) h[s] = i + 1 ans += i - hh print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def convertArrayToInt(A): for i in range(0, len(A), 1): A[i] = int(A[i]) return A def OrderN2LogNSolution(A): R = [] ans = 0 for i in range(0, len(A), 1): prefixSum = 0 ps_set = {0} flag = 0 for j in range(i, len(A), 1): prefixSum += A[j] if prefixSum in ps_set: ans += j - i flag = 1 break ps_set.add(prefixSum) if flag == 0: ans += j - i + 1 return ans def OrderNLogNSolution(A): prefixSum = 0 hashMap = {} hashMap[0] = -1 closestLeftIndex = -1 ans = 0 for i in range(0, len(A), 1): prefixSum += A[i] if prefixSum in hashMap: closestLeftIndex = max(closestLeftIndex, hashMap[prefixSum] + 1) ans += i - closestLeftIndex hashMap[prefixSum] = i return ans length = int(input()) A = [] X = input().split() A = convertArrayToInt(X) ans = OrderNLogNSolution(A) print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(arr): prefix = {} i = count = s = 0 maxL = -1 for i in range(len(arr)): s += arr[i] if s in prefix: last = prefix[s] maxL = max(maxL, last + 1) elif s == 0: maxL = 0 prefix[s] = i count += i - maxL return count x = input() arr = list(map(int, input().split())) print(solve(arr))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) l = list(map(int, input().split())) arr = [0] d = {} for i in range(1, n + 1): arr.append(arr[i - 1] + l[i - 1]) sum = 0 j = n for i in range(n, -1, -1): if arr[i] in d: j = min(j, d[arr[i]] - 1) sum += j - i d[arr[i]] = i print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from itertools import accumulate def lengthOfLongestSubstring(A): ans = 0 lastSeen = {A[0]: 0} l = 0 for i in range(1, len(A)): if A[i] not in lastSeen: ans += max(0, i - l) else: l = max(l, lastSeen[A[i]] + 1) ans += max(0, i - l) lastSeen[A[i]] = i return ans n = int(input()) a = [int(x) for x in input().split()] cum = list(accumulate(a)) ans = lengthOfLongestSubstring(cum) seen = set() for i in range(len(cum)): if cum[i] == 0: break elif cum[i] not in seen: ans += cum[i] != 0 else: break seen.add(cum[i]) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) l = list(map(int, input().split())) a = [0] lis = [] d = {(0): -1} for i in range(n): a.append(a[-1] + l[i]) if d.get(a[-1]) != None: lis.append((d[a[-1]] + 1, i)) d[a[-1]] = i else: d[a[-1]] = i ans = 0 lis = lis[::-1] for i in range(n): flag = 0 j = len(lis) - 1 while j >= 0: if lis[j][0] >= i: ans += lis[j][1] - i flag = 1 break else: lis.pop() j -= 1 if flag == 0: ans += n - i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER NONE EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def inn(): return map(int, input().split()) def cout(s): print(s, end="") (n,) = inn() a = inn() a = [x for x in a] s = 0 i = 0 dc = {(0): -1} pers = [] while i < n: s += a[i] a[i] = s if a[i] not in dc: dc[a[i]] = i else: pers.append((dc[a[i]] + 1, i)) dc[a[i]] = i i += 1 l = -1 s = 0 for x, y in pers: if l < x: s += (x - l) * (n - y) l = x s = n * (n + 1) / 2 - s s = int(s) print(s)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys from itertools import accumulate def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 N = INT() A = LIST() acc = [0] + list(accumulate(A)) D = {} R = [N] * N for i, a in enumerate(acc): if a in D: R[D[a]] = i - 1 D[a] = i R = list(accumulate(R[::-1], min))[::-1] ans = 0 for i in range(N): ans += R[i] - i print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from itertools import accumulate n = int(input()) A = list(map(int, input().split())) B = [0] + A B = list(accumulate(B)) s = 0 t = 0 S = set() ans = 0 for s in range(n + 1): while t < n + 1 and B[t] not in S: S.add(B[t]) t += 1 ans += t - 1 - s if s == t: t += 1 else: S.remove(B[s]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [int(i) for i in input().split(" ")] ans = 0 prefix, s = [0] * (n + 1), 0 for i in range(n): s += a[i] prefix[i + 1] = s l, r = 0, 0 s = {0} while l < n: while r < n and prefix[r + 1] not in s: r += 1 s.add(prefix[r]) ans += r - l s.remove(prefix[l]) l += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys def solve(a): n = len(a) ru = [0] * (n + 1) for i in range(n): ru[i + 1] = ru[i] + a[i] next_ = [n] * (n + 1) memo = {} for i in range(n + 1)[::-1]: if ru[i] in memo: next_[i] = memo[ru[i]] - 1 if i != n: next_[i] = min(next_[i + 1], next_[i]) memo[ru[i]] = i ans = 0 for i in range(n)[::-1]: ans += next_[i] - i return ans input = sys.stdin.readline n = int(input()) b = list(map(int, input().split())) ans = [[] for i in range(n)] pos = 0 for i in range(n): if b[i] == 0: pos += 1 else: ans[pos].append(b[i]) res = 0 for i in ans: res += solve(i) print(res)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) lis = list(map(int, input().split())) ss = [0] * (n + 1) ans = 0 for i in range(1, n + 1): ss[i] = ss[i - 1] + lis[i - 1] d = {} ans, l = 0, 0 for i in range(n + 1): if ss[i] in d: d[ss[i]] += 1 else: d[ss[i]] = 1 while d[ss[i]] > 1: d[ss[l]] -= 1 l += 1 ans += i - l + 1 print(ans - n - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(): n = int(input()) a = list(map(int, input().split())) total = 0 sd = {(0): -1} s = 0 mi = 0 for i in range(n): s += a[i] if s in sd: mi = max(mi, sd[s] + 2) sd[s] = i total += i + 1 - mi print(total) def main(): solve() main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
N = int(input()) D = {(0): -1} A = [int(a) for a in input().split()] m = -1 s = 0 ans = 0 for i, a in enumerate(A): if a == 0: m = i continue s += a if s not in D: D[s] = i ans += i - m else: m = max(m, D[s] + 1) ans += i - m D[s] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys n = int(sys.stdin.readline().strip()) l = [int(j) for j in sys.stdin.readline().split()] pre = [0] * (n + 1) last = 0 for i in range(1, n + 1): pre[i] = pre[i - 1] + l[i - 1] last = 0 ans = 0 idx = dict() idx[0] = 0 for i in range(1, n + 1): if pre[i] in idx: last = max(last, idx[pre[i]] + 1) ans += max(0, i - last) idx[pre[i]] = i print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys lines = sys.stdin.readlines() n = int(lines[0].strip()) nums = list(map(int, lines[1].strip().split(" "))) seen = {(0): -1} tmpSum = 0 prev = -1 res = 0 for i in range(n): tmpSum += nums[i] if tmpSum not in seen or seen[tmpSum] < prev: seen[tmpSum] = i res += i - prev else: prev = seen[tmpSum] + 1 seen[tmpSum] = i res += i - prev print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys N = int(sys.stdin.readline()) list_a = list(map(int, sys.stdin.readline().split())) list_sum = [0] * (N + 1) for i in range(N): list_sum[i + 1] = list_sum[i] + list_a[i] result = 0 set_count = set() set_count.add(0) s = e = 0 ans = 0 while s < N: while e < N and not list_sum[e + 1] in set_count: e += 1 set_count.add(list_sum[e]) ans += e - s set_count.remove(list_sum[s]) s += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [int(i) for i in input().split()] prefix_sum = [0] for i in range(n): prefix_sum.append(prefix_sum[-1] + a[i]) ans = 0 pos = 0 s = set() s.add(prefix_sum[0]) i = 0 j = 0 while i < n: while j < n and prefix_sum[j + 1] not in s: s.add(prefix_sum[j + 1]) j += 1 ans += j - i if prefix_sum[i] in s: s.remove(prefix_sum[i]) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys from itertools import accumulate input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) cum = [0] + list(accumulate(a)) s = t = 0 ans = 0 st = set() while s <= n: while t <= n and cum[t] not in st: st.add(cum[t]) t += 1 ans += t - s - 1 st.remove(cum[s]) s += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = list(map(int, input().split(" "))) prefix = [0] prefix_set = set() ans = 0 for i in range(n): prefix.append(arr[i] + prefix[i]) i = 0 j = 0 prefix_set.add(0) while i < n: while j < n and not prefix[j + 1] in prefix_set: j = j + 1 prefix_set.add(prefix[j]) ans = ans + j - i prefix_set.remove(prefix[i]) i = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
while True: try: def soln(a): elmn = {(0): 0} gdElPos = sAry = sum = 0 for i in range(len(a)): sum += a[i] if sum in elmn: gdElPos = max(gdElPos, elmn[sum] + 1) elmn[sum] = i + 1 sAry += i + 1 - gdElPos print(sAry) def read(): n = int(input()) a = list(map(int, input().split())) soln(a) if __name__ == "__main__": read() except EOFError: break
WHILE NUMBER FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
a = int(input()) la = list(map(int, input().split())) partsum = [la[0] for i in range(a)] for i in range(1, a): partsum[i] = partsum[i - 1] + la[i] good = [(1) for i in range(a)] if la[0] == 0: good[0] = 0 clear = [(True) for i in range(a)] parts = {partsum[0]} for i in range(1, a): if partsum[i] not in parts: parts.add(partsum[i]) else: clear[i] = False for i in range(1, a): good[i] = good[i - 1] + 1 if not clear[i]: for j in range(1, good[i - 1] + 2): if i >= j and partsum[i - j] == partsum[i]: good[i] = j - 1 break if partsum[i] == 0: good[i] = min(good[i], i) print(sum(good))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) d = n * (n + 1) // 2 s = 0 c = {(0): 0} k = -1 for j, q in enumerate(map(int, input().split())): s += q if s in c: i = c[s] if i > k: d -= (n - j) * (i - k) k = i c[s] = j + 1 print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) seen = dict() seen[0] = 0 out = 0 l = list(map(int, input().split())) curr = 0 bestCurr = 0 for i in range(n): curr += l[i] if curr in seen: bestCurr = min(bestCurr + 1, i - seen[curr]) else: bestCurr += 1 out += bestCurr seen[curr] = i + 1 print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(n, a): d = {(0): -1} s = 0 res = 0 j = -1 for i in range(n): s += a[i] if d.get(s) != None: j = max(d[s] + 1, j) d[s] = i res += i - j return res def main(): n = int(input()) a = [int(x) for x in input().split()] res = solve(n, a) print(res) main()
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) ar = [int(x) for x in input().split()] pre = [0] * (n + 1) ans = 0 for i in range(n): pre[i + 1] = pre[i] + ar[i] d = {} i = 0 j = 0 d[0] = 1 while i < n: while j < n and pre[j + 1] not in d: j += 1 d[pre[j]] = 1 ans += j - i d.pop(pre[i]) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) h = {(0): -1} s = 0 ans = 0 t = -1 for i in range(n): if a[i] != 0: s += a[i] if s in h and h[s] >= t: ans += i - h[s] - 1 t = h[s] + 1 else: ans += i - t h[s] = i if a[i] == 0: t = i h = {(0): i} else: h[s] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(): n = int(input()) a = list(map(int, input().split())) prefix = [(0) for _ in range(n + 1)] for i in range(1, n + 1, 1): prefix[i] = prefix[i - 1] + a[i - 1] cur_pref = dict() ans = 0 begin = 0 cur_pref[prefix[begin]] = 1 end = 0 while begin < n: while end < n and cur_pref.get(prefix[end + 1]) is None: end += 1 cur_pref[prefix[end]] = 1 ans += end - begin cur_pref.pop(prefix[begin]) begin += 1 print(ans) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NONE VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(): n = int(input()) a = list(map(int, input().split())) psum = [0] + a for i in range(n): psum[i + 1] += psum[i] ans = 0 dic = {(0): 0} rightmost = 0 for i in range(1, n + 1): if psum[i] in dic: rightmost = max(rightmost, dic[psum[i]] + 1) ans += i - rightmost dic[psum[i]] = i print(ans) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = list(map(int, input().split())) for i in range(1, len(arr)): arr[i] += arr[i - 1] arr = [0] + arr st = 0 mp = {} ans = 0 for i, c in enumerate(arr): if c in mp: st = max(st, mp[c] + 1) ans += i - st else: ans += i - st mp[c] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n = int(input()) a = list(map(int, input().split())) dic = {} dic[0] = -1 nsum = 0 ans = 0 ll = -2 for i in range(n): nsum += a[i] if nsum in dic: ll = max(dic[nsum], ll) ans += i - ll - 1 dic[nsum] = i print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = list(map(int, input().split())) notneeded = dict() notneeded[0] = -1 ans = sum = mx = 0 for i, elem in enumerate(arr): sum += elem if sum in notneeded: mx = max(mx, notneeded[sum] + 2) ans += mx notneeded[sum] = i z = len(arr) print(int(z * (z + 1) // 2) - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(a): seen = {(0): -1} res = curr = mx = 0 for i, x in enumerate(a): curr += x if curr in seen: mx = max(mx, seen[curr] + 2) res += mx seen[curr] = i return len(a) * (len(a) + 1) // 2 - res input() a = list(map(int, input().split())) print(solve(a))
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [0] + list(map(int, input().split())) hs, s, ans, hh = {(0): 0}, 0, 0, -1 for i in range(1, n + 1): s += a[i] if hs.__contains__(s): hh = max(hh, hs[s]) ans += i - hh - 1 hs[s] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys n = int(input()) a = [0] + [int(i) for i in input().split()] mx = 0 ps = 0 ans = 0 sdict = {(0): 0} for i in range(1, n + 1): ps += a[i] if ps in sdict.keys(): if ps == 0 and sdict[0] == -1: mx = max(mx, sdict[ps] + 1) else: mx = max(mx, sdict[ps] + 1) sdict[ps] = i ans = ans + i - mx print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def f(): n = int(input()) A = [int(s) for s in input().split()] S = [0] * (n + 1) memo = {(0): [0, 0]} sum = 0 for i in range(1, n + 1): sum += A[i - 1] S[i] = sum if sum not in memo: memo[sum] = [0] memo[sum].append(i) ans = 0 ok = -1 for i, s in enumerate(S): a = memo[s] a[0] += 1 if a[0] == 1: ans += i - ok - 1 else: ok = max(ok, a[a[0] - 1]) ans += i - ok - 1 print(ans) f()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def eugene_and_an_array(n, a): table = {} total = 0 table[0] = 0 ans = 0 posFrom = 0 for posTo in range(0, n): if a[posTo] == 0: posFrom = posTo + 1 continue ans += a[posTo] if ans in table: posFrom = max(table[ans] + 1, posFrom) total += posTo - posFrom + 1 table[ans] = posTo + 1 print(total) n = int(input()) a = list(map(int, input().split())) eugene_and_an_array(n, a)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) t = n * (n + 1) // 2 s = 0 d = {} b = [] d[0] = -1 for i in range(n): s += a[i] try: b.append([d[s] + 1, i]) d[s] = i except: d[s] = i if len(b) == 0: print(t) exit() pre = b[0] u, v = pre ans = (u + 1) * (n - v) mx = u for p in b[1:]: i, j = p x, y = pre if x > mx: mx = x if i - x > 0: ans += (i - mx) * (n - j) pre = p print(t - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
printn = lambda x: print(x, end="") inn = lambda: int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) ins = lambda: input().strip() DBG = True BIG = 10**18 R = 10**9 + 7 def ddprint(x): if DBG: print(x) n = inn() a = inl() a.append(0) acc = [0] * (n + 2) for i in range(1, n + 2): acc[i] = acc[i - 1] + a[i - 1] sm = 0 stt = 0 h = {acc[0]: 1} for i in range(1, n + 2): if acc[i] not in h: h[acc[i]] = 1 else: while stt < i: sm += i - stt - 1 found = acc[stt] == acc[i] del h[acc[stt]] stt += 1 if found: break h[acc[i]] = 1 print(sm)
ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
N = int(input()) A = list(map(int, input().split())) good_subarray = 0 prev_vals = {(0): -1} cum_sum = 0 left_boundary = -1 for i, a in enumerate(A): cum_sum += a if cum_sum == 0: left_boundary = prev_vals[0] + 1 if cum_sum in prev_vals: if prev_vals[cum_sum] >= left_boundary: left_boundary = prev_vals[cum_sum] + 1 good_subarray += i - left_boundary prev_vals[cum_sum] = i print(good_subarray)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().strip().split())) d = {(0): 0} l = -1 cumulative_array = [0] for i in range(n): cumulative_array.append(cumulative_array[-1] + a[i]) ans = 0 for i in range(1, n + 1): c = cumulative_array[i] if c in d: l = max(l, d[c]) d[c] = i ans += i - l - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = [int(i) for i in input().split()] dic = {(0): -1} ans = [] s = 0 for i in range(len(arr)): s += arr[i] if s in dic: if len(ans) > 0 and dic[s] + 1 > ans[-1][0]: ans.append([dic[s] + 1, i]) elif len(ans) == 0: ans.append([dic[s] + 1, i]) dic[s] = i sm = 0 for i in range(len(ans)): sm += (ans[i][0] + 1) * (n - ans[i][1]) if i != 0: sm -= min(ans[i - 1][0] + 1, ans[i][0] + 1) * min( n - ans[i][1], n - ans[i - 1][1] ) print(n * (n + 1) // 2 - sm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [0] + [int(x) for x in input().split(" ")] ans = 0 lastocc = {(0): 0} sum = 0 soms = 0 for i in range(1, n + 1): sum += a[i] if lastocc.get(sum, -1) >= soms: soms = lastocc[sum] + 1 lastocc[sum] = i ans += i - soms print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = input().split() a = [int(i) for i in a] c = [0] * n d = {} A = 0 v1 = 0 d[0] = -1 idx = -1 for i in range(n): if i == 0: c[i] = a[i] else: c[i] = c[i - 1] + a[i] v = d.get(c[i]) if v is not None: idx = max(idx, v + 1) if a[i] == 0: idx = max(idx, i) A += i - idx d[c[i]] = i print(A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [int(i) for i in input().split()] ss = [0] for ai in a: ss.append(ss[-1] + ai) si = {(0): 0} left = 0 count = 0 for i in range(1, len(ss)): if ss[i] in si: left = max(left, si[ss[i]] + 1) si[ss[i]] = i count += i - left print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = lambda: sys.stdin.readline().strip() ipnut = input for i in range(1): n = int(ipnut()) a = list(map(int, input().split())) s = {} s[0] = 1 pref = [0] line = [] ans = 0 last = 0 for i in range(n): pref.append(pref[-1] + a[i]) if pref[-1] in s: last = max(last, s[pref[-1]]) if abs(a[i]): ans += i + 1 - last s[pref[-1]] = i + 2 print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) l = list(map(int, input().split())) x = [0] * (n + 1) d = dict() for i in range(n): x[i + 1] = x[i + 1] + x[i] + l[i] s = 0 c = 0 for i in range(n + 1): if x[i] in d: d[x[i]] += 1 else: d[x[i]] = 1 while d[x[i]] > 1: d[x[c]] -= 1 c = c + 1 s = s + (i - c + 1) print(s - n - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = lambda: sys.stdin.readline().rstrip() n = list(map(int, input().split()))[0] + 1 a = list(map(int, input().split())) s = [0] st = set() beg = 0 end = 0 ans = 0 for i in range(len(a)): s.append(a[i] + s[-1]) s.append(s[-1]) while True: if beg == n: print(ans) break if len(st) == end - beg: st.add(s[end]) end += 1 else: ans += end - beg - 2 if s[end - 1] != s[beg]: st.remove(s[beg]) beg += 1
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline n = int(input()) a = [int(item) for item in input().split()] cuma = [0] for item in a: cuma.append(cuma[-1] + item) dic = dict() dic[cuma[0]] = 0 right_most = -1 ans = 0 for i, item in enumerate(cuma): if i == 0: continue if item in dic: ans += i - max(dic[item], right_most) - 1 right_most = max(right_most, dic[item]) else: ans += i - right_most - 1 dic[item] = i print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def main(): n = int(input()) arr = list(map(int, input().split())) begin = 0 dp = [0] for i in range(1, n + 1): dp.append(dp[i - 1] + arr[i - 1]) end = 0 d = {(0): 1} ans = 0 while begin < n: while end < n and (d.get(dp[end + 1]) == None or d[dp[end + 1]] == 0): end += 1 d[dp[end]] = 1 ans += end - begin d[dp[begin]] = 0 begin += 1 print(ans) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NONE VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
k = {0} rez = 0 n = int(input()) a = input().split(" ") p = [0] for i in range(n): a[i] = int(a[i]) p.append(p[i] + a[i]) i = 0 j = 0 while i < n: while j < n and p[j + 1] not in k: j += 1 k.add(p[j]) rez += j - i k.remove(p[i]) i += 1 print(rez)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) nums = list(map(int, input().split())) pre_sum = [0] * len(nums) pre = 0 for idx, num in enumerate(nums): pre += num pre_sum[idx] = pre count = {} for idx, num in enumerate(pre_sum): if num != 0: count[num] = count.get(num, []) else: count[num] = count.get(num, [-1]) count[num].append(idx) intervals = [] for num in count: if len(count[num]) > 1: for i in range(len(count[num]) - 1): intervals.append((count[num][i] + 1, count[num][i + 1])) intervals.sort(key=lambda x: x[1]) cur_inter = 0 ans = 0 for i in range(n): while cur_inter < len(intervals) and i > intervals[cur_inter][0]: cur_inter += 1 if cur_inter == len(intervals): ans += n - i else: ans += intervals[cur_inter][1] - i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
t = int(input()) b = list(map(int, input().split())) s = 0 p = 0 q = 0 c = dict() for j in range(t): s += b[j] if s == 0 and q == 0: p += j + 1 - 1 c[0] = j q = 1 else: if s in c.keys(): k = c[s] q = max(q, k + 2) c[s] = j p += j + 1 - q print(p)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from sys import stdin input = stdin.readline R = lambda: list(map(int, input().split())) n, a = int(input()), R() mp, sm = {(0): -1}, 0 cc, pp = 0, 0 for i in range(n): sm += a[i] p = mp[sm] + 2 if sm in mp else 0 if a[i] == 0: p = i + 1 mp[sm] = i pp = max(pp, p) cc += i - pp + 1 print(cc)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from sys import gettrace, stdin if not gettrace(): def input(): return next(stdin)[:-1] def main(): n = int(input()) aa = [int(a) for a in input().split()] saa = [(0, 0)] * (n + 1) sa = 0 for i, a in enumerate(aa, 1): sa += a saa[i] = sa, i saa.sort() zranges = [] for i in range(n): if saa[i][0] == saa[i + 1][0]: zranges.append((saa[i][1], saa[i + 1][1])) zranges.sort() mines = [0] * n mine = n + 1 for i in range(n - 1, -1, -1): if zranges and zranges[-1][0] == i: mine = min(mine, zranges[-1][1]) zranges.pop() mines[i] = mine count = 0 for i in range(n): count += mines[i] - i - 1 print(count) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline a = int(input()) b = list(map(int, input().split())) c = [0] su = 0 for i in b: su += i c.append(su) ans = 0 thing = {} temp = 0 for i in range(a + 1): if i != 0: try: temp = min(i - thing[c[i]] - 1, temp + 1) ans += temp except: temp = min(i, temp + 1) ans += temp thing[c[i]] = i print(str(ans) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = [int(x) for x in input().split()] arr.insert(0, 0) mp = {(0): 0} sol = n * (n + 1) // 2 acum = 0 index = -1 for i in range(1, n + 1): acum += arr[i] if acum in mp: index = max(index, mp[acum]) mp[acum] = i sol -= index + 1 print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) tmp = 0 dic = {(0): 1} ans = 0 least = 0 for i, a in enumerate(A): tmp += a if a == 0: least = i + 1 continue elif tmp in dic: least = max(least, dic[tmp]) ans += i + 1 - least dic[tmp] = i + 2 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) nums = list(map(int, input().split())) prefixSum = [] for i in range(n): if i == 0: prefixSum.append(nums[0]) else: prefixSum.append(prefixSum[i - 1] + nums[i]) next_val = {} ans = 0 r_max = n - 1 for i in range(n - 1, -1, -1): next_val[prefixSum[i]] = i if i == 0: prefixSum[i - 1] = 0 if prefixSum[i - 1] in next_val: r_max = min(next_val[prefixSum[i - 1]] - 1, r_max) ans += r_max - i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from sys import stdin n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) ans = [0] * n sums = list() sums.append(0) for i in range(n): sums.append(sums[i] + a[i]) ans = 0 ri = 0 s = set() done = False for i in range(n): for j in range(ri, n + 1): if sums[j] in s: ri = j ans += ri - i - 1 s.remove(sums[i]) break s.add(sums[j]) if j == n: ri = j + 1 done = True if done: ans += ri - i - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solution(n, array): memo = {(0): 0} addptr = 0 maxptr = 0 ans = 0 for i in range(1, n + 1): addptr += array[i] if addptr in memo: maxptr = max(maxptr, memo[addptr] + 1) memo[addptr] = i ans += i - maxptr return ans print(solution(int(input()), [0] + list(map(int, input().split()))))
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = [int(x) for x in input().split()] for i in range(1, n): a[i] += a[i - 1] dic = {} dic[0] = 0 ans = 0 maximum = -1 for i in range(n): val = dic.get(a[i], -1) maximum = max(maximum, val) ans += i - maximum dic[a[i]] = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline n = int(input()) + 1 a = [0] + list(map(int, input().split())) def init_min(init_min_val): for i in range(n): seg_min[i + num_min - 1] = init_min_val[i] for i in range(num_min - 2, -1, -1): seg_min[i] = min(seg_min[2 * i + 1], seg_min[2 * i + 2]) def update_min(k, x): k += num_min - 1 seg_min[k] = x while k: k = (k - 1) // 2 seg_min[k] = min(seg_min[k * 2 + 1], seg_min[k * 2 + 2]) def query_min(p, q): if q <= p: return ide_ele_min p += num_min - 1 q += num_min - 2 res = ide_ele_min while q - p > 1: if p & 1 == 0: res = min(res, seg_min[p]) if q & 1 == 1: res = min(res, seg_min[q]) q -= 1 p = p // 2 q = (q - 1) // 2 if p == q: res = min(res, seg_min[p]) else: res = min(min(res, seg_min[p]), seg_min[q]) return res ide_ele_min = n num_min = 2 ** (n - 1).bit_length() seg_min = [ide_ele_min] * 2 * num_min d = dict() b = [0] * n for i in range(n): b[i] = b[i - 1] + a[i] for i in range(n): if a[i] == 0: update_min(i, i) d[b[i]] = i elif b[i] in d: update_min(d[b[i]] + 1, i) d[b[i]] = i res = 0 for i in range(n): res += query_min(i, n) - i print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline n = int(input()) L = list(map(int, input().split())) M = [0] for i in L: M.append(M[-1] + i) d = dict() cklist = [-1] * n ckd = -1 for i in range(n + 1): if M[i] not in d: d[M[i]] = [i] else: for j in range(ckd + 1, d[M[i]][-1] + 1): if cklist[j] == -1 or cklist[j] > i: cklist[j] = i ckd = d[M[i]][-1] d[M[i]].append(i) c = n * (n + 1) c //= 2 for i in range(n): if cklist[i] == -1: continue else: k = cklist[i] c -= n - k + 1 print(c)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = lambda: sys.stdin.readline().rstrip() def I(): return list(map(int, input().split())) n = I()[0] a = I() d = {(0): -1} last = -1 s = [a[0]] ans = 0 for i in range(1, n): s.append(a[i] + s[-1]) for i in range(len(s)): if s[i] in d: last = max(last, d[s[i]] + 1) d[s[i]] = i ans += i - last print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(a, n): s = 0 sums = {} sums[0] = 0 p = 0 ans = 0 subarrs = [] for x in range(n): s = s + a[x] if sums.get(s) == None: sums[s] = x + 1 else: if sums[s] + 1 >= p: subarrs.append((sums[s] + 1, x + 1)) p = sums[s] + 1 sums[s] = x + 1 p = 1 for x in range(len(subarrs)): ans = ans + (subarrs[x][0] - p + 1) * (n - subarrs[x][1] + 1) p = subarrs[x][0] + 1 return int(n * (n + 1) / 2) - ans n = int(input()) a = list(map(int, input().split())) print(solve(a, len(a)))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) l = list(map(int, input().split())) for i in range(1, len(l)): l[i] = l[i] + l[i - 1] count = 0 used = 0 d = {} d[0] = 0 for i in range(0, n): if l[i] in d: used = max(used, d[l[i]] + 1) count += i + 1 - used d[l[i]] = i + 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) ans = 0 st = {0} cum_a = 0 idx_l = 0 cum_A = [0] for idx_r, a in enumerate(A): cum_a += a cum_A.append(cum_a) while cum_a in st: st.remove(cum_A[idx_l]) idx_l += 1 st.add(cum_a) ans += idx_r - idx_l print(ans + N)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(a): n = len(a) prefix = [0] * (n + 1) for i in range(n): prefix[i + 1] = prefix[i] + a[i] begin, end = 0, 0 res = 0 s = set([0]) while begin < n: while end < n and prefix[end + 1] not in s: end += 1 s.add(prefix[end]) res += end - begin s.remove(prefix[begin]) begin += 1 return res n = int(input()) a = [int(i) for i in input().split()] print(solve(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) d = {} ar = list(map(int, input().split())) pref = [0] * n ans = 0 d = {} uk = -1 pref[0] = ar[0] d[0] = -1 for i in range(1, n): pref[i] = pref[i - 1] + ar[i] for i in range(n): if pref[i] in d: uk = max(uk, d.get(pref[i]) + 1) ans += i - uk d[pref[i]] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def solve(a, size): hash_map = {(0): 0} track_sum = 0 bad_comb = 0 total = 0 for i in range(1, size + 1): track_sum += a[i - 1] if track_sum in hash_map: bad_comb = max(bad_comb, hash_map[track_sum] + 1) elif track_sum == 0: bad_comb = max(bad_comb, 1) hash_map[track_sum] = i total += i - bad_comb return total size = int(input()) a = list(map(int, input().split())) print(solve(a, size))
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) array = [int(s) for s in input().split()] prefs = {} s = 0 left = 0 ans = 0 for i in range(1, len(array) + 1): s += array[i - 1] if array[i - 1] == 0: left = max(i, left) elif s == 0: if 0 not in prefs: left = max(1, left) else: left = max(1, prefs[0][-1] + 1) elif s in prefs: left = max(prefs[s][-1] + 1, left) if s in prefs: prefs[s].append(i) else: prefs[s] = [i] if array[i - 1] != 0: ans += i - left print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) lis = list(map(int, input().split())) pre = [0] * (n + 1) for i in range(1, n + 1): pre[i] = pre[i - 1] + lis[i - 1] d = {} ans = l = 0 for i in range(n + 1): if pre[i] not in d: d[pre[i]] = i else: l = max(l, d[pre[i]] + 1) d[pre[i]] = i ans += i - l print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) sp = list(map(int, input().split())) pref = [0] * (n + 1) for i in range(n): pref[1 + i] = pref[i] + sp[i] i = 1 while i < n + 1: if pref[i] != 0: break i += 1 if i == n + 1: print(0) exit() d = dict() d[pref[i]] = i ans = 1 d[0] = 0 proshn = i - 1 for j in range(i + 1, n + 1): if pref[j] == pref[j - 1]: proshn = j continue if pref[j] in d: ans += j - max(proshn, d[pref[j]] + 1) proshn = max(proshn, d[pref[j]] + 1) d[pref[j]] = j else: d[pref[j]] = j ans += j - max(i - 1, proshn) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def main(): n = int(input()) a = [int(i) for i in input().split(" ")] p = [0] * (n + 1) s = set() ans = 0 for i in range(n): p[i + 1] = p[i] + a[i] s.add(0) end = 0 for i in range(n): while end < n and p[end + 1] not in s: end += 1 s.add(p[end]) ans += end - i s.remove(p[i]) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) pref = [(0) for i in range(n + 1)] for i in range(n): pref[i + 1] = pref[i] + a[i] d = {} d[0] = 0 ans = 0 used = 0 for i in range(1, n + 1): if pref[i] in d: used = max(used, d[pref[i]] + 1) ans += i - used d[pref[i]] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) u = list(map(int, input().split())) d = dict() p = [] d[u[0]] = 0 if u[0] == 0: p.append((0, 0)) else: d[0] = -1 w = [0] * n for i in range(1, n): u[i] += u[i - 1] if u[i] in d.keys(): st, fn = d[u[i]] + 1, i p.append((st, fn)) w[fn] = len(p) d[u[i]] = i m = len(p) pp = [True] * m op = -1 for i in range(n): if w[i] == 0: continue if op == -1: op = w[i] elif p[w[i] - 1][0] < p[op - 1][0]: pp[w[i] - 1] = False else: op = w[i] px = [] for i in range(m): if pp[i]: px.append(p[i]) sm = n * (n + 1) // 2 px.append((n, n)) for i in range(len(px) - 1): sm -= (px[i][0] + 1) * (px[i + 1][1] - px[i][1]) print(sm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) s = 0 mi = -1 c = {(0): -1} su = 0 for i in range(n): if a[i] == 0: c = {(0): i} su = 0 mi = i else: su += a[i] if su in c: mi = max(mi, c[su] + 1) c[su] = i s += i - mi print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR DICT NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) aux = [0] * (n + 1) for i in range(n): aux[i + 1] = aux[i] + a[i] total = 0 l = 0 r = 0 s = set({}) d = {} while r <= n: if aux[r] in s and l <= d[aux[r]]: total += (r - l) * (r - l - 1) // 2 - (r - d[aux[r]] - 1) * ( r - d[aux[r]] - 2 ) // 2 l = d[aux[r]] + 1 d[aux[r]] = r else: s.add(aux[r]) d[aux[r]] = r r += 1 total += (r - l) * (r - l - 1) // 2 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = list(map(int, input().split(" "))) prefix_sum = [0] prefix_set = set() prefix_set.add(0) for i in range(len(arr)): prefix_sum.append(prefix_sum[i] + arr[i]) i, j, results = 0, 0, 0 while i < n: while j < n and prefix_sum[j + 1] not in prefix_set: j += 1 prefix_set.add(prefix_sum[j]) results += j - i prefix_set.remove(prefix_sum[i]) i += 1 print(results)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from sys import stdin n = int(stdin.readline()) cnt = 0 bad = -1 currsum = 0 suffixes = {(0): 0} for i, x in enumerate(map(int, stdin.readline().split())): currsum += x bad = max(bad, suffixes.get(currsum, -1)) cnt += i - bad suffixes[currsum] = i + 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) l = list(map(int, input().split())) s = set() s.add(0) pre = [0] for i in range(n): pre.append(pre[-1] + l[i]) i = 0 j = 0 ans = 0 while i < n: while j < n and pre[j + 1] not in s: j += 1 s.add(pre[j]) ans += j - i if pre[i] in s: s.remove(pre[i]) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) kk = dict() s = [int(x) for x in input().split()] L = [0] kk[0] = -1 rngs = [-1] * (n + 10) rnge = [-1] * (n + 10) c = 1 for i in range(0, len(s)): L.append(L[-1] + s[i]) if kk.get(L[-1]) != None: rngs[kk[L[-1]] + 1] = c rnge[i] = c c += 1 kk[L[-1]] = i ans = 0 ptr = 0 pos = dict() lst = 0 diff = 0 while ptr < len(s): if rngs[ptr] != -1: pos[rngs[ptr]] = ptr if rnge[ptr] != -1: if pos.get(rnge[ptr]) != None: tot = ptr - lst ans += tot * (tot + 1) // 2 tot2 = ptr - pos[rnge[ptr]] - 1 ans -= tot2 * (tot2 + 1) // 2 ptr = pos[rnge[ptr]] lst = ptr + 1 pos.clear() ptr += 1 tot = ptr - lst ans += tot * (tot + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = [int(i) for i in input().split()] for i in range(1, n): arr[i] += arr[i - 1] dic = {} dic[0] = [-1] for i in range(n): if arr[i] in dic: dic[arr[i]].append(i) else: dic[arr[i]] = [i] temp = [0] * n endly = {} for a in dic: if len(dic[a]) >= 2: for i in range(1, len(dic[a])): temp[dic[a][i]] = -1 if dic[a][i] in endly: endly[dic[a][i]].append(dic[a][i - 1] + 1) else: endly[dic[a][i]] = [dic[a][i - 1] + 1] l = 0 r = 0 count = 0 while r < n: if temp[r] == -1: l = max(max(endly[r]) + 1, l) count += r - l + 1 r += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) prefix = [0] * (n + 1) for i, x in enumerate(input().split()): x = int(x) prefix[i + 1] = prefix[i] + x begin = 0 end = 0 ans = 0 s = {0} while begin < n: while end < n and prefix[end + 1] not in s: end += 1 s.add(prefix[end]) ans += end - begin s.remove(prefix[begin]) begin += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) arr = [int(c) for c in input().split()] arr.insert(0, 0) sumsofar = 0 dic = {(0): 0} h = -1 ans = 0 for i in range(1, n + 1): sumsofar = sumsofar + arr[i] if sumsofar in dic: h = max(h, dic[sumsofar]) ans += i - h - 1 dic[sumsofar] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
import sys n = int(input()) l = list(map(int, input().split())) p = [0] * (n + 1) for i in range(1, n + 1): p[i] = p[i - 1] + l[i - 1] secik = set() pr = 0 le = 0 p += [p[-1]] n += 1 odp = 0 while True: if le == n: print(odp) break if len(secik) == pr - le: secik.add(p[pr]) pr += 1 else: odp += pr - le - 2 if p[le] != p[pr - 1]: secik.remove(p[le]) le += 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) a = list(map(int, input().split())) mp = dict() mp[0] = -1 pre = 0 mx = -1 ans = 0 for i in range(n): pre += a[i] ok = mp.get(pre, "Huh") if ok == "Huh": ans += i - mx mp[pre] = i else: if mx < ok + 1: mx = ok + 1 ans += i - mx mp[pre] = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def I(): return list(map(int, input().split())) def sieve(n): a = [1] * n for i in range(2, n): if a[i]: for j in range(i * i, n, i): a[j] = 0 return a n = int(input()) arr = I() l = -1 d = {(0): -1} prefix = [0] * (n + 1) prefix[0] = arr[0] ans = 0 for i in range(1, n): prefix[i] = arr[i] + prefix[i - 1] for i in range(n): l = max(l, d.get(prefix[i], l - 1) + 1) d[prefix[i]] = i ans += i - l print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
from itertools import accumulate n = int(input()) a = [int(x) for x in input().split()] sums = [0] + list(accumulate(a)) s = {0} i, j = 0, 0 ans = 0 while i < n: while j < n and sums[j + 1] not in s: s.add(sums[j + 1]) j += 1 ans += j - i s.remove(sums[i]) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
def sumoflength(arr, n): s = set() j = 0 ans = 0 for i in range(n): while j < n and arr[j] not in s: s.add(arr[j]) j += 1 ans += j - i - 1 s.remove(arr[i]) return ans n = int(input().strip()) numbers = list(map(int, input().strip().split())) prefsum = [(0) for i in range(n + 1)] prefsum[0] = 0 for q in range(1, n + 1): prefsum[q] = prefsum[q - 1] + numbers[q - 1] print(sumoflength(prefsum, n + 1))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$. Help Eugene to calculate the number of nonempty good subarrays of a given array $a$. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2 \times 10^5$)  — the length of array $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$)  — the elements of $a$. -----Output----- Output a single integer  — the number of good subarrays of $a$. -----Examples----- Input 3 1 2 -3 Output 5 Input 3 41 -41 41 Output 3 -----Note----- In the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$.
n = int(input()) ls = [int(x) for x in input().split()] presum = [0] for e in ls: presum.append(e + presum[-1]) height = {} ints = [] for i, s in enumerate(presum): if s not in height: height[s] = i - 1 else: ints.append((height[s], i - 1)) height[s] = i - 1 ints.append((len(ls), len(ls))) ints = sorted(ints, key=lambda x: x[1]) pos = 0 c = 0 for p in range(len(ls)): while p > ints[pos][0] + 1: pos += 1 c += ints[pos][1] - p print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR