Problem p02763 (python)

Fixed submission:
def ctoi(c):
  return ord(c)-97

n=int(input())
s=list(input())

bits=[[0]*(n+1) for _ in range(26)]

def query(pos):
  ret=[0]*26
  for i in range(26):
    tmp=0
    p=pos
    while p>0:
      tmp+=bits[i][p]
      p-=p&(-p)
    ret[i]=tmp
  return ret

def update(pos,a,x):
  while pos<=n:
    bits[a][pos]+=x
    pos+=pos&(-pos)

for i in range(n):
  update(i+1,ctoi(s[i]),1)

q=int(input())
for _ in range(q):
  flag,a,b=map(str,input().split())
  flag=int(flag)
  a=int(a)
  if flag==1:
    if s[a-1]==b:
      continue
    else:
      update(a,ctoi(s[a-1]),-1)
      update(a,ctoi(b),1)
      s[a-1]=b
  elif flag==2:
    b=int(b)
    cnt1=query(b)
    cnt2=query(a-1)
    ans=0
    for i in range(26):
      if cnt1[i]-cnt2[i]>0:
        ans+=1
    print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**((n-1).bit_length())
st=[[0]*(2*num) for _ in range(26)]

def update(pos,a,x):
  pos+=num
  st[a][pos]=x
  while pos>=0:
    pos=(pos-1)//2
    st[a][pos]=st[a][2*pos+1]|st[a][2*pos+2]
    
def query(l,r):
  ret=0
  for a in range(26):
    tl=l+num
    tr=r+num
    tmp=0
    while tl<tr:
      if tr&1==1:
        tr-=1
        tmp=max(tmp,st[a][tr-1])
      if tl&1==1:
        tmp=max(tmp,st[a][tl-1])
        tl+=1
      tl>>=1
      tr>>=1
    if tmp!=0:
      tmp=1
    ret+=tmp
  return ret

for i in range(n):
  update(i,ord(s[i])-97,1)

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)-1
  if a==1:
    if c==s[b]:
      continue
    update(b,ord(s[b])-97,0)
    update(b,ord(c)-97,1)
    s[b]=c
  elif a==2:
    c=int(c)-1
    print(query(b,c+2))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**((n-1).bit_length())
st=[[0]*(2*num) for _ in range(26)]

def update(pos,a,x):
  pos+=num
  st[a][pos]=x
  while pos>=0:
    pos=(pos-1)//2
    st[a][pos]=st[a][2*pos+1]|st[a][2*pos+2]
    
def query(l,r):
  ret=0
  for a in range(26):
    tl=l+num
    tr=r+num
    tmp=0
    while tl<tr:
      if tr&1==1:
        tr-=1
        tmp=max(tmp,st[a][tr-1])
      if tl&1==1:
        tmp=max(tmp,st[a][tl-1])
        tl+=1
      tl>>=1
      tr>>=1
    if tmp!=0:
      tmp=1
    ret+=tmp
  return ret

for i in range(n):
  update(i,ord(s[i])-97,1)

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)-1
  if a==1:
    if c==s[b]:
      continue
    update(b,ord(s[b])-97,0)
    update(b,ord(c)-97,1)
    s[b]=c
  elif a==2:
    c=int(c)-1
    print(query(b,c+2))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**((n-1).bit_length())
st=[[0]*(2*num) for _ in range(26)]

def update(pos,a,x):
  pos+=num
  st[a][pos]=x
  while pos>=0:
    pos=(pos-1)//2
    st[a][pos]=max(st[a][2*pos+1],st[a][2*pos+2])
    
def query(l,r):
  ret=0
  for a in range(26):
    tl=l+num
    tr=r+num
    tmp=0
    while tl<tr:
      if tr&1==1:
        tr-=1
        tmp=max(tmp,st[a][tr-1])
      if tl&1==1:
        tmp=max(tmp,st[a][tl-1])
        tl+=1
      tl>>=1
      tr>>=1
    if tmp!=0:
      tmp=1
    ret+=tmp
  return ret

