結果
問題 | No.877 Range ReLU Query |
ユーザー | ccppjsrb |
提出日時 | 2019-11-09 19:47:51 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 568 ms / 2,000 ms |
コード長 | 3,536 bytes |
コンパイル時間 | 14,349 ms |
コンパイル使用メモリ | 222,376 KB |
実行使用メモリ | 9,984 KB |
最終ジャッジ日時 | 2024-11-08 10:24:02 |
合計ジャッジ時間 | 20,404 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 3 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 440 ms
9,472 KB |
testcase_12 | AC | 378 ms
8,704 KB |
testcase_13 | AC | 301 ms
7,552 KB |
testcase_14 | AC | 323 ms
7,680 KB |
testcase_15 | AC | 454 ms
9,600 KB |
testcase_16 | AC | 478 ms
9,472 KB |
testcase_17 | AC | 491 ms
9,728 KB |
testcase_18 | AC | 488 ms
9,728 KB |
testcase_19 | AC | 285 ms
9,984 KB |
testcase_20 | AC | 568 ms
9,728 KB |
ソースコード
package main import ( "bufio" "fmt" "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) seg := Segment{} seg.init(n) seg2 := Segment{} seg2.init(n) aa := make(AA, n) qq := make(QQ, q) for i := 0; i < n; i++ { aa[i].a = getNextInt64(scanner) aa[i].i = i } for i := 0; i < q; i++ { t := getNextInt(scanner) l := getNextInt(scanner) - 1 r := getNextInt(scanner) x := getNextInt64(scanner) if t == 1 { qq[i].i = i qq[i].l = l qq[i].r = r qq[i].x = x } } sort.Sort(aa) sort.Sort(qq) ai := 0 ans := make([]int64, q) for i := 0; i < q; i++ { for ai < n && aa[ai].a > qq[i].x { seg.add(aa[ai].i, aa[ai].a) seg2.add(aa[ai].i, 1) ai++ } ans[qq[i].i] = seg.rangeSum(qq[i].l, qq[i].r) - seg2.rangeSum(qq[i].l, qq[i].r)*qq[i].x } for i := 0; i < q; i++ { fmt.Fprintln(writer, ans[i]) } writer.Flush() } type A struct { a int64 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 { i, l, r int x int64 } 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].x > a[j].x } // Segment ... type Segment struct { n, h, i, chunk int unit []int bucket [][]int64 } func (seg *Segment) init(n int) { seg.n = n seg.unit = make([]int, 1) seg.unit[0] = 1 seg.bucket = make([][]int64, 1) seg.bucket[0] = make([]int64, n) chunk := 8 for i := 0; n > 1; i++ { n = (n-1)/chunk + 1 seg.bucket = append(seg.bucket, make([]int64, n)) seg.unit = append(seg.unit, seg.unit[i]*chunk) } seg.h = len(seg.unit) seg.chunk = chunk } func (seg *Segment) rangeAdd(l, r int, value int64) { 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 } } seg.bucket[seg.i][index/seg.unit[seg.i]] += value } } func (seg *Segment) add(index int, value int64) { for i := 0; i < seg.h; i++ { seg.bucket[i][index/seg.unit[i]] += value } } func (seg *Segment) rangeSum(l, r int) int64 { var value int64 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 } } value += seg.bucket[seg.i][index/seg.unit[seg.i]] } return value }