Problem p02714 (python)

Fixed submission:
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
if len(r) == 0 or len(g) == 0 or len(b) == 0:
	print (0)
	exit()
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b) and b[x] == k:
				ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b) and b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b) and b[x] == l:
				ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x < len(b) and b[x] == k:
				ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x < len(b) and b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x < len(b) and b[x] == l:
				ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k < b[-1] and k > b[0]:
			x = bisect.bisect_left(b,k)
			if b[x] == k:
				ans -= 1
		if m < b[-1] and m > b[0]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if b[x] == k:
				ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1]:
			x = bisect.bisect_left(b,k)
			if b[x] == k:
				ans -= 1
		if m <= b[-1]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		if i > j:
			k = i-abs(i-j)
		else:
			k = i+abs(i-j)
		if k in b:
			ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)