- 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
- 66 lines of Go from the credited upstream file 279C.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 Sol279C(reader io.Reader, writer io.Writer) {11 in := bufio.NewReader(reader)12 out := bufio.NewWriter(writer)13 defer out.Flush()14 15 var n, q, l, r int16 Fscan(in, &n, &q)17 a := make([]int, n+2)18 sameL := make([]int, n+1)19 sameLI := 020 prev := make([]int, n+1)21 isAsc := true22 changeI := 023 for i := 1; i <= n; i++ {24 Fscan(in, &a[i])25 if a[i] != a[i-1] {26 sameLI = i27 }28 sameL[i] = sameLI29 if a[i-1] < a[i] && !isAsc {30 changeI = i - 131 isAsc = true32 } else if a[i-1] > a[i] && isAsc {33 changeI = i - 134 isAsc = false35 }36 prev[i] = changeI37 }38 39 next := make([]int, n+1)40 isAsc = false41 changeI = n + 142 for i := n; i >= 1; i-- {43 if a[i] < a[i+1] && !isAsc {44 changeI = i + 145 isAsc = true46 } else if a[i] > a[i+1] && isAsc {47 changeI = i + 148 isAsc = false49 }50 next[i] = changeI51 }52 53 for ; q > 0; q-- {54 Fscan(in, &l, &r)55 if next[l] >= r || prev[r] <= l || sameL[next[l]] == sameL[prev[r]] && a[next[l]] >= a[l] {56 Fprintln(out, "Yes")57 } else {58 Fprintln(out, "No")59 }60 }61}62 63646566