Problem p02763 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
S = input().strip()
Q = int(input())
n = 1
while n < N:
    n *= 2
T = [0] * (2 * n)

def update(k, a):
    k += n-1
    T[k] = 1 << ord(a) - 97
    while k > 0:
        k = (k - 1) // 2
        T[k] = T[k*2+1] | T[k*2+2]

def query(a, b, k, l, r):
    # print(a, b, k, l, r)
    if r <= a or b <= l:
        # print('  ', set())
        return 0
    if a <= l and r <= b:
        # print('  ', T[k])
        return T[k]
    else:
        vl = query(a, b, k*2+1, l, (l+r)//2)
        vr = query(a, b, k*2+2, (l+r)//2, r)
        return vl | vr

for i in range(N):
    update(i, S[i])

for q in range(Q):
    f, i, c = [x for x in input().strip().split()]
    if f == '1':
        # print(T,T[int(i)-1])
        update(int(i)-1, c)
    elif f == '2':
        # print(int(i)-1, int(c)-1)
        tmp = query(int(i)-1, int(c), 0, 0, n)
        ans = 0
        for i in range(26):
            ans += tmp >> i & 1
         
        print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
S = input().strip()
Q = int(input())
T = [set()] * (2 * N)
n = 1
while n < N:
    n *= 2

def update(k, a):
    k += n-1
    T[k] = {a}
    while k > 0:
        k = (k - 1) // 2
        T[k] = T[k*2] ^ T[k*2+1]

def query(a, b, k, l, r):
    # print(a, b, k, l, r)
    if r <= a or b <= l:
        # print('  ', set())
        return set()
    if a <= l and r <= b:
        # print('  ', T[k])
        return T[k]
    else:
        vl = query(a, b, k*2+1, l, (l+r)//2)
        vr = query(a, b, k*2+2, (l+r)//2, r)
        return vl | vr

for i in range(N):
    update(i, S[i])

for q in range(Q):
    f, i, c = [x for x in input().strip().split()]
    if f == '1':
        # print(T,T[int(i)-1])
        update(int(i)-1, c)
    elif f == '2':
        # print(int(i)-1, int(c)-1)
        print(len(query(int(i)-1, int(c), 0, 0, n)))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
S = input().strip()
Q = int(input())
T = [()] * (2 * N)

n = 1
while n < N:
    n *= 2

def update(k, a):
    k += n-1
    T[k] = tuple(a)
    while k > 0:
        k = (k - 1) // 2
        T[k] = T[k*2+1] + T[k*2+2]

def query(a, b, k, l, r):
    if r <= a or b <= l:
        return ()
    if a <= l and r <= b:
        return T[k]
    else:
        vl = query(a, b, k*2+1, l, (l+r)//2)
        vr = query(a, b, k*2+2, (l+r)//2, r)
        return vl+vr

for i in range(n-1, n-1+N):
    update(i-n-1, S[i-n-1])

for q in range(Q):
    f, i, c = [x for x in input().strip().split()]
    if f == '1':
        update(int(i)-n-1, c)
    elif f == '2':
        print(len(query(int(i), int(c)-1, 0, 0, n))+1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
S = input().strip()
Q = int(input())
T = [()] * (2 * N)

n = 1
while n < N:
    n *= 2

def update(k, a):
    k += n-1
    T[k] = tuple(a)
    while k > 0:
        k = (k - 1) // 2
        T[k] = T[k*2+1] + T[k*2+2]

def query(a, b, k, l, r):
    if r <= a or b <= l:
        return ()
    if a <= l and r <= b:
        return T[k]
    else:
        vl = query(a, b, k*2+1, l, (l+r)//2)
        vr = query(a, b, k*2+2, (l+r)//2, r)
        return vl+vr

for i in range(n-1, n-1+N):
    update(i-n-1, S[i-n-1])

for q in range(Q):
    f, i, c = [x for x in input().strip().split()]
    if f == '1':
        update(int(i)-n-1, c)
    elif f == '2':
        print(len(query(int(i), int(c), 0, 0, n))+1)