結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2024-03-09 16:55:09
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,724 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 849,284 KB
最終ジャッジ日時 2024-03-09 16:55:22
合計ジャッジ時間 13,367 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
70,984 KB
testcase_01 AC 74 ms
74,216 KB
testcase_02 AC 170 ms
80,976 KB
testcase_03 AC 239 ms
83,112 KB
testcase_04 AC 284 ms
96,320 KB
testcase_05 AC 198 ms
93,376 KB
testcase_06 AC 190 ms
85,680 KB
testcase_07 AC 186 ms
81,960 KB
testcase_08 AC 126 ms
86,252 KB
testcase_09 AC 220 ms
94,784 KB
testcase_10 AC 218 ms
91,980 KB
testcase_11 AC 150 ms
82,180 KB
testcase_12 AC 1,226 ms
272,648 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# import pypyjit
# pypyjit.set_param("max_unroll_recursion=-1")
from collections import *
from functools import *
from itertools import *
from heapq import *
import sys, math,random,time
# input = sys.stdin.readline

class SegTree:
    """
    Segment Tree
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        初期化

        init_val: 配列の初期値
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新

        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            tk = k>>1
            self.tree[tk] = self.segfunc(self.tree[tk<<1], self.tree[(tk<<1)+1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る

        l: index(0-index)
        r: index(0-index)
        """
        res_l = self.ide_ele
        res_r = self.ide_ele
        
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res_l = self.segfunc(res_l, self.tree[l])
                l += 1
            if r & 1:
                res_r = self.segfunc(self.tree[r - 1], res_r)
            l >>= 1
            r >>= 1
        res = self.segfunc(res_l,res_r)
        return res
    
N = int(input())
S = list(input())
S = [ord(s)-ord('A') for s in S]
Q = int(input())
M = 26
ide_ele = tuple([0]*(M*(M+1)+1))
L = M*(M+1)+1
def segfunc(x,y):
    z = [0]*(M*(M+1)+1)
    for i in range(L):
        z[i] = x[i]+y[i]
    for i in range(M):
        for j in range(M):
            if i!=j:
                z[-1] = (z[-1] 
                     + x[i]*y[M*(i+1)+j] 
                     + x[M*(i+1)+i]*y[j])
            z[M*(i+1)+j] = z[M*(i+1)+j] + x[i]*y[j]
    return tuple(z)

I = []
for s in S:
    X = [0]*(M*(M+1)+1)
    X[s] = 1
    I.append(tuple(X))

T = SegTree(I,segfunc,ide_ele)
for _ in range(Q):
    query = tuple(input().split())
    if query[0]=='1':
        x,c = query[1:]
        x = int(x) - 1
        c = ord(c) - ord('A')
        X = [0]*(M*(M+1)+1)
        X[c] = 1
        T.update(x,tuple(X))
    else:
        l,r = map(int,query[1:])
        l -= 1
        print(T.query(l,r)[-1])
        # print(l,r,sum(T.query(l,r)))
# print(T.query(0,2))
0