結果
問題 | No.878 Range High-Element Query |
ユーザー | ccppjsrb |
提出日時 | 2019-11-10 20:11:03 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,246 bytes |
コンパイル時間 | 18,012 ms |
コンパイル使用メモリ | 222,924 KB |
実行使用メモリ | 7,628 KB |
最終ジャッジ日時 | 2024-09-15 05:05:03 |
合計ジャッジ時間 | 16,480 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
ソースコード
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 }