for i in range(n):
  update(i,ord(s[i])-97,1)

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)-1
  if a==1:
    if c==s[b]:
      continue
    update(b,ord(s[b])-97,0)
    update(b,ord(c)-97,1)
    s[b]=c
  elif a==2:
    c=int(c)-1
    print(query(b,c+2))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**((n-1).bit_length())
st=[[0]*(2*num) for _ in range(26)]

def update(pos,a,x):
  pos+=num
  st[a][pos]=x
  while pos>=0:
    pos=(pos-1)//2
    st[a][pos]=max(st[a][2*pos+1],st[a][2*pos+2])
    
def query(l,r):
  ret=0
  for a in range(26):
    tl=l+num
    tr=r+num
    tmp=0
    while tl<tr:
      if tr&1==1:
        tr-=1
        tmp=max(tmp,st[a][tr-1])
      if tl&1==1:
        tmp=max(tmp,st[a][tl-1])
        tl+=1
      tl>>=1
      tr>>=1
    if tmp!=0:
      tmp=1
    print(a,tmp)
    ret+=tmp
  return ret

for i in range(n):
  update(i,ord(s[i])-97,1)

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)-1
  if a==1:
    if c==s[b]:
      continue
    update(b,ord(s[b])-97,0)
    update(b,ord(c)-97,1)
    s[b]=c
  elif a==2:
    c=int(c)-1
    print(query(b,c+2))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**(n-1).bit_length()
st=[set() for _ in range(2*num)]

def update1(pos,x):
  pos+=num
  st[pos].add(x)
  while pos>=0:
    pos=(pos-1)//2
    st[pos]=st[2*pos+1]|st[2*pos+2]
    
def update2(pos,x):
  pos+=num
  st[pos].discard(x)
  while pos>=0:
    pos=(pos-1)//2
    st[pos]=st[2*pos+1]|st[2*pos+2]
    st[pos].discard(x)
    
def query(l,r):
  tl=l+num
  tr=r+num
  ret=set()
  while tl<tr:
    if tr&1==1:
      tr-=1
      ret=ret|st[tr-1]
    if tl&1==1:
      ret=ret|st[tl-1]
      tl+=1
    tl=tl//2
    tr=tr//2
  return len(ret)

for i in range(n):
  update1(i,s[i])

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)-1
  if a==1:
    if c==s[b]:
      continue
    update2(b,s[b])
    s[b-1]=c
    update1(b,c)
    print(st)
  elif a==2:
    c=int(c)-1
    print(query(b,c))

Buggy submission (verdict: Runtime Error):
n=int(input())
s=list(input())

num=2**(n-1).bit_length()
st=[set() for _ in range(2*num)]

def update1(pos,x):
  pos+=num
  st[pos].add(x)
  while pos>=0:
    pos=(pos-1)//2
    st[pos]=st[2*pos+1]|st[2*pos+2]
    
def update2(pos,x):
  pos+=num
  st[pos].discard(x)
  while pos>=0:
    pos=(pos-1)//2
    st[pos]=st[2*pos+1]|st[2*pos+2]
    st[pos].discard(x)
    
def query(l,r):
  tl=l+num
  tr=r+num
  ret=set()
  while tl<tr:
    if tr&1==1:
      tr-=1
      ret=ret|st[tr-1]
    if tl&1==1:
      ret=ret|st[tl-1]
      tl+=1
    tl=tl//2
    tr=tr//2
  return len(ret)

for i in range(n):
  update1(i,s[i])

q=int(input())
for _ in range(q):
  a,b,c=map(str,input().split())
  a=int(a)
  b=int(b)
  if a==1:
    if c==s[b-1]:
      continue
    update2(b,s[b-1])
    s[b-1]=c
    update1(b,c)
  elif a==2:
    c=int(c)
    print(query(b,c+1))