Approach
Sorting and greedy selection
For Codeforces 762E — Radio stations, 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 762E.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 cf762E(in io.Reader, out io.Writer) {12 var n, k, ans int13 Fscan(in, &n, &k)14 type tuple struct{ x, r, f 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].f)19 g[a[i].f] = append(g[a[i].f], 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 f, xs := range g {25 slices.Sort(xs)26 tree[f] = make([]int, len(xs)+1)27 }28 add := func(f, i int) {29 t := tree[f]30 for i = sort.SearchInts(g[f], i) + 1; i < len(t); i += i & -i {31 t[i]++32 }33 }34 pre := func(f, i int) (res int) {35 t := tree[f]36 for i = sort.SearchInts(g[f], i); i > 0; i &= i - 1 {37 res += t[i]38 }39 return40 }41 42 for _, p := range a {43 for f := p.f - k; f <= p.f+k; f++ {44 ans += pre(f, p.x+p.r+1) - pre(f, p.x-p.r)45 }46 add(p.f, p.x)47 }48 Fprint(out, ans)49}50 5152