Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02744 (python)

Fixed submission:
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
#N, M = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
#S = list(input())

def append_str(W, N):
    w_new = []
    for w in W:
        kind = len(set(w))
        for i in range(kind+1):
            w_new.append(w+alphabets[i])
    return w_new
    

seed = 'a'
alphabets = ['a', 'b','c','d','e','f','g','h','i','j']

#長さNの文字列に含まれる文字の種類はN以下
for n in range(N):
    if n == 0:
        w = [seed]
    else:
        w = append_str(w, n+1)
    
for buf in w:
    print(buf)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
#N, M = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
#S = list(input())

def append_str(W, N):
    w_new = []
    for w in W:
        kind = len(set(w))
        for i in range(kind+1):
            w_new.append(w+alphabets[i])
    return w_new
    

seed = 'a'
alphabets = ['a', 'b','c','d','e','f','g','h','i','j']

#長さNの文字列に含まれる文字の種類はN以下
for n in range(N):
    if n == 0:
        w = [seed]
    else:
        w = append_str(w, n+1)
    
for buf in w:
    print(buf)
Problem p03103 (python)

Fixed submission:
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
N, M = [int(x) for x in stdin.readline().rstrip().split()]
#N = int(input())
#S = list(input())

costs = []
num = []

for n in range(N):
    ai, bi = [int(x) for x in stdin.readline().rstrip().split()]
    costs += [ai]
    num += [bi]
    
sorted_idx = np.argsort(costs)
costs = list(np.array(costs)[sorted_idx])
num = list(np.array(num)[sorted_idx])

count_num = 0
count_cost = 0
for i, n in enumerate(num):
    if count_num + n < M:
        count_cost += n*costs[i]
        count_num += n
    else:
        count_cost += costs[i]*(M-count_num)
        break

print(count_cost)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
N, M = [int(x) for x in stdin.readline().rstrip().split()]
#N = int(input())
#S = list(input())

costs = []
num = []

#is_exist = 'Yes'
for n in range(N):
    ai, bi = [int(x) for x in stdin.readline().rstrip().split()]
    costs += [ai]
    num += [bi]
    
sorted_idx = np.argsort(costs)
costs = list(np.array(costs)[sorted_idx])
num = list(np.array(num)[sorted_idx])

count_num = 0
count_cost = 0
for i, n in enumerate(num):
    if count_num + n < M:
        count_cost += n*costs[i]
        count_num += n
    else:
        count_cost += costs[i]*(M-count_num)
        break

print(count_cost)
Problem p02606 (python)

Fixed submission:
L,R,d=input().split()

count=0
for i in range(int(L),int(R)+1,1):
    if i%int(d)==0:
        count+=1

print(count)

Buggy submission (verdict: Runtime Error):
n = int(input())
ans = [0 for _ in range(10050)]
for i in range(1,105):
	for j in range(1,105):
		for k in range(1,105):
			v = i*i+j*j+k*k+i*j+j*k+k*i
			if v<10050:
				ans[v]+=1
for i in range(n):
	print(ans[i+1])

Buggy submission (verdict: Runtime Error):
n = int(input())
ans = [0 for _ in range(10050)]
for i in range(1,105):
	for j in range(1,105):
		for k in range(1,105):
			v = i*i+j*j+k*k+i*j+j*k+k*i;
			if v<10050:
				ans[v]+=1
for i in range(n):
	print(ans[i+1])
Problem p02640 (python)

Fixed submission:
x, y = map(int, input().split())

a=b=0


if x*4<y or x*2>y:
    print('No')
    exit()

if (y-2*x)%2==0:
    n=(y-2*x)/2
    if x-b>=0:
        print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import sympy

x, y = map(int, input().split())

a=b=0


if x*4<y or x*2>y:
    print('No')
    exit()

if (y-2*x)%2==0:
    n=(y-2*x)/2
    if x-b>=0:
        print('Yes')

Buggy submission (verdict: Runtime Error):
import sympy

x, y = map(int, input().split())

a=b=0


if x*4<y:
    print('No')
    exit()

if (y-2*x)%2==0:
    n=(y-2*x)/2
    if x-b>=0:
        print('Yes')
