結果
問題 | No.1115 二つの数列 / Two Sequences |
ユーザー | ccppjsrb |
提出日時 | 2020-07-17 21:51:37 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 87 ms / 2,000 ms |
コード長 | 2,395 bytes |
コンパイル時間 | 15,779 ms |
コンパイル使用メモリ | 239,556 KB |
実行使用メモリ | 11,648 KB |
最終ジャッジ日時 | 2024-11-29 23:21:22 |
合計ジャッジ時間 | 18,622 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 73 ms
11,136 KB |
testcase_04 | AC | 85 ms
11,648 KB |
testcase_05 | AC | 73 ms
11,136 KB |
testcase_06 | AC | 70 ms
10,752 KB |
testcase_07 | AC | 84 ms
11,520 KB |
testcase_08 | AC | 4 ms
5,248 KB |
testcase_09 | AC | 47 ms
9,600 KB |
testcase_10 | AC | 74 ms
11,648 KB |
testcase_11 | AC | 1 ms
5,248 KB |
testcase_12 | AC | 86 ms
11,648 KB |
testcase_13 | AC | 86 ms
11,648 KB |
testcase_14 | AC | 87 ms
11,648 KB |
testcase_15 | AC | 1 ms
5,248 KB |
testcase_16 | AC | 1 ms
5,248 KB |
testcase_17 | AC | 1 ms
5,248 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 1 ms
5,248 KB |
testcase_20 | AC | 1 ms
5,248 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 1 ms
5,248 KB |
testcase_23 | AC | 6 ms
5,248 KB |
testcase_24 | AC | 29 ms
6,016 KB |
testcase_25 | AC | 67 ms
10,496 KB |
testcase_26 | AC | 12 ms
5,248 KB |
testcase_27 | AC | 33 ms
6,528 KB |
testcase_28 | AC | 53 ms
9,728 KB |
testcase_29 | AC | 72 ms
10,752 KB |
testcase_30 | AC | 79 ms
11,392 KB |
testcase_31 | AC | 17 ms
5,248 KB |
testcase_32 | AC | 13 ms
5,248 KB |
testcase_33 | AC | 63 ms
11,136 KB |
testcase_34 | AC | 1 ms
5,248 KB |
testcase_35 | AC | 1 ms
5,248 KB |
testcase_36 | AC | 1 ms
5,248 KB |
testcase_37 | AC | 1 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 1 ms
5,248 KB |
ソースコード
package main import ( "bufio" "fmt" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } 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 getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) aa := make([]int, n) bb := make([]int, n) ii := map[int]int{} seg := newSegment(n) for i := 0; i < n; i++ { seg.update(i, 1, seg.add) } for i := 0; i < n; i++ { aa[i] = getNextInt(scanner) ii[aa[i]] = i } var ans int for i := 0; i < n; i++ { bb[i] = getNextInt(scanner) ans += seg.query(0, ii[bb[i]], 0, seg.add) seg.update(ii[bb[i]], 0, seg.add) } fmt.Fprintln(writer, ans) } type segment [][]int func newSegment(n int) segment { seg := make(segment, 0) for n > 1 { seg = append(seg, make([]int, n)) n = (n + 1) >> 1 } seg = append(seg, make([]int, 1)) return seg } func (seg segment) update(i int, v int, f func(x, y int) int) { seg[0][i] = v for h := 1; h < len(seg); h++ { i >>= 1 if i<<1+1 < len(seg[h-1]) { seg[h][i] = f(seg[h-1][i<<1], seg[h-1][i<<1+1]) } else { seg[h][i] = seg[h-1][i<<1] } } } func (seg segment) query(l, r int, s int, f func(x, y int) int) int { for h := 0; h < len(seg) && l < r; h++ { if l&1 == 1 { s = f(s, seg[h][l]) l++ } l >>= 1 if r&1 == 1 { s = f(s, seg[h][r-1]) } r >>= 1 } return s } func (seg segment) add(x, y int) int { return x + y }