結果

問題 No.875 Range Mindex Query
ユーザー ccppjsrb
提出日時 2019-11-07 22:23:31
言語 Go
(1.13)
結果
AC  
実行時間 465 ms
コード長 3,290 Byte
コンパイル時間 4,674 ms
使用メモリ 11,296 KB
最終ジャッジ日時 2019-11-07 22:23:41

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample_01 AC 3 ms
7,068 KB
22_random_01 AC 5 ms
7,072 KB
22_random_02 AC 6 ms
7,072 KB
22_random_03 AC 4 ms
7,068 KB
22_random_04 AC 4 ms
7,072 KB
22_random_05 AC 4 ms
8,916 KB
22_random_06 AC 5 ms
7,072 KB
22_random_07 AC 5 ms
8,920 KB
22_random_08 AC 5 ms
7,068 KB
22_random_09 AC 5 ms
9,056 KB
22_random_10 AC 6 ms
8,920 KB
33_large_01 AC 279 ms
11,124 KB
33_large_02 AC 222 ms
9,144 KB
33_large_03 AC 184 ms
9,144 KB
33_large_04 AC 183 ms
9,144 KB
33_large_05 AC 255 ms
9,152 KB
43_max_01 AC 456 ms
11,292 KB
43_max_02 AC 443 ms
11,296 KB
43_max_03 AC 465 ms
11,296 KB
テストケース一括ダウンロード

ソースコード

diff #
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"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)

	seg := Segment{}
	seg.init(n)

	for i := 0; i < n; i++ {
		a := getNextInt(scanner)
		seg.minimize(i, a)
	}

	for i := 0; i < q; i++ {
		t := getNextInt(scanner)
		l := getNextInt(scanner) - 1
		r := getNextInt(scanner) - 1
		if t == 1 {
			ol := seg.bucket[0][l]
			or := seg.bucket[0][r]
			seg.minimize(l, or)
			seg.minimize(r, ol)
			continue
		}
		fmt.Fprintln(writer, seg.rangeMinIndex(l, r+1)+1)
	}
	writer.Flush()
}

// 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) minimize(index, value int) {
	seg.bucket[0][index] = value
	for seg.i = 0; seg.i < seg.h-1; seg.i++ {
		s := index - index%seg.chunk
		t := s + seg.chunk
		if t > len(seg.bucket[seg.i]) {
			t = len(seg.bucket[seg.i])
		}
		parent := index / seg.chunk
		min := seg.inf
		for i := s; i < t; i++ {
			if min > seg.bucket[seg.i][i] {
				min = seg.bucket[seg.i][i]
			}
		}
		seg.bucket[seg.i+1][parent] = min
		index /= seg.chunk
	}
}

func (seg *Segment) min(l, r int) int {
	if seg.n < r {
		r = seg.n
	}
	min := seg.inf
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		if min > seg.bucket[seg.i][index/seg.unit[seg.i]] {
			min = seg.bucket[seg.i][index/seg.unit[seg.i]]
		}
	}

	return min
}

func (seg *Segment) rangeMinIndex(l, r int) int {
	value := seg.min(l, r)
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
			if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] == value {
				break
			}
		}
		if seg.bucket[0][index] == value {
			return index
		}
	}
	return -1
}
0