結果

問題 No.878 Range High-Element Query
ユーザー ccppjsrbccppjsrb
提出日時 2019-11-10 20:41:44
言語 Go
(1.22.1)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 3,874 bytes
コンパイル時間 13,111 ms
コンパイル使用メモリ 222,532 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-09-15 05:05:24
合計ジャッジ時間 16,299 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 264 ms
9,856 KB
testcase_12 AC 181 ms
8,704 KB
testcase_13 AC 212 ms
8,448 KB
testcase_14 AC 155 ms
7,168 KB
testcase_15 AC 182 ms
8,448 KB
testcase_16 AC 287 ms
10,368 KB
testcase_17 AC 310 ms
10,752 KB
testcase_18 AC 308 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

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