提出 #54386590
ソースコード 拡げる
END { load __FILE__ unless $stdin.eof? }
class Elem
attr_accessor :ab, :a, :b, :l
def initialize(a = 0, b = 0)
@m = 998_244_353
@ab = (a * b) % @m
@a = a
@b = b
@l = 1
end
def op(x, y)
@ab = (x.ab + y.ab) % @m
@a = (x.a + y.a) % @m
@b = (x.b + y.b) % @m
@l = (x.l + y.l) % @m
self
end
def id
@ab = 0
@a = 0
@b = 0
@l = 0
self
end
def map(f)
fx = f.x
fy = f.y
@ab = (@ab + @a * fy + @b * fx + @l * (fx * fy % @m)) % @m
@a = (@a + @l * fx) % @m
@b = (@b + @l * fy) % @m
self
end
end
class Func
attr_accessor :x, :y
def initialize(x = 0, y = 0)
@m = 998_244_353
@x = x
@y = y
end
def composite(f)
@x = (f.x + @x) % @m
@y = (f.y + @y) % @m
self
end
def id
@x = 0
@y = 0
self
end
end
module AtCoder
class SegmentTree
def initialize(ary_or_size = 0)
ary =
if ary_or_size.kind_of?(Integer)
Array.new(ary_or_size) { id }
else
ary_or_size.to_a
end
@n = ary.size
@log = @n.pred.bit_length
@size = 1 << @log
@ary = Array.new(@size * 2) { id }
@ary[@size, @n] = ary
@size.pred.downto(1) { |i| update(i) }
@sml0 = id
@smr0 = id
end
def op(x, y, z)
z.op(x, y)
end
def id(z = Elem.new)
z.id
end
def []=(idx, x)
idx += @size
@ary[idx] = x
1.upto(@log) { |i| update(idx >> i) }
x
end
def [](idx)
@ary[idx + @size]
end
def prod(l, r)
sml = id(@sml0)
smr = id(@smr0)
l += @size
r += @size
while l < r
(sml = op(sml, @ary[l], sml); l += 1) if l.odd?
(r -= 1; smr = op(@ary[r], smr, smr)) if r.odd?
l >>= 1
r >>= 1
end
op(sml, smr, sml)
end
def all_prod
@ary[1]
end
private
def update(k)
@ary[k] = op(@ary[2 * k], @ary[2 * k + 1], @ary[k])
end
end
class LazySegmentTree < SegmentTree
def initialize(ary_or_size = 0)
super
@lz = Array.new(@size) { id_func }
end
def mapping(f, x)
x.map(f)
end
def composition(f, g)
g.composite(f)
end
def id_func(f = Func.new)
f.id
end
def []=(idx, x)
push_all(idx)
super
end
def [](idx)
push_all(idx)
super
end
def prod(l, r)
return id if l == r
l2, r2 = l, r
l += @size
r += @size
@log.downto(1) do |i|
m = -1[0, i]
push(l >> i) if l.anybits?(m)
push(r >> i) if r.anybits?(m)
end
super(l2, r2)
end
def apply(l, r = l + 1, f)
case r - l
when 0
return self
when 1
push_all(l)
self[l] = mapping(f, self[l])
return self
end
l += @size
r += @size
q = r - 1
@log.downto(1) do |i|
m = -1[0, i]
push(l >> i) if l.anybits?(m)
push(q >> i) if r.anybits?(m)
end
l2, r2 = l, r
while l < r
(all_apply(l, f); l += 1) if l.odd?
(r -= 1; all_apply(r, f)) if r.odd?
l >>= 1
r >>= 1
end
l, r = l2, r2
1.upto(@log) do |i|
m = -1[0, i]
update(l >> i) if l.anybits?(m)
update(q >> i) if r.anybits?(m)
end
self
end
def apply_all(f)
all_apply(1, f)
self
end
private
def all_apply(k, f)
@ary[k] = mapping(f, @ary[k])
@lz[k] = composition(f, @lz[k]) if k < @size
end
def push(k)
all_apply(2 * k, @lz[k])
all_apply(2 * k + 1, @lz[k])
@lz[k] = id_func(@lz[k])
end
def push_all(idx)
idx += @size
@log.downto(1) { |i| push(idx >> i) }
end
end
end
n, q = gets.split.map!(&:to_i)
a = gets.split.map!(&:to_i)
b = gets.split.map!(&:to_i)
ary = Array.new(n) { |i| Elem.new(a[i], b[i]) }
st = AtCoder::LazySegmentTree.new(ary)
f = Func.new
q.times do
t, l, r, x = gets.split.map!(&:to_i)
l -= 1
case t
when 1
f.x = x
f.y = 0
st.apply(l, r, f)
when 2
f.x = 0
f.y = x
st.apply(l, r, f)
when 3
puts st.prod(l, r).ab
end
end
__END__
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
550 / 550 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
example_00.txt, example_01.txt |
| All |
example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| example_00.txt |
AC |
46 ms |
17660 KiB |
| example_01.txt |
AC |
43 ms |
17252 KiB |
| hand_00.txt |
AC |
2118 ms |
107672 KiB |
| hand_01.txt |
AC |
2119 ms |
107332 KiB |
| hand_02.txt |
AC |
3859 ms |
107484 KiB |
| hand_03.txt |
AC |
4166 ms |
108376 KiB |
| hand_04.txt |
AC |
1801 ms |
105456 KiB |
| hand_05.txt |
AC |
2027 ms |
106904 KiB |
| random_00.txt |
AC |
329 ms |
17780 KiB |
| random_01.txt |
AC |
329 ms |
17628 KiB |
| random_02.txt |
AC |
426 ms |
18024 KiB |
| random_03.txt |
AC |
427 ms |
17820 KiB |
| random_04.txt |
AC |
426 ms |
17908 KiB |
| random_05.txt |
AC |
4738 ms |
107448 KiB |
| random_06.txt |
AC |
4752 ms |
107300 KiB |
| random_07.txt |
AC |
4772 ms |
107420 KiB |
| random_08.txt |
AC |
4741 ms |
108416 KiB |
| random_09.txt |
AC |
4726 ms |
107456 KiB |
| random_10.txt |
AC |
4751 ms |
107484 KiB |
| random_11.txt |
AC |
4748 ms |
107420 KiB |
| random_12.txt |
AC |
4753 ms |
107432 KiB |
| random_13.txt |
AC |
4760 ms |
107392 KiB |
| random_14.txt |
AC |
4852 ms |
107328 KiB |
| random_15.txt |
AC |
4763 ms |
107392 KiB |
| random_16.txt |
AC |
4883 ms |
107528 KiB |
| random_17.txt |
AC |
4766 ms |
107416 KiB |
| random_18.txt |
AC |
4740 ms |
107436 KiB |
| random_19.txt |
AC |
4789 ms |
107368 KiB |
| random_20.txt |
AC |
4734 ms |
108400 KiB |
| random_21.txt |
AC |
4733 ms |
107440 KiB |
| random_22.txt |
AC |
4780 ms |
107380 KiB |
| random_23.txt |
AC |
4746 ms |
107328 KiB |
| random_24.txt |
AC |
4760 ms |
107224 KiB |
| random_25.txt |
AC |
4753 ms |
107288 KiB |
| random_26.txt |
AC |
4778 ms |
107440 KiB |
| random_27.txt |
AC |
4752 ms |
107360 KiB |
| random_28.txt |
AC |
4863 ms |
107540 KiB |
| random_29.txt |
AC |
4716 ms |
107428 KiB |