結果
問題 | No.875 Range Mindex Query |
ユーザー | sgsw |
提出日時 | 2021-08-12 22:08:44 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 592 ms / 2,000 ms |
コード長 | 3,522 bytes |
コンパイル時間 | 11,665 ms |
コンパイル使用メモリ | 233,568 KB |
実行使用メモリ | 31,380 KB |
最終ジャッジ日時 | 2024-10-02 03:30:06 |
合計ジャッジ時間 | 17,535 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 3 ms
6,820 KB |
testcase_02 | AC | 3 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,824 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 3 ms
6,820 KB |
testcase_08 | AC | 2 ms
6,816 KB |
testcase_09 | AC | 3 ms
6,820 KB |
testcase_10 | AC | 4 ms
6,820 KB |
testcase_11 | AC | 592 ms
31,316 KB |
testcase_12 | AC | 459 ms
20,740 KB |
testcase_13 | AC | 401 ms
29,204 KB |
testcase_14 | AC | 381 ms
29,176 KB |
testcase_15 | AC | 570 ms
31,380 KB |
testcase_16 | AC | 503 ms
27,236 KB |
testcase_17 | AC | 547 ms
29,280 KB |
testcase_18 | AC | 530 ms
27,220 KB |
ソースコード
package main import ( "bufio" "fmt" "os" "strconv" ) var wg = bufio.NewScanner(os.Stdin) const ( inf = int(1e18) initialBufSize = int(1e6) maxBufSize = int(1e6) ) var buf []byte = make([]byte, initialBufSize) // greatest common divisor (GCD) via Euclidean algorithm func GCD(a, b int) int { for b != 0 { t := b b = a % b a = t } return a } func init() { wg.Split(bufio.ScanWords) wg.Buffer(buf, maxBufSize) } func nextInt() int { wg.Scan() i, e := strconv.Atoi(wg.Text()) if e != nil { panic(e) } return i } /* Monoid-Structure should be decleared some-where else. -> op(Monoid,Monoid)Monoid & ide()Monoid is required. */ type Monoid interface { get() interface{} op(Monoid) Monoid ide() Monoid } type SegTree struct { op func(Monoid) Monoid /* op */ e func() Monoid /*identity*/ d []Monoid _d []Monoid n int /* size*/ log int size int } func Gen(monoid Monoid, n interface{}) (SegTree, bool) { seg := SegTree{op: monoid.op, e: monoid.ide, d: []Monoid{}, _d: []Monoid{}, n: 0, log: 0, size: 0} collect := true switch T := n.(type) { case int: seg.d = make([]Monoid, n.(int)) for i := range seg.d { seg.d[i] = seg.e() } case []Monoid: _ = T seg.d = make([]Monoid, len(n.([]Monoid))) copy(seg.d, n.([]Monoid)) default: collect = false return seg, collect } seg.n = len(seg.d) for ord := 0; (1 << ord) < seg.n; { ord++ seg.log = ord } seg.size = 1 << seg.log seg._d = make([]Monoid, 2*seg.size) for i := range seg._d { seg._d[i] = seg.e() } for i := 0; i < seg.n; i++ { seg._d[seg.size+i] = seg.d[i] } for i := seg.size - 1; i > 0; i-- { seg._update(i) } return seg, collect } func (seg SegTree) _update(k int) { seg._d[k] = seg._d[2*k].op(seg._d[2*k+1]) } func (seg SegTree) Set(p int, x Monoid) { //a[p] -> x p += seg.size seg._d[p] = x for i := 1; i <= seg.log; i++ { seg._update(p >> i) } } func (seg SegTree) Get(p int) Monoid { // a[p] return seg._d[p+seg.size] } func (seg SegTree) OverWrite(p int, f func(Monoid) Monoid) { p += seg.size m := seg._d[p] seg._d[p] = f(m) for i := 1; i <= seg.log; i++ { seg._update(p >> i) } } func (seg SegTree) Prod(l, r int) Monoid { //op(a[l..r)) sml := seg.e() smr := seg.e() l += seg.size r += seg.size for l < r { if l&1 == 1 { sml = sml.op(seg._d[l]) l++ } if r&1 == 1 { r-- smr = seg._d[r].op(smr) } l >>= 1 r >>= 1 } return sml.op(smr) } func (seg SegTree) All_prod() Monoid { return seg._d[1] } //独自のモノイド定義 type monoid struct { val int idx int } func (m monoid) get() interface{} { return m } func (x monoid) op(y Monoid) Monoid { a := x.get().(monoid) b := y.get().(monoid) if a.val < b.val { return a } else if a.val > b.val { return b } else { if a.idx < b.idx { return monoid{a.val, a.idx} } else { return monoid{a.val, b.idx} } } } func (m monoid) ide() Monoid { return monoid{val: inf, idx: 0} } func main() { n, q := nextInt(), nextInt() a := make([]Monoid, n) for i := 0; i < n; i++ { x := nextInt() a[i] = monoid{val: x, idx: i} } seg, _ := Gen(monoid{}, a) for i := 0; i < q; i++ { switch nextInt() % 2 { case 0: l, r := nextInt(), nextInt() l-- ans := seg.Prod(l, r).(monoid).idx ans++ fmt.Printf("%d\n", ans) case 1: l, r := nextInt(), nextInt() l-- r-- a := seg.Get(l).(monoid).val b := seg.Get(r).(monoid).val seg.Set(l, monoid{b, l}) seg.Set(r, monoid{a, r}) } } }