Problem p02687 (python)

Fixed submission:
str=input()

if str=='ABC':
  print('ARC')
elif str=='ARC':
  print('ABC')

Buggy submission (verdict: Runtime Error):
import itertools

N, M = input().split()
N = int(N)
M = int(M)

h = [int(i) for i in input().split()]

List = [list(map(int, input().split())) for i in range(M)]
f_list=list(itertools.chain.from_iterable(List))

cnt=0


for i in range(1,N+1):
  if f_list.count(i)==0:
    cnt+=1


ToF = [0] * N


for i in range(0, M):
    tmp1 = List[i][1] - 1
    tmp2 = List[i][0] - 1
    diff = h[tmp1] - h[tmp2]
    if diff > 0:
        ToF[List[i][1] - 1] = 1

print(ToF.count(1)+cnt)
Problem p02705 (python)

Fixed submission:
import math

def circle_round(R):
 return 2 * math.pi * R

if __name__ == '__main__':
  R = int(input())
  print(circle_round(R))

Buggy submission (verdict: Runtime Error):
import numpy as np
 
 
if __name__ == '__main__':
    List = [list(map(int, input().split())) for i in range(2)]
    N = List[0][0]
    buka = np.array(List[1][:])
 
 
    l_out = np.zeros(N,dtype=int)
 
    for i in range(N-1):
        l_out[buka[i]-1] += 1
 
    for i in range(N):
        print(l_out[i])
Problem p02582 (python)

Fixed submission:
S=input()
if S=="SSS":
  print(0)
elif S=="RSS":
  print(1)
elif S=="RRS":
  print(2)
elif S=="RRR":
  print(3)
elif S=="SRS":
  print(1)
elif S=="SRR":
  print(2)
elif S=="RSR":
  print(1)
elif S=="SSR":
  print(1)

Buggy submission (verdict: Runtime Error):
S=input()
if S="SSS":
  print(0)
elif S="RSS":
  print(1)
elif S="RRS":
  print(2)
elif S="RRR":
  print(3)
elif S="SRS":
  print(1)
elif S="SRR":
  print(2)
elif S="RSR":
  print(1)
elif S="SSR":
  print(1)

Buggy submission (verdict: Runtime Error):
S=input()
if S="SSS":
  return 0
elif S="RSS":
  return 1
elif S="RRS":
  return 2
elif S="RRR":
  return 3
elif S="SRS":
  return 1
elif S="SRR":
  return 2
elif S="RSR":
  return 1
elif S="SSR":
  return 1

Buggy submission (verdict: Runtime Error):
S=input()
if S="sss":
  return 0
elif S="RSS":
  return 1
elif S="RRS":
  return 2
elif S="RRR":
  return 3
elif S="SRS":
  return 1
elif S="SRR":
  return 2
elif S="RSR":
  return 1
elif S="SSR":
  return 1
Problem p03455 (python)

Fixed submission:
a,b=map(int,input().split())

if a*b%2==0:
    print("Even")
else:
    print("Odd")

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
if a*b%2==0:
  print(Even)
else:
  print(Odd)

Buggy submission (verdict: Runtime Error):
a=int(input("整数aを入力してください："))
b=int(input("整数bを入力してください："))
 
if (a*b)%2==0:
  print("Even")
else:
  print("Odd")

Buggy submission (verdict: Runtime Error):
a=int(print("整数aを入力してください："))
b=int(print("整数bを入力してください："))

if (a*b)%2==0:
  print("Even")
else:
  print("Odd")
Problem p02613 (python)

Fixed submission:
n = int(input())
list = [0,0,0,0]

for i in range(n):
  judge = input()
  if judge == 'AC':
    list[0] += 1
  elif judge == 'WA':
    list[1] += 1
  elif judge == 'TLE':
    list[2] += 1
  elif judge == 'RE':
    list[3] += 1
    
print( 'AC x {}'.format(list[0]) )
print( 'WA x {}'.format(list[1]) )
print( 'TLE x {}'.format(list[2]) )
print( 'RE x {}'.format(list[3]) )

Buggy submission (verdict: Runtime Error):
n = int(input())
list = [0,0,0,0]

