package main import ( "bufio" "fmt" "math" "os" "sort" "strconv" ) func getScanner(fp *os.File) *bufio.Scanner { scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 500001), 500000) return scanner } func getNextString(scanner *bufio.Scanner) string { scanner.Scan() return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextUint64(scanner *bufio.Scanner) uint64 { i, _ := strconv.ParseUint(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout if len(os.Args) > 1 { fp, _ = os.Open(os.Args[1]) if len(os.Args) > 2 { wfp, _ = os.Create(os.Args[2]) } } scanner := getScanner(fp) writer := bufio.NewWriter(wfp) n := getNextInt(scanner) q := getNextInt(scanner) aa := make(AA, n) qq := make(QQ, q) for i := 0; i < n; i++ { aa[i].a = getNextInt(scanner) aa[i].i = i } sort.Sort(aa) segl := Segment{} segl.init(n) ll := make(LL, n) for i := 0; i < n; i++ { ll[aa[i].i].l = segl.nextLeft(aa[i].i) ll[aa[i].i].i = aa[i].i segl.add(aa[i].i, 1) } sort.Sort(ll) for i := 0; i < q; i++ { qq[i].x = getNextInt(scanner) qq[i].l = getNextInt(scanner) - 1 qq[i].r = getNextInt(scanner) qq[i].i = i } sort.Sort(qq) seg := Segment{} seg.init(n) ai := 0 ans := make([]int, q) for i := 0; i < q; i++ { for ai < n && ll[ai].l < qq[i].l { seg.add(ll[ai].i, 1) ai++ } ans[qq[i].i] = seg.rangeSum(qq[i].l, qq[i].r) } for i := 0; i < q; i++ { fmt.Fprintln(writer, ans[i]) } writer.Flush() } type A struct { a, i int } type AA []A func (a AA) Len() int { return len(a) } func (a AA) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a AA) Less(i, j int) bool { return a[i].a > a[j].a } type Q struct { l, r, i, x int } type QQ []Q func (a QQ) Len() int { return len(a) } func (a QQ) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a QQ) Less(i, j int) bool { return a[i].l < a[j].l } type L struct { l, i int } type LL []L func (a LL) Len() int { return len(a) } func (a LL) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a LL) Less(i, j int) bool { return a[i].l < a[j].l } // Segment ... type Segment struct { n, h, i, chunk, inf int unit []int bucket [][]int } func (seg *Segment) init(n int) { seg.n = n seg.unit = make([]int, 1) seg.unit[0] = 1 seg.bucket = make([][]int, 1) seg.bucket[0] = make([]int, n) chunk := 8 for i := 0; n > 1; i++ { n = (n-1)/chunk + 1 seg.bucket = append(seg.bucket, make([]int, n)) seg.unit = append(seg.unit, seg.unit[i]*chunk) } seg.h = len(seg.unit) seg.chunk = chunk seg.inf = math.MaxInt32 } func (seg *Segment) add(index, value int) { for i := 0; i < seg.h; i++ { seg.bucket[i][index/seg.unit[i]] += value } } func (seg *Segment) rangeSum(l, r int) int { var sum int for i := l; i < r; i += seg.unit[seg.i] { for seg.i = 0; seg.i < seg.h-1 && i%seg.unit[seg.i+1] == 0; seg.i++ { if i+seg.unit[seg.i+1] >= r { break } } sum += seg.bucket[seg.i][i/seg.unit[seg.i]] } return sum } func (seg *Segment) nextLeft(index int) int { return seg.kthLeft(index, 1) } func (seg *Segment) kthLeft(index, k int) int { for index = index - 1; index >= 0; index -= seg.unit[seg.i] { for seg.i = 0; seg.i < seg.h-1 && (index+1)%seg.unit[seg.i+1] == 0; seg.i++ { if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] >= k { break } } k -= seg.bucket[seg.i][index/seg.unit[seg.i]] if k == 0 { return index } } return -1 }