Problem p02763 (python)

Fixed submission:
n=int(input())
s=input()
q=int(input())

query=[]
for i in range(q):
    query.append(input())

ide_ele = set()

def segfunc(x, y):
    return x | y

class SegTree:
    def __init__(self, init_val, segfunc, ide_ele):
        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] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        # param k: index(0-index)
        # param x: update value
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1
    def query(self, l, r):
        #[l, r)segfunc, 0-index
        res = self.ide_ele
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

al = [{s[i]} for i in range(n)]
seg = SegTree(al, segfunc, ide_ele)

for q in query:
    que,a,b=q.split()

    if que == "1":
        seg.update(int(a)-1,set(b))
    else:
        ret=seg.query(int(a)-1,int(b))
        print(len(ret))

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n = int(input())
s = input()
N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [""]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = ope(data[2*k+1],data[2*k+2])
def query(l, r):
    L = l + N0; R = r + N0
    s = ""
    while L < R:
        if R & 1:
            R -= 1
            s = ope(s, data[R-1])

        if L & 1:
            s = ope(s,data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s
  
def ope(x, y):
    r = []
    if type(x) is str and type(y) is str:
        r = [x, y]
    elif type(x) is str:
        r = [x] + y
    elif type(y) is str:
        r = x + [y]
    else:
        r = x + y

    return list(set(r))

for i in range(len(s)):
    update(i,s[i])

quenum = int(input())
ans=[]
for i in range(quenum):
    que, a, b = input().split()

    if que == "1":
        update(int(a)-1,b)
    else:
        ans.append(str(len(query(int(a)-1,int(b)))-1))
print("\n".join(ans))

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n = int(input())
s = input()
quenum = int(input())
q = []
for i in range(quenum):
    q.append(input().split())

N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [set()]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1]  | data[2*k+2]
def query(l, r):
    L = l + N0; R = r + N0
    s = set()
    while L < R:
        if R & 1:
            R -= 1
            s = s | data[R-1]

        if L & 1:
            s = s |  data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(len(s)):
    update(i,set([s[i]]))

for que in q:
    if que[0] == "1":
        update(int(que[1])-1,set([que[2]]))
    else:
        print(len(query(int(que[1])-1,int(que[2]))))

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n = int(input())
s = input()
quenum = int(input())
q = []
for i in range(quenum):
    q.append(input().split())

N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [set()]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1]  | data[2*k+2]
def query(l, r):
    L = l + N0; R = r + N0
    s = set()
    while L < R:
        if R & 1:
            R -= 1
            s = s | data[R-1]

        if L & 1:
            s = s |  data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(len(s)):
    update(i,set(s[i]))

for que in q:
    if que[0] == "1":
        update(int(que[1])-1,set(que[2]))
    else:
        print(len(query(int(que[1])-1,int(que[2]))))

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n = int(input())
s = input()
n = int(input())
q = []
for i in range(n):
    q.append(input().split())

N0 = 2**(n-1).bit_length()
INF = 2**31-1
data = [set()]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1]  | data[2*k+2]
def query(l, r):
    L = l + N0; R = r + N0
    s = set()
    while L < R:
        if R & 1:
            R -= 1
            s = s | data[R-1]

        if L & 1:
            s = s |  data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(len(s)):
    update(i,set(s[i]))

for que in q:
    if que[0] == "1":
        update(int(que[1])-1,set(que[2]))
    else:
        print(len(query(int(que[1])-1,int(que[2]))))