for i in range(n):
  judge = input()
  if judge === 'AC':
    list[0] += 1
  elif judge === 'WA':
    list[1] += 1
  elif judge === 'TLE':
    list[2] += 1
  elif judge === 'RE':
    list[3] += 1

print( 'AC x {}'.format(list[0]) )
print( 'WA x {}'.format(list[1]) )
print( 'TLE x {}'.format(list[2]) )
print( 'RE x {}'.format(list[3]) )
Problem p02582 (python)

Fixed submission:
x, y ,z = input()
if  x == "S" and y == "S" and z == "S":
    print(0)
elif x =="S" and y=="R" and z=="R":
    print(2)
elif x=="R" and y=="R" and z=="S":
    print(2)
elif x=="R" and y=="R" and z=="R":
    print(3)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
code = input()

if code == SSS:
    print(0)
elif code == SSR or SRS or RSS or RSR:
    print(1)
elif code == SRR or RRS:
    print(2)
else:
    print(3)

Buggy submission (verdict: Runtime Error):
code = input()

if code == SSS:
    print(0)
elif code == SSR or SRS or RSS or RSR:
    print(1)
elif code == SRR or RRS:
    print(2)
else:
    print(3)
Problem p02720 (python)

Fixed submission:
k = int(input())

a = [[str(i) for i in range(1,10)]]
c = 9

if k <= 12 :
    print(k)
else :
    for i in range(10) :
        b = []
        for s in a[i] :
            if s[-1] == '0' :
                b.append(s+'0')
                b.append(s+'1')
            elif s[-1] == '9' :
                b.append(s+'8')
                b.append(s+'9')
            else :
                b.append(s+str(int(s[-1])-1))
                b.append(s+str(int(s[-1])))
                b.append(s+str(int(s[-1])+1))

            if c + len(b) > k :
                print(b[k-c-len(b)-1])
                break
    
        if c + len(b) > k :
            break
    
        a.append(b)
        c += len(b)

Buggy submission (verdict: Runtime Error):
k = int(input())

a = [[str(i) for i in range(1,10)]]
c = 9

for i in range(10) :
    b = []
    for s in a[i] :
        if s[-1] == '0' :
            b.append(s+'0')
            b.append(s+'1')
        elif s[-1] == '9' :
            b.append(s+'8')
            b.append(s+'9')
        else :
            b.append(s+str(int(s[-1])-1))
            b.append(s+str(int(s[-1])))
            b.append(s+str(int(s[-1])+1))

        if c + len(b) > k :
            print(b[k-c-len(b)-1])
            break
    
    if c + len(b) > k :
        break
    
    a.append(b)
    c += len(b)
Problem p03126 (python)

Fixed submission:
n,m = map(int,input().split())
k = [0]*n
a = [0]*(m+1)
b = [0]*(m+1)

for i in range(n) :
    a = list(map(int,input().split()))
    for j in range(1,len(a)) :
        b[a[j]] += 1

print(b.count(n))

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
k = [0]*n
a = [0]*m
b = [0]*m

for i in range(n) :
    a = list(map(int,input().split()))
    for j in range(1,len(a)) :
        b[a[j]] += 1

print(a.count(n))
Problem p03280 (python)

Fixed submission:
a,b = map(int, input().split() )
print((a-1)*(b-1))

Buggy submission (verdict: Runtime Error):
s = input()
k = input()
t = '1'

for i in range(len(s)) :
   if s[i]=='1' :
      continue
   else :
      t = s[i]
      break

print(t)
Problem p02696 (python)

Fixed submission:
def m():
    a, b, n = map(int, input().split())

    def calc(x):
        return int((a * x) / b) - (a * int(x / b))

    return calc(min(n, b-1))

print(m())

