結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー sepa38sepa38
提出日時 2023-11-23 16:05:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,451 ms / 5,000 ms
コード長 1,497 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 272,208 KB
最終ジャッジ日時 2024-09-26 08:11:33
合計ジャッジ時間 89,871 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

class segt:
  def __init__(self, n, calc):
    self.num = 2 ** (n - 1).bit_length()
    self.data = [[0]*53 for _ in range(2*self.num)]
    self.calc = calc

  def update(self, idx, x):
    idx += self.num - 1
    self.data[idx] = [0] * 53
    self.data[idx][x] = 1
    while idx > 0:
      idx = (idx - 1) >> 1
      self.data[idx] = self.calc(self.data[2*idx+1][:], self.data[2*idx+2][:])[:]

  def renew(self, idx, x):
    self.update(idx, self.calc(self.get(idx), x))

  def prod(self, left, right):
    l = left + self.num
    r = right + self.num
    res_l = [0] * 53
    res_r = [0] * 53
    while l < r:
      if l % 2:
        res_l = self.calc(res_l[:], self.data[l-1][:])[:]
        l += 1
      if r % 2:
        r -= 1
        res_r = self.calc(self.data[r-1][:], res_r[:])[:]
      l >>= 1
      r >>= 1
    return self.calc(res_l, res_r)

  def get(self, idx):
    return self.data[idx+self.num-1]

def func(x, y):
  res = [x[i] + y[i] for i in range(53)]
  s = sum(y[:26])
  for i in range(26):
    res[i+26] += x[i] * (s - y[i])
  for i in range(26):
    res[52] += x[i] * (x[i] - 1) // 2 * (s - y[i])
    res[52] += x[i] * y[i+26]

  return res


n = int(input())
s = list(map(lambda x: ord(x)-65, input()))
st = segt(n, func)
for i in range(n):
  st.update(i, s[i])
for _ in range(int(input())):
  t, x, y = map(str, input().split())
  if t == "1":
    x = int(x) - 1
    y = ord(y) - 65
    st.update(x, y)
  else:
    l = int(x) - 1
    r = int(y)
    print(st.prod(l, r)[52])
0