- 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
- 103 lines of Go from the credited upstream file 348C.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 "math"8)9 1011func cf348C(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, m, q, k, x int15 var op string16 Fscan(in, &n, &m, &q)17 a := make([]int, n)18 for i := range a {19 Fscan(in, &a[i])20 }21 22 B := int(math.Sqrt(1e5 * 3))23 sets := make([][]int, m)24 iToBig := make([]int, m)25 bigSum := []int{}26 for i := range sets {27 Fscan(in, &k)28 sets[i] = make([]int, k)29 s := 030 for j := range sets[i] {31 Fscan(in, &sets[i][j])32 sets[i][j]--33 s += a[sets[i][j]]34 }35 if k > B {36 bigSum = append(bigSum, s)37 iToBig[i] = len(bigSum)38 }39 }40 41 nb := len(bigSum)42 intersectionSize := make([][]int32, m)43 for i := range intersectionSize {44 intersectionSize[i] = make([]int32, nb)45 }46 has := make([]bool, n)47 cur := 048 for _, big := range sets {49 if len(big) <= B {50 continue51 }52 for _, j := range big {53 has[j] = true54 }55 for i, st := range sets {56 for _, j := range st {57 if has[j] {58 intersectionSize[i][cur]++59 }60 }61 }62 cur++63 for _, j := range big {64 has[j] = false65 }66 }67 68 bigTodo := make([]int, nb)69 for range q {70 Fscan(in, &op, &k)71 k--72 st := sets[k]73 if op == "+" {74 Fscan(in, &x)75 if len(st) > B {76 bigTodo[iToBig[k]-1] += x77 } else {78 for _, i := range st {79 a[i] += x80 }81 for j, sz := range intersectionSize[k] {82 bigSum[j] += x * int(sz)83 }84 }85 } else {86 s := 087 if len(st) > B {88 s = bigSum[iToBig[k]-1]89 } else {90 for _, i := range st {91 s += a[i]92 }93 }94 for j, sz := range intersectionSize[k] {95 s += bigTodo[j] * int(sz)96 }97 Fprintln(out, s)98 }99 }100}101 102103