- 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
- 100 lines of Go from the credited upstream file 1363D.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 "os"7 "sort"8)9 10type (11 input1363 struct {12 n, k int13 a [][]int14 }15 guess1363 struct{ ans []int }16 qIn1363 struct{ q []int }17 qOut1363 struct{ max int }18)19 2021func CF1363D(in input1363, Q func(qIn1363) qOut1363) (gs guess1363) {22 n, k, a := in.n, in.k, in.a23 gs.ans = make([]int, k)24 q := []int{}25 for i := 1; i <= n; i++ {26 q = append(q, i)27 }28 allMax := Q(qIn1363{q}).max29 l, r := 0, k30 for r-l > 1 {31 m := (l + r) >> 132 q = []int{}33 for _, ids := range a[l:m] {34 q = append(q, ids...)35 }36 if Q(qIn1363{q}).max == allMax {37 for i := m; i < r; i++ {38 gs.ans[i] = allMax39 }40 r = m41 } else {42 for i := l; i < m; i++ {43 gs.ans[i] = allMax44 }45 l = m46 }47 }48 sort.Ints(a[l])49 q = []int{}50 for i, j := 1, 0; i <= n; i++ {51 if j == len(a[l]) || i != a[l][j] {52 q = append(q, i)53 } else {54 j++55 }56 }57 gs.ans[l] = Q(qIn1363{q}).max58 return59}60 61func ioq1363D() {62 in := bufio.NewReader(os.Stdin)63 out := bufio.NewWriter(os.Stdout)64 Q := func(qi qIn1363) (resp qOut1363) {65 Fprint(out, "? ", len(qi.q))66 for _, v := range qi.q {67 Fprint(out, " ", v)68 }69 Fprintln(out)70 out.Flush()71 Fscan(in, &resp.max)72 return73 }74 var t, c int75 for Fscan(in, &t); t > 0; t-- {76 d := input1363{}77 Fscan(in, &d.n, &d.k)78 a := make([][]int, d.k)79 for i := range a {80 Fscan(in, &c)81 a[i] = make([]int, c)82 for j := range a[i] {83 Fscan(in, &a[i][j])84 }85 }86 d.a = a87 gs := CF1363D(d, Q)88 Fprint(out, "!")89 for _, v := range gs.ans {90 Fprint(out, " ", v)91 }92 Fprintln(out)93 out.Flush()94 var s []byte95 Fscan(in, &s)96 }97}98 99100