- 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
- 82 lines of Go from the credited upstream file 1194E.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 "sort"8)9 1011func CF1194E(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 min := func(a, b int) int {14 if a < b {15 return a16 }17 return b18 }19 max := func(a, b int) int {20 if a > b {21 return a22 }23 return b24 }25 const mx = 500126 type line struct{ p, l, r int }27 28 var n, x1, y1, x2, y2 int29 var hs, vs []line30 for Fscan(in, &n); n > 0; n-- {31 Fscan(in, &x1, &y1, &x2, &y2)32 x1 += mx33 y1 += mx34 x2 += mx35 y2 += mx36 if y1 == y2 {37 if x1 > x2 {38 x1, x2 = x2, x139 }40 hs = append(hs, line{y1, x1, x2})41 } else {42 if y1 > y2 {43 y1, y2 = y2, y144 }45 vs = append(vs, line{x1, y1, y2})46 }47 }48 sort.Slice(hs, func(i, j int) bool { return hs[i].p < hs[j].p })49 sort.Slice(vs, func(i, j int) bool { return vs[i].l < vs[j].l })50 51 ans := int64(0)52 for i, p := range hs {53 tree := [mx * 2]int{}54 add := func(i int) {55 for ; i < mx*2; i += i & -i {56 tree[i]++57 }58 }59 sum := func(i int) (res int) {60 for ; i > 0; i &= i - 1 {61 res += tree[i]62 }63 return64 }65 j := 066 for _, q := range hs[:i] {67 for ; j < len(vs) && vs[j].l <= q.p; j++ {68 if vs[j].r >= p.p {69 add(vs[j].p)70 }71 }72 if l, r := max(q.l, p.l), min(q.r, p.r); l <= r {73 c := sum(r) - sum(l-1)74 ans += int64(c * (c - 1) / 2)75 }76 }77 }78 Fprint(out, ans)79}80 8182