結果

問題 No.878 Range High-Element Query
ユーザー ccppjsrb
提出日時 2019-11-10 20:11:03
言語 Go
(1.13)
結果
WA   .
実行時間 -
コード長 3,246 Byte
コンパイル時間 4,060 ms
使用メモリ 11,296 KB
最終ジャッジ日時 2019-11-10 20:11:15

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample_01 AC 2 ms
7,084 KB
22_random_01 WA -
22_random_02 WA -
22_random_03 WA -
22_random_04 WA -
22_random_05 WA -
22_random_06 WA -
22_random_07 WA -
22_random_08 WA -
22_random_09 WA -
22_random_10 WA -
33_large_01 WA -
33_large_02 WA -
33_large_03 WA -
33_large_04 WA -
33_large_05 WA -
43_max_01 WA -
43_max_02 WA -
43_max_03 WA -
テストケース一括ダウンロード

ソースコード

diff #
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)

	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([]int, n)
	for i := 0; i < n; i++ {
		ll[aa[i].i] = segl.nextLeft(aa[i].i)
		segl.add(aa[i].i, 1)
	}

	for i := 0; i < q; i++ {
		x := getNextInt(scanner)
		if x != 1 {
			continue
		}
		li := getNextInt(scanner) - 1
		ri := getNextInt(scanner)
		l := li
		r := ri
		for l < r {
			m := (l + r) >> 1
			if ll[m] < li {
				l = m + 1
				continue
			}
			r = m
		}
		fmt.Fprintln(writer, r-li)
	}

	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 }

// 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
}
0