Problem:
 Score: 400 points    Problem Statement  In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1 , 2 , 3 , ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i ).
Takahashi the king is interested in the following Q matters:   The number of the trains that runs strictly within the section from City p_i to City q_i , that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i .   Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.      Constraints   N is an integer between 1 and 500 (inclusive).  M is an integer between 1 and 200 \ 000 (inclusive).  Q is an integer between 1 and 100 \ 000 (inclusive).  1 \leq L_i \leq R_i \leq N  (1 \leq i \leq M)  1 \leq p_i \leq q_i \leq N  (1 \leq i \leq Q)         Input  Input is given from Standard Input in the following format:  N  M  Q  L_1  R_1  L_2  R_2  :  L_M  R_M  p_1  q_1  p_2  q_2  :  p_Q  q_Q       Output  Print Q lines. The i -th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i .        Sample Input 1 2 3 1
1 1
1 2
2 2
1 2      Sample Output 1 3  As all the trains runs within the section from City 1 to City 2 , the answer to the only query is 3 .       Sample Input 2 10 3 2
1 5
2 8
7 10
1 7
3 10      Sample Output 2 1
1  The first query is on the section from City 1 to 7 . There is only one train that runs strictly within that section: Train 1 .
The second query is on the section from City 3 to 10 . There is only one train that runs strictly within that section: Train 3 .       Sample Input 3 10 10 10
1 6
2 9
4 5
4 7
4 7
5 8
6 6
6 7
7 9
10 10
1 8
1 9
1 10
2 8
2 9
2 10
3 8
3 9
3 10
1 10      Sample Output 3 7
9
10
6
8
9
6
7
8
10

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,q = LI()
    a = [[0]*n for i in range(n)]
    for _ in range(m):
        l,r = LI()
        l -= 1
        r -= 1
        a[l][r] += 1
    for i in range(n):
        for j in range(n-1):
            a[i][j+1] += a[i][j]
    for j in range(n):
        for i in range(n-1)[::-1]:
            a[i][j] += a[i+1][j]
    for _ in range(q):
        l,r = LI()
        l -= 1
        r -= 1
        print(a[l][r])
    return


if __name__ == "__main__":
    solve()

Solution (Python):
N, M, Q = map(int, input().split())
L, R = [], []
for _ in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

p, q = [], []
for _ in range(Q):
    tmp = list(map(int, input().split()))
    p.append(tmp[0])
    q.append(tmp[1])

