Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M, Q = [int(i) for i in input().strip().split(' ')]

flag  = False
Ls = []
Rs = []
counter = M

d = []
for i in range(N):
    d.append([0] * N)


for line in sys.stdin:
    L, R = [int(i) for i in line.strip().split(' ')]
    if not flag:
        for i in range(L):
            for j in range(N - R + 1):
                d[i][R + j - 1] += 1
        counter -= 1
        if counter == 0:
            flag = True
    else:
        print(d[L-1][R-1])


Fixed submission:
import sys
from itertools import accumulate
N, M, Q = [int(i) for i in input().strip().split(' ')]

flag  = False
counter = M

d = []
d2 = []
for i in range(N):
    d.append([0] * N)
for i in range(N):
    d2.append([0] * N)


for line in sys.stdin:
    L, R = [int(i) for i in line.strip().split(' ')]
    if not flag:
        d[L-1][R-1] += 1
#        for i in range(L):
#            for j in range(N - R + 1):
#                d[i][R + j - 1] += 1
        counter -= 1
        if counter == 0:
#            print(d)
            for i in range(N):
                d[i] = d[i][::-1]
#            print(d)
            for i in range(N):
                if i == 0:
                    d[N-1] = list(accumulate(d[N-1][::-1]))[::-1]
                else:
                    for j in range(N):
                        if j == 0:
                            temp = d[N-1-i][N-1-j]
                        else:
                            temp += d[N-1-i][N-1-j]
                        d[N-1-i][N-1-j] = d[N-i][N-1-j] + temp
#            print(d)
#            for x in d:
#                print(x)
            flag = True
    else:
        print(d[L-1][N-R])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M, Q = [int(i) for i in input().strip().split(' ')]

flag  = False
Ls = []
Rs = []
ps = []
qs = []
counter = M

d = []
for i in range(N):
    d.append([0] * N)


for line in sys.stdin:
    L, R = [int(i) for i in line.strip().split(' ')]
#    print(line.strip().split(' '))
#    L, R == line.strip().split(' ')
    if not flag:
        for i in range(L):
            for j in range(N - R + 1):
                d[i][R + j - 1] += 1
        Ls.append(L)
        Rs.append(R)
        counter -= 1
        if counter == 0:
            flag = True
    else:
        print(d[L-1][R-1])

Fixed submission:
import sys
from itertools import accumulate
N, M, Q = [int(i) for i in input().strip().split(' ')]

flag  = False
counter = M

d = []
d2 = []
for i in range(N):
    d.append([0] * N)
for i in range(N):
    d2.append([0] * N)


for line in sys.stdin:
    L, R = [int(i) for i in line.strip().split(' ')]
    if not flag:
        d[L-1][R-1] += 1
#        for i in range(L):
#            for j in range(N - R + 1):
#                d[i][R + j - 1] += 1
        counter -= 1
        if counter == 0:
#            print(d)
            for i in range(N):
                d[i] = d[i][::-1]
#            print(d)
            for i in range(N):
                if i == 0:
                    d[N-1] = list(accumulate(d[N-1][::-1]))[::-1]
                else:
                    for j in range(N):
                        if j == 0:
                            temp = d[N-1-i][N-1-j]
                        else:
                            temp += d[N-1-i][N-1-j]
                        d[N-1-i][N-1-j] = d[N-i][N-1-j] + temp
#            print(d)
#            for x in d:
#                print(x)
            flag = True
    else:
        print(d[L-1][N-R])