Approach
Sorting and greedy selection
For Codeforces 1045G — AI robots, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 52 lines of Go from the credited upstream file 1045G.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 . "fmt"5 "io"6 "slices"7 "sort"8)9 1011func cf1045G(in io.Reader, out io.Writer) {12 var n, k, ans int13 Fscan(in, &n, &k)14 type tuple struct{ x, r, iq int }15 a := make([]tuple, n)16 g := map[int][]int{}17 for i := range a {18 Fscan(in, &a[i].x, &a[i].r, &a[i].iq)19 g[a[i].iq] = append(g[a[i].iq], a[i].x)20 }21 slices.SortFunc(a, func(a, b tuple) int { return b.r - a.r })22 23 tree := map[int][]int{}24 for iq, xs := range g {25 slices.Sort(xs)26 tree[iq] = make([]int, len(xs)+1)27 }28 add := func(iq, i int) {29 t := tree[iq]30 for i = sort.SearchInts(g[iq], i) + 1; i < len(t); i += i & -i {31 t[i]++32 }33 }34 pre := func(iq, i int) (res int) {35 t := tree[iq]36 for i = sort.SearchInts(g[iq], i); i > 0; i &= i - 1 {37 res += t[i]38 }39 return40 }41 42 for _, p := range a {43 for iq := p.iq - k; iq <= p.iq+k; iq++ {44 ans += pre(iq, p.x+p.r+1) - pre(iq, p.x-p.r)45 }46 add(p.iq, p.x)47 }48 Fprint(out, ans)49}50 5152