cum_sum = [[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(M):
    cum_sum[L[i]][R[i]] += 1

for i in range(1, N+1):
    for j in range(1, N+1):
        cum_sum[i][j] += cum_sum[i][j-1]
        cum_sum[i][j] += cum_sum[i-1][j]
        cum_sum[i][j] -= cum_sum[i-1][j-1]

def get_sum_range(L, R):
    ans = cum_sum[R][R] - cum_sum[R][L-1] - cum_sum[L-1][R] + cum_sum[L-1][L-1]
    return ans

for i in range(Q):
    ans = get_sum_range(p[i], q[i])
    print(ans)

Solution (Python):
N,M,Q = map(int, input().split())

A = [[0] * 509 for _ in range(509)]
for _ in range(M):
    L, R = map(int, input().split())
    A[L][R] += 1


for i in range(1, N + 1):
    for j in range(1, N + 1):
        A[i][j] += A[i][j - 1]
for i in range(1, N + 1):
    for j in range(1, N + 1):
        A[i][j] += A[i - 1][j]


for _ in range(Q):
    L, R = map(int, input().split())
    ans = A[R][R] - A[L - 1][R] - A[R][L - 1] + A[L - 1][L - 1]
    print(ans)

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
n,m,q=map(int,input().split())
T=[[0]*(n+2) for _ in range(n+2)]
for _ in range(m):
  l,r=map(int,input().split())
  T[l][r]+=1
for i in range(1,n+1)[::-1]:
  for j in range(1,n+1):
    T[i][j+1]+=T[i][j]
for j in range(1,n+1):
  for i in range(1,n+1)[::-1]: 
    T[i-1][j]+=T[i][j]

for _ in range(q):
  p,q=map(int,input().split())
  print(T[p][q])

Solution (Python):
import sys
input = sys.stdin.readline

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

D = [[0 for j in range(N)] for i in range(N)]

for _ in range(M):
    l, r = map(int, input().split())
    D[l - 1][r - 1] += 1

for i in range(N):
    for j in range(1, N):
        D[i][j] += D[i][j - 1]

for j in range(N):
    for i in range(N - 1)[::-1]:
        D[i][j] += D[i + 1][j]

for _ in range(Q):
    p, q = map(int, input().split())
    print(D[p - 1][q - 1])

Solution (Python):
import sys
input = sys.stdin.readline

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

D = [[0 for j in range(N)] for i in range(N)]

for _ in range(M):
    l, r = map(int, input().split())
    D[l - 1][r - 1] += 1

for i in range(N):
    for j in range(1, N):
        D[i][j] += D[i][j - 1]

for j in range(N):
    for i in range(N - 1)[::-1]:
        D[i][j] += D[i + 1][j]

for _ in range(Q):
    p, q = map(int, input().split())
    print(D[p - 1][q - 1])

Solution (Python):
'''
https://atcoder.jp/contests/abc106/tasks/abc106_d
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    N,M,Q = map(int, input().split())
    table = [[0]*N for _ in range(N)]
    for _ in range(M):
        l,r = map(int, input().split())
        table[l-1][r-1] += 1

    def csum_gen(h, w, table):
        d = [[0]*w for _ in range(h)]
        d[0][0] = table[0][0]
        for i in range(1, w):
            d[0][i] = d[0][i-1]+table[0][i]
        
        for i in range(1, h):
            total = 0
            for j in range(w):
                total += table[i][j]
                d[i][j] = d[i-1][j]+total
        return d

    d = csum_gen(N, N, table)

    def csum_cal(p, q, x, y, d):
        
        if p>x or q>y:
            return 0
        if p==0 and q==0:
            return d[x][y]
        if p==0:
            return d[x][y] - d[x][q-1]
        if q==0:
            return d[x][y] - d[p-1][y]
        return d[x][y]-d[p-1][y]-d[x][q-1]+d[p-1][q-1]


    for _ in range(Q):
        p,q = map(int, input().split())
        print(csum_cal(p-1, p-1, q-1, q-1, d))



if __name__ == '__main__':
    main()

Solution (Python):
import math,itertools,bisect,sys,copy,queue

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())

N,M,Q = LI()

train = [[0 for _ in range(N+1)] for _ in range(N+1)]
for _ in range(M):
    l,r = LI()
    train[l][r] += 1

c = [[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(1,N+1):
    cnt = 0
    for j in range(1,N+1):
        cnt += train[i][j]
        c[i][j] = c[i-1][j] + cnt

for _ in range(Q):
    l,r = LI()
    print (c[r][r]-c[r][l-1]-c[l-1][r]+c[l-1][l-1])

Solution (Python):
N,M,Q = map(int,input().split())
plane = [[0]*N for _ in range(N)]
for i in range(M):
    l,r = map(int,input().split())
    plane[l-1][r-1] += 1
acc = [[0]*(N+1) for _ in range(N)]

for i in range(N):
    for j in range(N):
        acc[i][j+1] = acc[i][j]+plane[i][j]

for i in range(Q):
    p,q = map(int,input().split())
    tmp = 0
    for j in range(p,q+1):
        tmp += acc[j-1][q]-acc[j-1][p-1]
    print(tmp)

Solution (Python):
import sys
input=sys.stdin.readline
n,m,q=map(int,input().split())
num=[[0]*(n+1) for _ in range(n+1)]
for i in range(m):
	l,r=map(int,input().split())
	num[l][r]+=1
for i in range(n):
	for j in range(n):
		num[i+1][j+1]+=num[i+1][j]
for i in range(n):
	for j in range(n):
		num[i+1][j+1]+=num[i][j+1]
for i in range(q):
	p,q=map(int,input().split())
	ans=num[q][q]+num[p-1][p-1]-num[p-1][q]-num[q][p-1]
	print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
n,m,q=map(int,input().split())
num=[[0]*(n+1) for _ in range(n+1)]
for i in range(m):
	l,r=map(int,input().split())
	num[l][r]+=1
for i in range(n):
	for j in range(n):
		num[i+1][j+1]+=num[i+1][j]
for i in range(q):
	p,q=map(int,input().split())
	ans=0
	for i in range(p,q+1):
		ans+=num[i][q]-num[i][p-1]
	print(ans)

Solution (Python):
from operator import itemgetter

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

que = []

for _ in range(M):
    l, r = list(map(int, input().split()))
    que.append((-1, l, r))
for i in range(Q):
    l, r = list(map(int, input().split()))
    que.append((i, l, r))

ans = [0 for _ in range(Q)]
que = sorted(que, key=itemgetter(2, 0))
before = -1
ans_data = [0 for _ in range(N)]
for i, l, r in que:
    if i == -1:
        ans_data[l-1] += 1
    else:
        ans[i] = sum(ans_data[l-1:r])

print("\n".join(map(str, ans)))

Solution (Python):
N, M, Q = map(int, input().split())
X = [[0 for _ in range(N)] for _ in range(N)]


for _ in range(M):
    l, r = list(map(int, input().split()))
    X[l-1][r-1] += 1
C = []
for line in X:
    tmp = [0, ]
    for c in line:
        tmp.append(tmp[-1] + c)
    C.append(tmp)
query = [map(int, input().split()) for _ in range(Q)]



for p, q in query:
    ans = 0
    for i, line in enumerate(C):
        if p-1 <= i and i <= q-1:
            ans += line[q] - line[p-1]
    print(ans)

Solution (Python):
n, m, Q = map(int, input().split())
data = [[0]*(n+2) for i in range(n+2)]
for i in range(m):
    l, r = map(int, input().split())
    data[l][r] += 1

for i in range(n+2):
    for j in range(n+1):
        data[i][j+1] += data[i][j]

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

for i in range(Q):
    p, q = map(int, input().split())
    print(data[p][q])

Solution (Python):
n, m, Q = map(int, input().split())
data = [[0]*(n+2) for i in range(n+2)]
for i in range(m):
    l, r = map(int, input().split())
    data[l][r] += 1

for i in range(n+2):
    for j in range(n+1):
        data[i][j+1] += data[i][j]

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

for i in range(Q):
    p, q = map(int, input().split())
    print(data[p][q])

Solution (Python):
n, m, Q = map(int, input().split())
data = [[0]*(n+2) for i in range(n+2)]
for i in range(m):
    l, r = map(int, input().split())
    data[l][r] += 1

for i in range(n+2):
    for j in range(n+1):
        data[i][j+1] += data[i][j]

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

for i in range(Q):
    p, q = map(int, input().split())
    print(data[p][q])

Solution (Python):
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N, M, Q = map(int, input().split())
sects = []
for _ in range(M):
    sects.append([0] + list(map(int, input().split())))
for q in range(Q):
    sects.append([1] + list(map(int, input().split())) + [q])

sects.sort(key=lambda x: x[2])
b = Bit(N)
ans = []
for s in sects:
    if s[0] == 0:
        b.add(s[1], 1)
    else:
        ans.append((b.sum(s[2]) - b.sum(s[1] - 1), s[3]))
ans.sort(key=lambda x: x[1])
for x in ans:
    print(x[0])

Solution (Python):
import numpy as np
import sys

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

I = sys.stdin.read().split()

L,R = np.array(I[:2*M], np.int32).reshape([2,-1], order='F')
p,q = np.array(I[2*M:], np.int32).reshape(2,-1, order='F')
d = np.zeros([N+1, N+1], np.int32)

d = np.zeros([1+N, 1+N], np.int32)
np.add.at(d, (N-L,R), 1)

d = np.add.accumulate(d, axis = 1)
d = np.add.accumulate(d)

ans = d[N-p,q]

for a in ans:
	print(a)

Solution (Python):
6
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,M,Q=map(int,input().split())
    x=[[0]*N for _ in range(N)]
    for i in range(M):
        L,R=map(lambda x: int(x)-1,input().split())
        x[L][R]+=1
    S=[[0]*(N+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(N):
            S[i+1][j+1]=S[i+1][j]+S[i][j+1]-S[i][j]+x[i][j]
    for _ in range(Q):
        p,q=map(lambda x: int(x)-1,input().split())
        print(S[q+1][q+1]-S[q+1][p]-S[p][q+1]+S[p][p])

if __name__ == '__main__':
    main()

Solution (Python):
6
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,M,Q=map(int,input().split())
    x=[[0]*N for _ in range(N)]
    for i in range(M):
        L,R=map(lambda x: int(x)-1,input().split())
        x[L][R]+=1
    S=[[0]*(N+1) for _ in range(N)]
    for i in range(N):
        for j in range(N):
            S[i][j+1]=S[i][j]+x[i][j]
    for _ in range(Q):
        ans=0
        p,q=map(lambda x: int(x)-1,input().split())
        for i in range(p,q+1):
            ans+=S[i][q+1]-S[i][p]
        print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,M,Q = map(int,input().split())
A = [[0 for _ in range(N+1)] for _ in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    A[L][R] += 1
dp = [[0 for _ in range(N+1)] for _ in range(N+1)]
dp[1][1] = A[1][1]
for j in range(2,N+1):
    dp[1][j] = A[1][j]+dp[1][j-1]
for i in range(2,N+1):
    dp[i][1] = A[i][1]+dp[i-1][1]
for i in range(2,N+1):
    for j in range(2,N+1):
        dp[i][j] = A[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]
for _ in range(Q):
    p,q = map(int,input().split())
    if p==1:
        cnt = dp[N][q]
    else:
        cnt = dp[N][q]-dp[p-1][q]
    print(cnt)

Solution (Python):
n,m,q=map(int,input().split())
LR=[tuple(map(int,input().split())) for _ in range(m)]
PQ=[tuple(map(int,input().split())) for _ in range(q)]

MAP=[[0]*(n+1) for _ in range(n+1)]
for l,r in LR:
    MAP[l][r] +=1

for i in range(n+1):
    for j in range(n+1):
        MAP[i][j] +=MAP[i][j-1]

for i in range(n+1):
    for j in range(n+1):
        MAP[i][j] +=MAP[i-1][j]

for p,q in PQ:
    print(MAP[q][q]-(MAP[q][p-1]+MAP[p-1][q])+MAP[p-1][p-1])

Solution (Python):
N, M, Q = map(int, input().split())
p = []
q = []




CS = [[0] * (N + 1) for _ in range(N + 1)]

for _ in range(M):
	l, r = map(int, input().split())
	CS[l][r] += 1


for i in range(N + 1):
	for j in range(N + 1):
		if j - 1 >= 0: 
			CS[i][j] += CS[i][j - 1]
		if i - 1 >= 0: 
			CS[i][j] += CS[i - 1][j]
		if j - 1 >= 0 and i - 1 >= 0: 
			CS[i][j] -= CS[i - 1][j - 1]
	
for _ in range(Q):
	a, b = map(int, input().split())
	p.append(a)
	q.append(b)
	
for i in range(Q):
	print(CS[q[i]][q[i]] - CS[p[i] - 1][q[i]] - CS[q[i]][p[i] - 1] + CS[p[i] - 1][p[i] - 1])

Solution (Python):

n,m,q=map(int,input().split())
mat=[[0]*(n+1) for i in range(n+1)]
for i in range(m):
    l,r=map(int,input().split())
    mat[l][r]+=1

for i in range(n+1):
    for j in range(n+1):
        mat[i][j]+=mat[i-1][j]
for i in range(n+1):
    for j in range(n+1):
        mat[i][j]+=mat[i][j-1]

for i in range(q):
    l,r=map(int,input().split())
    ans=mat[r][r]+mat[l-1][l-1]-mat[l-1][r]-mat[r][l-1]
    print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(100000000)

def main():
    n,m,q=map(int,input().split())
    cum=[[0]*n for _ in range(n)]

    for _ in range(m):
        l,r=map(int,input().split())
        l-=1
        r-=1
        cum[0][r]+=1
        if l!=n-1:
            cum[l+1][r]-=1
       
    for i in range(n):
        for j in range(1,n):
            cum[i][j]+=cum[i][j-1]
    
    for i in range(1,n):
        for j in range(n):
            cum[i][j]+=cum[i-1][j]

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

if __name__=='__main__':
    main()

Solution (Python):

n,m,q=map(int,input().split())
mat=[[0]*(n+1) for i in range(n+1)]
for i in range(m):
    l,r=map(int,input().split())
    mat[l][r]+=1

for i in range(n+1):
    for j in range(n+1):
        mat[i][j]+=mat[i-1][j]
        mat[i][j]+=mat[i][j-1]
        mat[i][j]-=mat[i-1][j-1]

for i in range(q):
    l,r=map(int,input().split())
    ans=mat[r][r]+mat[l-1][l-1]-mat[l-1][r]-mat[r][l-1]
    print(ans)

Solution (Python):
import sys

N, M, Q = map(int, sys.stdin.readline().split())

S = [[0 for _ in range(N+1)] for _ in range(N+1)]
for _ in range(M):
    l, r = map(int, sys.stdin.readline().split())
    S[l][r] += 1

for l in range(N):
    for r in range(N+1):
        S[l+1][r] += S[l][r]

for l in range(N+1):
    for r in range(N):
        S[l][r+1] += S[l][r]

for _ in range(Q):
    p, q = map(int, sys.stdin.readline().split())
    ans = S[q][q] - S[q][p-1] - S[p-1][q] + S[p-1][p-1]
    print(ans)

Solution (Python):

import sys


def solve(N: int, M: int, Q: int, L: "List[int]", R: "List[int]", p: "List[int]", q: "List[int]"):
    matrix = [[0]*(N+1) for _ in range(N+1)]

    for i in range(M):
        matrix[L[i]][R[i]] += 1
    
    for i in range(1,N+1):
        for j in range(1,N+1):
            matrix[i][j] += matrix[i-1][j]+matrix[i][j-1]-matrix[i-1][j-1]
    
    for i in range(Q):
        pp,qq = p[i],q[i]
        print(matrix[qq][qq]-matrix[pp-1][qq]-matrix[qq][pp-1]+matrix[pp-1][pp-1])

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    Q = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    p = [int()] * (Q)  
    q = [int()] * (Q)  
    for i in range(Q):
        p[i] = int(next(tokens))
        q[i] = int(next(tokens))
    solve(N, M, Q, L, R, p, q)

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

N,M,Q,*LRpq = map(int,open(0).read().split())
LR = LRpq[:2*M]
pq = LRpq[2*M:]

train_count = np.zeros((N,N),int)

for l,r in zip(LR[::2],LR[1::2]):
    train_count[l-1,r-1] += 1

query_ans = train_count[::-1,].cumsum(axis=0)[::-1,].cumsum(axis=1)

for p,q in zip(pq[::2],pq[1::2]):
    print(query_ans[p-1,q-1])

Solution (Python):

def accumulater2D(A): 
    import copy
    
    n = len(A)
    B = copy.deepcopy(A)
    for i in range(n):
        for j in range(n):
            if i: B[i][j] += B[i-1][j]
    
    for i in range(n):
        for j in range(n):
            if j: B[i][j] += B[i][j-1]
    return B 

N,M,Q = map(int,input().split())
LR = []
pq = []
tiles = [[0 for x in range(N+1)] for y in range(N+1)]
for i in range(M):
    L,R = list(map(int,input().split()))
    tiles[L][R] += 1
    
A = accumulater2D(tiles)
for i in range(Q):
    p,q = list(map(int,input().split()))
    pq.append([p,q])

for i in range(Q):
    p,q = pq[i]
    print(A[q][q]-A[p-1][q]-A[q][p-1]+A[p-1][p-1])

Solution (Python):
n, m, Q = map(int, input().split())
LR = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    LR[l-1][r-1] += 1


LR_rui = [[0 for _ in range(n+1)] for _ in range(n+1)]

for i in range(n):
    for j in range(n):
        LR_rui[i+1][j+1] = LR_rui[i+1][j]+LR[i][j]

for i in range(n):
    for j in range(n):
        LR_rui[i+1][j+1] = LR_rui[i+1][j+1] + LR_rui[i][j+1]

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