- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 81 lines of Go from the credited upstream file 1333C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF1333C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 var n, v, l int13 var ans, s int6414 sumP := map[int64]int{0: 0}15 Fscan(in, &n)16 for i := 1; i <= n; i++ {17 Fscan(in, &v)18 s += int64(v)19 if p, ok := sumP[s]; ok && p+1 > l { 20 l = p + 121 }22 ans += int64(i - l)23 sumP[s] = i24 }25 Fprint(_w, ans)26}27 2829func CF1333C2(_r io.Reader, _w io.Writer) {30 ans := int64(0)31 var f func([]int64)32 f = func(a []int64) {33 n := len(a)34 if n == 1 {35 if a[0] != 0 {36 ans++37 }38 return39 }40 m := n >> 141 f(a[:m])42 f(a[m:])43 sumPos := map[int64]int{}44 s := int64(0)45 maxPos := m46 for i := m - 1; i >= 0 && a[i] != 0; i-- {47 s += a[i]48 if s == 0 || sumPos[s] != 0 {49 break50 }51 sumPos[s] = i52 maxPos = i53 }54 s = 055 vis := map[int64]bool{}56 for i := m; maxPos < m && i < n && a[i] != 0; i++ {57 s += a[i]58 if s == 0 || vis[s] {59 break60 }61 vis[s] = true62 if p, ok := sumPos[-s]; ok && p+1 > maxPos {63 maxPos = p + 164 }65 ans += int64(m - maxPos)66 }67 }68 69 in := bufio.NewReader(_r)70 var n int71 Fscan(in, &n)72 a := make([]int64, n)73 for i := range a {74 Fscan(in, &a[i])75 }76 f(a)77 Fprint(_w, ans)78}79 8081