結果
問題 | No.876 Range Compress Query |
ユーザー | sgsw |
提出日時 | 2021-08-12 23:03:36 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,872 bytes |
コンパイル時間 | 15,215 ms |
コンパイル使用メモリ | 225,832 KB |
実行使用メモリ | 30,208 KB |
最終ジャッジ日時 | 2024-10-02 05:17:56 |
合計ジャッジ時間 | 18,548 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | WA | - |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 3 ms
5,248 KB |
testcase_09 | AC | 3 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 640 ms
29,140 KB |
testcase_12 | AC | 532 ms
27,264 KB |
testcase_13 | AC | 540 ms
27,648 KB |
testcase_14 | AC | 632 ms
28,928 KB |
testcase_15 | AC | 434 ms
28,288 KB |
testcase_16 | AC | 606 ms
29,192 KB |
testcase_17 | AC | 602 ms
28,928 KB |
testcase_18 | AC | 655 ms
30,208 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 { num int val int } func (m monoid) get() interface{} { return m } func (x monoid) op(y Monoid) Monoid { a := x.get().(monoid) b := y.get().(monoid) return monoid{a.num + b.num, a.val + b.val} } func (m monoid) ide() Monoid { return monoid{0, 0} } func main() { n, q := nextInt(), nextInt() a := make([]int, n) for i := 0; i < n; i++ { a[i] = nextInt() } b := make([]Monoid, n) for i := 0; i < n; i++ { if i+1 < n { switch a[i+1] - a[i] { case 0: b[i] = monoid{0, 0} default: b[i] = monoid{a[i+1] - a[i], 1} } } else { b[i] = monoid{inf, 1} } } seg, _ := Gen(monoid{}, b) for i := 0; i < q; i++ { switch nextInt() { case 1: l, r, x := nextInt(), nextInt(), nextInt() l-- r-- if l-1 >= 0 { a := seg.Get(l - 1).(monoid) switch a.num + x { case 0: seg.Set(l-1, monoid{num: a.num + x, val: 0}) default: seg.Set(l-1, monoid{num: a.num + x, val: 1}) } } if r < n-1 { a := seg.Get(r).(monoid) switch a.num - x { case 0: seg.Set(r, monoid{a.num - x, 0}) default: seg.Set(r, monoid{a.num - x, 1}) } } case 2: l, r := nextInt(), nextInt() l-- ans := seg.Prod(l, r).(monoid).val //ans++ fmt.Printf("%d\n", ans) default: os.Exit(1) } } }