結果

問題 No.230 Splarraay スプラレェーイ
ユーザー tyawanmusityawanmusi
提出日時 2020-08-29 03:10:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 710 ms / 5,000 ms
コード長 3,572 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 103,536 KB
最終ジャッジ日時 2024-11-14 04:58:48
合計ジャッジ時間 6,941 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,504 KB
testcase_01 AC 40 ms
53,532 KB
testcase_02 AC 39 ms
53,288 KB
testcase_03 AC 40 ms
54,300 KB
testcase_04 AC 39 ms
53,724 KB
testcase_05 AC 103 ms
76,552 KB
testcase_06 AC 208 ms
78,512 KB
testcase_07 AC 98 ms
76,176 KB
testcase_08 AC 153 ms
77,300 KB
testcase_09 AC 428 ms
88,840 KB
testcase_10 AC 403 ms
78,544 KB
testcase_11 AC 332 ms
83,836 KB
testcase_12 AC 429 ms
88,672 KB
testcase_13 AC 237 ms
79,172 KB
testcase_14 AC 218 ms
84,068 KB
testcase_15 AC 482 ms
86,420 KB
testcase_16 AC 541 ms
95,828 KB
testcase_17 AC 710 ms
103,536 KB
testcase_18 AC 384 ms
88,316 KB
testcase_19 AC 514 ms
97,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

# SegmentTree
class SegmentTree:

  def __init__(self, n, p, unit, f, g, h):
    num = 2**((n-1).bit_length())
    seg = [unit]*(num*2)
    self.lazy = [None]*(num*2)
    for i in range(n):
      seg[num+i] = p[i]
    for i in range(num-1, 0, -1):
      seg[i] = f(seg[i << 1], seg[(i << 1)+1])
    self.num = num
    self.seg = seg
    self.unit = unit
    self.flag = False
    self.f = f
    self.g = g
    self.h = h

  def gindex(self, l, r):
    l += self.num
    r += self.num
    lm = (l//(l & -l)) >> 1
    rm = (r//(r & -r)) >> 1
    mm = max(lm, rm)
    r -= 1
    while l < r:
      if r <= rm:
        yield r
      if l <= lm:
        yield l
      l >>= 1
      r >>= 1
    while l:
      if l <= mm:
        yield l
      l >>= 1

  def propagates(self, ids):
    num = self.num
    g = self.g
    h = self.h
    for i in reversed(ids):
      v = self.lazy[i]
      if v is None:
        continue
      # ここ!!!!!!!!!!!!!
      # ここ!!!!!!!!!!!!!

      newv = (v[0]//2,v[1]//2)

      # ここ!!!!!!!!!!!!!
      # ここ!!!!!!!!!!!!!
      if (i << 1) < num:
        self.lazy[i << 1] = h(self.lazy[i << 1], newv)
        self.lazy[(i << 1)+1] = h(self.lazy[(i << 1)+1], newv)
      self.seg[i << 1] = g(self.seg[i << 1], newv)
      self.seg[(i << 1)+1] = g(self.seg[(i << 1)+1], newv)
      self.lazy[i] = None

  def query(self, l, r):
    f = self.f
    if self.flag:
      *ids, = self.gindex(l, r)
      self.propagates(ids)
    ansl = ansr = self.unit
    l += self.num
    r += self.num-1
    if l == r:
      return self.seg[l]
    while l < r:
      if l & 1:
        ansl = f(ansl, self.seg[l])
        l += 1
      if r & 1 == 0:
        ansr = f(self.seg[r], ansr)
        r -= 1
      l >>= 1
      r >>= 1
    if l == r:
      ansl = f(ansl, self.seg[l])
    return f(ansl, ansr)

  def update1(self, i, x):
    i += self.num
    f = self.f
    self.seg[i] = x
    while i:
      i >>= 1
      self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1])

  def update2(self, l, r, x):
    self.flag = True
    *ids, = self.gindex(l, r)
    self.propagates(ids)
    num = self.num
    f = self.f
    g = self.g
    h = self.h
    l += num
    r += num-1
    if l == r:
      self.seg[l] = g(self.seg[l], x)
      for i in ids:
        self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1])
      return
    while l < r:
      if l & 1:
        if l < num:
          self.lazy[l] = h(self.lazy[l], x)
        self.seg[l] = g(self.seg[l], x)
        l += 1
      if r & 1 == 0:
        if r < num:
          self.lazy[r] = h(self.lazy[r], x)
        self.seg[r] = g(self.seg[r], x)
        r -= 1
      l >>= 1
      r >>= 1
      x = f(x, x)
    if l == r:
      self.lazy[l] = h(self.lazy[l], x)
      self.seg[l] = g(self.seg[l], x)
    for i in ids:
      self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1])
  
  def update(self, i, x):
    if type(i) is int:
      self.update1(i, x)
    else:
      self.update2(i[0], i[1], x)

n=int(input())
q=int(input())
f=lambda x,y: (x[0]+y[0],x[1]+y[1])
g=lambda x,y: y
h=lambda x,y: y
seg=SegmentTree(n,[(0,0)]*n,(0,0),f,g,h)
ans1=ans2=0
for _ in range(q):
  qq,s,t=map(int,input().split())
  t+=1
  if qq==0:
    x1,x2=seg.query(s,t)
    if x1<x2:
      ans2+=x2
    if x1>x2:
      ans1+=x1
  elif qq==1:
    seg.update((s,t),(1,0))
  else:
    seg.update((s,t),(0,1))
x1,x2=seg.query(0,n)
ans1+=x1
ans2+=x2
print(ans1,ans2)
0