Buggy python submission (verdict: Time Limit Exceeded):
n, m, q = map(int, input().split())
edges = [[0]*n for _ in range(n)]

for _ in range(m):
    L, R = map(lambda x: int(x)-1, input().split())
    edges[L][R] += 1


for _ in range(q):
    p, q = map(int, input().split())
    lines = edges[p-1:q]
    print(sum(sum(row[p-1:q]) for row in lines))

Fixed submission:
n, m, q = map(int, input().split())
edges = [[0]*n for _ in range(n)]

for _ in range(m):
    L, R = map(lambda x: int(x)-1, input().split())
    edges[L][R] += 1


S = [[0]*(n+1)for _ in range(n+1)]
for i in range(n):
    for j in range(n):
        S[i][j+1] = S[i][j] + edges[i][j]

for _ in range(q):
    p, q = map(int, input().split())
    lines = S[p-1:q]
    ans = 0
    for row in lines:
        ans += row[q]-row[p-1]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m, q = map(int, input().split())
edges = []
for i in range(m):
    L, R = map(int, input().split())
    edges.append((L, R, i))

L_sorted = sorted(edges)
L_sorted_bi = [L for L, R, i in L_sorted]
L_cum_set = [set()for _ in range(m+1)]
for i in reversed(range(m)):
    L_cum_set[i] = L_cum_set[i+1] | {L_sorted[i][2]}


R_sorted = sorted(edges, key=lambda x: x[1])
R_sorted_bi = [R for L, R, i in R_sorted]
R_cum_set = [set()for _ in range(m+1)]
for i in range(m):
    R_cum_set[i+1] = R_cum_set[i] | {R_sorted[i][2]}


for _ in range(q):
    p, q = map(int, input().split())
    idx = bisect.bisect_left(L_sorted_bi, p)
    s = L_cum_set[idx]

    idx = bisect.bisect_left(R_sorted_bi, q+1)
    t = R_cum_set[idx]
    print(len(s & t))


Fixed submission:
n, m, q = map(int, input().split())
edges = [[0]*n for _ in range(n)]

for _ in range(m):
    L, R = map(lambda x: int(x)-1, input().split())
    edges[L][R] += 1


S = [[0]*(n+1)for _ in range(n+1)]
for i in range(n):
    for j in range(n):
        S[i][j+1] = S[i][j] + edges[i][j]

for _ in range(q):
    p, q = map(int, input().split())
    lines = S[p-1:q]
    ans = 0
    for row in lines:
        ans += row[q]-row[p-1]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m, q = map(int, input().split())
edges = []
for i in range(m):
    L, R = map(int, input().split())
    edges.append((L, R, i))

L_sorted = sorted(edges)
L_sorted_bi = [L for L, R, i in L_sorted]
L_cum_set = [set()for _ in range(m+1)]
for i in reversed(range(m)):
    L_cum_set[i] = L_cum_set[i+1] | {L_sorted[i][2]}


R_sorted = sorted(edges, key=lambda x: x[1])
R_sorted_bi = [R for L, R, i in R_sorted]
R_cum_set = [set()for _ in range(m+1)]
for i in range(m):
    R_cum_set[i+1] = R_cum_set[i] | {R_sorted[i][2]}


for _ in range(q):
    p, q = map(int, input().split())
    idx = bisect.bisect_left(L_sorted_bi, p)
    s = L_cum_set[idx]

    idx = bisect.bisect_left(R_sorted_bi, q+1)
    t = R_cum_set[idx]
    print(len(s & t))


Fixed submission:
n, m, q = map(int, input().split())
edges = [[0]*n for _ in range(n)]

for _ in range(m):
    L, R = map(lambda x: int(x)-1, input().split())
    edges[L][R] += 1


S = [[0]*(n+1)for _ in range(n+1)]
for i in range(n):
    for j in range(n):
        S[i][j+1] = S[i][j] + edges[i][j]

for _ in range(q):
    p, q = map(int, input().split())
    lines = S[p-1:q]
    ans = 0
    for row in lines:
        ans += row[q]-row[p-1]
    print(ans)