Buggy submission (verdict: Runtime Error):
def m():
    a, b, n = map(int, input().split())

    def calc(x):
        return int((a * x) / b) - (a * int(x / b))

    if calc(1) < calc(n // 2):
        return max([calc(i) for i in range(n // 2, n + 1)])
    else:
        return max([calc(i) for i in range(1, n // 2 + 1)])


print(m())

Buggy submission (verdict: Runtime Error):
import math


def m():
    a, b, n = map(int, input().split())

    def calc(x):
        return math.floor((a * x) / b) - (a * math.floor(x / b))

    if calc(1) < calc(n // 2):
        return max([calc(i) for i in range(n // 2, n + 1)])
    else:
        return max([calc(i) for i in range(1, n // 2 + 1)])


print(m())
Problem p02702 (python)

Fixed submission:
def main():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = pow(10, i, MOD)
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


if __name__ == '__main__':
    print(main())

Buggy submission (verdict: Runtime Error):
from sys import stdin

input = stdin.readline


def main():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = (10 ** i) % MOD
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


if __name__ == '__main__':
    print(main())
Problem p02866 (python)

Fixed submission:
def m():
    MOD = 998244353
    N = int(input())
    D = [0] + list(map(int, input().split()))
    if D[1] != 0:
        return 0
    tree = {1:1}
    for i in range(2, N + 1):
        if D[i] == 0: return 0
        tree[D[i] + 1] = tree[D[i] + 1] + 1 if tree.get(D[i] + 1, False) else 1

    height = len(tree.keys())+1
    cnt = 1
    for i in range(2, height):
        if not tree.get(i, 0):
            return 0
        cnt = cnt * pow(tree[i-1], tree[i]) % MOD
    return cnt


print(m())

Buggy submission (verdict: Runtime Error):
def m():
    MOD = 998244353
    N = int(input())
    D = [0] + list(map(int, input().split()))
    if D[1] != 0:
        return 0
    tree = {1:1}
    for i in range(2, N + 1):
        if D[i] == 0: return 0
        tree[D[i] + 1] = tree[D[i] + 1] + 1 if tree.get(D[i] + 1, False) else 1

    height = len(tree.keys())+1
    cnt = 1
    for i in range(2, height):
        cnt = cnt * pow(tree[i-1], tree[i]) % MOD
    return cnt


print(m())

Buggy submission (verdict: Runtime Error):
def m():
    MOD = 998244353
    N = int(input())
    D = [0] + list(map(int, input().split()))
    if D[1] != 0:
        return 0
    tree = {}
    for i in range(2, N + 1):
        if D[i] == 0: return 0
        tree[D[i] + 1] = tree[D[i] + 1] + 1 if tree.get(D[i] + 1, False) else 1

    height = len(tree.keys())+1
    cnt = 1
    for i in range(2, height):
        cnt = cnt * pow(tree[i-1], tree[i]) % MOD
    return cnt


print(m())

Buggy submission (verdict: Runtime Error):
def m():
    MOD = 998244353
    N = int(input())
    D = [0] + list(map(int, input().split()))
    if D[1] != 0:
        return 0
    tree = {}
    for i in range(1, N + 1):
        tree[D[i] + 1] = tree[D[i] + 1] + 1 if tree.get(D[i] + 1, 0) else 1

    height = len(tree.keys())+1
    cnt = 1
    for i in range(2, height):
        cnt = cnt * pow(tree[i-1], tree[i]) % MOD
    return cnt


print(m())
Problem p02936 (python)

Fixed submission:
import queue
import sys
def input(): return sys.stdin.readline().rstrip()

def m():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(1, n):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0] * (n + 1)
    for _ in range(q):
        p, x = map(int, input().split())
        counter[p] += x

    que = queue.Queue()
    visited = [False] * (n+1)

    def bfs(root, base):
        base += counter[root]
        visited[root] = True
        for e in tree[root]:
            if visited[e]:
                continue
            que.put(e)
            counter[e] += base

        while not que.empty():
            x = que.get()
            point = counter[x]
            visited[x] = True
            for e in tree[x]:
                if visited[e]:
                    continue
                que.put(e)
                counter[e] += point

    bfs(1, 0)
    print(*counter[1:])
    return


m()

Buggy submission (verdict: Runtime Error):
def m():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(1, n):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0 for _ in range(n + 1)]
    for _ in range(q):
        p, x = map(int, input().split())
        counter[p] += x

    ans = [0] * (n + 1)
    visited = []

    def dfs(root, point):
        point += counter[root]
        ans[root] = point
        visited.append(root)
        for j in tree[root]:
            if j in visited:
                continue
            dfs(j, point)
        return

    dfs(1, 0)
    for i in range(1, n + 1):
        print(ans[i], '', end='')
    return ans


m()
Problem p03061 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))


def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)


def m():
    left, right = A.copy(), A.copy()
    for i in range(1, N):
        left[i] = gcd(A[i], left[i-1])

    for i in reversed(range(0, N-1)):
        right[i] = gcd(A[i], right[i+1])

    res = max(left[N-2], right[1])
    for i in range(1, N-1):
        res = max(gcd(left[i-1],right[i+1]), res)
    return res


print(m())

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))


def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)


def m():
    left, right = A.copy(), A.copy()
    for i in range(1, N):
        left[i] = gcd(A[i], left[i-1])

    for i in reversed(range(0, N-1)):
        right[i] = gcd(A[i], right[i+1])

    res = max(left[N-1], right[1])
    for i in range(1, N-1):
        res = max(gcd(left[i-1],right[i+1]), res)
    return res


print(m())

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))


def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)


def m():
    left, right = A.copy(), A.copy()
    for i in range(1, N):
        left[i] = gcd(A[i], left[i-1])

    for i in reversed(range(0, N-1)):
        right[i] = gcd(A[i], right[i+1])

    res = max(left[N-1], right[1])
    for i in range(1, N-1):
        res = max(gcd(left[i-1],right[i+1]), res)
    return res


print(m())
Problem p03240 (python)

Fixed submission:
N = int(input())
xyr = [list(map(int, input().split())) for _ in range(N)]


def m():
    tmpx, tmpy, tmpr = 0, 0, 0
    for a,b,c in xyr:
        if c != 0:
            tmpx = a
            tmpy = b
            tmpr = c
            break

    for cx in range(0, 101):
        for cy in range(0, 101):
            h = max(0, tmpr + abs(tmpx - cx) + abs(tmpy - cy))
            f = 0
            for x, y, r in xyr:
                if not (r == max(h - abs(x - cx) - abs(y - cy), 0)):
                    f = 1
                    break
            if f == 0:
                return "{} {} {}".format(cx, cy, h)


print(m())

Buggy submission (verdict: Runtime Error):
N = int(input())
xyr = [list(map(int, input().split())) for _ in range(N)]


def m():
    for cx in range(0, 101):
        for cy in range(0, 101):
            hs = []
            h = 0
            for x, y, r in xyr:
                h = max(0, r + abs(x - cx) + abs(y - cy))
                hs.append(h)
            if len(set(hs)) == 1:
                return "{} {} {}".format(cx, cy, set(hs)[0])


print(m())
Problem p03244 (python)

Fixed submission:
N = int(input())
V = list(map(int, input().split()))


def m():
    first = V[0::2]
    second = V[1::2]
    fdic = {}
    sdic = {}
    for i in range(N//2):
        if fdic.get(first[i], 0) == 0:
            fdic[first[i]] = 1
        else:
            fdic[first[i]] +=1

        if sdic.get(second[i], 0) == 0:
            sdic[second[i]] = 1
        else:
            sdic[second[i]] +=1
    flist = list(fdic.items())
    slist = list(sdic.items())

    flist.sort(reverse=True, key=lambda x:x[1])
    slist.sort(reverse=True, key=lambda x:x[1])

    if len(flist) == 1 and len(slist) == 1:
        if flist[0][0] == slist[0][0]:
            return N//2
        return 0
    if len(flist) == 1:
        return N//2 - slist[0][1]
    if len(slist) == 1:
        return N//2 - flist[0][1]

    if flist[0][0] != slist[0][0]:
        ans = 0
        for k, v in flist:
            if k != flist[0][0]:
                ans += v
        for k, v in slist:
            if k != slist[0][0]:
                ans += v
        return ans

    fans = 0
    sans = 0
    for k, v in flist:
        if k != flist[0][0]:
            fans += v
    for k, v in slist:
        if k != slist[1][0]:
            fans += v

    for k, v in flist:
        if k != flist[1][0]:
            sans += v
    for k, v in slist:
        if k != slist[0][0]:
            sans += v
    return min(fans,sans)

print(m())

Buggy submission (verdict: Runtime Error):
N = int(input())
V = list(map(int, input().split()))


def m():
    first = V[0::2]
    second = V[1::2]
    fdic = {}
    sdic = {}
    for i in range(N//2):
        if fdic.get(first[i], 0) == 0:
            fdic[first[i]] = 1
        else:
            fdic[first[i]] +=1

        if sdic.get(second[i], 0) == 0:
            sdic[second[i]] = 1
        else:
            sdic[second[i]] +=1
    flist = list(fdic.items())
    slist = list(sdic.items())

    flist.sort(reverse=True, key=lambda x:x[1])
    slist.sort(reverse=True, key=lambda x:x[1])

    if flist[0][0] != slist[0][0] and len(flist) == 1 and len(slist) == 1:
        return 0

    if flist[0][0] != slist[0][0]:
        ans = 0
        for k, v in flist[1:]:
            ans += v
        for k, v in slist[1:]:
            ans += v
        return ans

    if flist[0][1] > slist[0][1]:
        ans = 0
        for k, v in flist[1:]:
            ans += v
        for k, v in slist[2]:
            ans += v
        ans += slist[0][0]
        return ans
    else:
        ans = 0
        for k, v in slist[1:]:
            ans += v
        for k, v in flist[2]:
            ans += v
        ans += flist[0][0]
        return ans

print(m())
Problem p03283 (python)

Fixed submission:
def m():
    n, M, Q = map(int, input().split())
    a = [[0 for _ in range(n+1)] for _ in range(n+1)]
    b = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
    for i in range(M):
        l, r = map(int, input().split())
        a[l][r] += 1

    for i in range(1, n+1):
        b[1][i] += a[1][i] + b[1][i-1]
    for i in range(2, n+1):
        b[i][1] += a[i][1] + b[i-1][1]

    for i in range(1, n+1):
        for j in range(1, n+1):
            b[i][j] = a[i][j] + b[i-1][j] + b[i][j-1] - b[i-1][j-1]

    for i in range(Q):
        p, q = map(int, input().split())
        ans = b[q][q] + b[p-1][p-1] - b[q][p-1] - b[p-1][q]
        print(ans)

m()

Buggy submission (verdict: Runtime Error):
def m():
    n, M, Q = map(int, input().split())
    a = [[0 for _ in range(n+1)] for _ in range(n+1)]
    for i in range(M):
        l, r = map(int, input().split())
        a[l][r] += 1

    for i in range(1, n+1):
        p, q = map(int, input().split())
        ans = 0
        for j in range(p, q+1):
            for k in range(p, q + 1):
                ans += a[j][k]
        print(ans)

m()
Problem p03354 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

def m():
    n, m = map(int, input().split())
    p = list(map(int, input().split()))
    g = [[] for _ in range(n+1)]
    for _ in range(m):
        x, y = map(int, input().split())
        g[x].append(y)
        g[y].append(x)
    color = [-1] * (n+1)

    def dfs(v, c):
        s = []
        s.append(v)
        color[v] = c
        while len(s) != 0:
            a = s.pop()
            for elem in g[a]:
                if color[elem] == -1:
                    dfs(elem, c)

    k = 0
    for i in range(1, n):
        if color[i] == -1:
            dfs(i,k)
            k +=1
    ans = 0
    for i in range(n):
        if color[p[i]] == color[i+1]:
            ans +=1
    return ans

print(m())

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10*6)

def m():
    n, m = map(int, input().split())
    p = list(map(int, input().split()))
    g = [[] for _ in range(n+1)]
    for _ in range(m):
        x, y = map(int, input().split())
        g[x].append(y)
        g[y].append(x)
    color = [-1] * (n+1)

    def dfs(v, c):
        s = []
        s.append(v)
        color[v] = c
        while len(s) != 0:
            a = s.pop()
            for elem in g[a]:
                if color[elem] == -1:
                    dfs(elem, c)

    k = 0
    for i in range(1, n):
        if color[i] == -1:
            dfs(i,k)
            k +=1
    ans = 0
    for i in range(n):
        if color[p[i]] == color[i+1]:
            ans +=1
    return ans

print(m())