- 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
- 64 lines of Go from the credited upstream file 1512E.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 CF1512E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var T, n, l, r, s int16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n, &l, &r, &s)18 l--19 k := r - l20 mi := (1 + k) * k / 221 mx := (2*n - k + 1) * k / 222 if s < mi || s > mx {23 Fprintln(out, -1)24 continue25 }26 27 a := make([]int, n)28 for i := l; i < r; i++ {29 a[i] = i - l + 130 }31 cur := mi32 for i := r - 1; i >= l && cur < s; i-- {33 d := r - 1 - i34 mx := n - d35 if cur+mx-a[i] >= s {36 a[i] += s - cur37 break38 }39 cur += mx - a[i]40 a[i] = mx41 }42 43 vis := make([]bool, n+1)44 for i := l; i < r; i++ {45 vis[a[i]] = true46 }47 cur = 148 for i, v := range a {49 if v == 0 {50 for ; vis[cur]; cur++ {51 }52 a[i] = cur53 cur++54 }55 }56 for _, v := range a {57 Fprint(out, v, " ")58 }59 Fprintln(out)60 }61}62 6364