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):

import sys


def solve_iter(N: int, M: int, Q: int, L: "List[int]", R: "List[int]", p: "List[int]", q: "List[int]"):
    f = [[0] * N for _ in range(N)]
    for l, r in zip(L, R):
        f[l-1][r-1] += 1
    dp = [[0] * (N + 1) for _ in range(N+1)]
    for h in range(N):
        for w in range(N):
            dp[h+1][w+1] = dp[h+1][w] + dp[h][w+1] - dp[h][w] + f[h][w]

    
    
    for pp, qq in zip(p, q):
        
        yield dp[qq][qq] - dp[qq][pp-1] - dp[pp-1][qq] + dp[pp-1][pp-1]

def solve(N: int, M: int, Q: int, L: "List[int]", R: "List[int]", p: "List[int]", q: "List[int]"):
    return list(solve_iter(N, M, Q, L, R, p, q))


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))
    print(*solve(N, M, Q, L, R, p, q), sep='\n')

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

Solution (Python):
import sys
import numpy as np
from itertools import accumulate

input = sys.stdin.readline

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

lines = np.zeros((501, 501), np.int32)

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

lines = np.cumsum(lines, axis = 0)
lines = np.cumsum(lines, axis = 1)


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

Solution (Python):


import sys






















def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, m, q = mi()
    train = [lmi() for _ in range(m)]
    query = [lmi() for _ in range(q)]

    """
    電車の運行区間のうち開始地点を x 座標で、終着地点を y 座標で表し二次元座標で管理する
    [i, j] 区間内で運行する電車は 0 <= x <= i, 0 <= y <= j の長方形区間内の全ての電車である
    """
    coordinate = [[0] * (n + 1) for _ in range(n + 1)]
    for start, end in train:
        coordinate[start][end] += 1
    
    
    
    
    
    for i in range(n):
        for j in range(n):
            coordinate[i+1][j+1] += coordinate[i][j+1] + coordinate[i+1][j] - coordinate[i][j]
    
    
    

    
    for l, r in query:
        print(coordinate[r][r] - coordinate[l-1][r] - coordinate[r][l-1] + coordinate[l-1][l-1])


if __name__ == "__main__":
    main()

Solution (Python):
n, m, Q = map(int, input().split())

class Cumulative_Sum_2D:
    def __init__(self, a):
        lai = len(a)
        laj = len(a[0])
        self.s = [[0] * (laj + 1) for _ in range(lai + 1)]
        for i in range(lai):
            for j in range(laj):
                self.s[i+1][j+1] = self.s[i+1][j] + a[i][j]
        for i in range(lai):
            for j in range(laj):
                self.s[i+1][j+1] += self.s[i][j+1]
    def sum(self, li, lj, ri, rj):
        s = self.s
        return s[ri][rj] - s[li][rj] - s[ri][lj] + s[li][lj]

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

cs = Cumulative_Sum_2D(a)

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

Solution (Python):


import sys



from operator import itemgetter              


















def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, m, q = mi()
    intervals = []
    for _ in range(m):
        l, r = mi()
        intervals.append((l - 1, r))
    intervals.sort(key=itemgetter(1))

    commands = []
    for _ in range(q):
        a, b = mi()
        commands.append((a - 1, b))

    
    
    completely_included = [0] * (n + 1)
    for _, right in intervals:
        completely_included[right] += 1
    prev = 0
    for i in range(1, n + 1):
        completely_included[i] += prev
        prev = completely_included[i]
    
    
    
    across_num = [[0] * (n + 1) for _ in range(n + 1)]
    for left, right in intervals:
        if right - left >= 2:
            for i in range(right, n + 1):
                across_num[i][left + 1] += 1
                across_num[i][right] -= 1
    for i in range(1, n + 1):
        prev = 0
        for j in range(1, n + 1):
            across_num[i][j] += prev
            prev = across_num[i][j]
    

    def calc_included_intervals(left, right):
        return completely_included[right] - completely_included[left] - across_num[right][left]


    for l, r in commands:
        print(calc_included_intervals(l, r))


if __name__ == "__main__":
    main()

Solution (Python):
import sys

input = lambda :sys.stdin.readline().rstrip()

def main():
    n, m, Q = map(int, input().split())
    Train= [[0 for _ in range(n)] for _ in range(n)]
    
    for i in range(m):
        l, r = map(int, input().split())
        l, r = l - 1, r - 1
        Train[l][r] += 1
    
    P_Train = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
    for i in range(n):
        for j in range(n):
            P_Train[i + 1][j + 1] = P_Train[i][j + 1] + P_Train[i + 1][j] + Train[i][j] - P_Train[i][j]
    
    

    for i in range(Q):
        p, q = map(int, input().split())
        p, q = p - 1, q - 1
        ans = solve(P_Train, p, q)
        print(ans)

def solve(P_Train, p, q):
    ans = 0
    ans = P_Train[q + 1][q + 1] -P_Train[p][q + 1] - P_Train[q + 1][p] + P_Train[p][p]
    return ans


if __name__ == '__main__':
    main()

Solution (Python):
import sys

input = lambda :sys.stdin.readline().rstrip()

def main():
    n, m, Q = map(int, input().split())
    Train= [[0 for _ in range(n)] for _ in range(n)]
    
    for i in range(m):
        l, r = map(int, input().split())
        l, r = l - 1, r - 1
        Train[l][r] += 1
    
    P_Train = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
    for i in range(n):
        for j in range(n):
            P_Train[i + 1][j + 1] = P_Train[i][j + 1] + P_Train[i + 1][j] + Train[i][j] - P_Train[i][j]
    
    

    for i in range(Q):
        p, q = map(int, input().split())
        p, q = p - 1, q - 1
        ans = solve(P_Train, p, q)
        print(ans)

def solve(P_Train, p, q):
    ans = 0
    ans = P_Train[q + 1][q + 1] -P_Train[p][q + 1] - P_Train[q + 1][p] + P_Train[p][p]
    return ans


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

input = lambda :sys.stdin.readline().rstrip()

def main():
    n, m, Q = map(int, input().split())
    Train= [[0 for _ in range(n)] for _ in range(n)]
    
    for i in range(m):
        l, r = map(int, input().split())
        l, r = l - 1, r - 1
        Train[l][r] += 1
    
    P_Train = [[0 for _ in range(n + 1)] for _ in range(n)]
    for i in range(n):
        tmp = 0
        for j in range (n):
            tmp += Train[i][j]
            P_Train[i][j + 1] = tmp

    for i in range(Q):
        p, q = map(int, input().split())
        p, q = p - 1, q - 1
        ans = solve(P_Train, n, p, q)
        print(ans)

def solve(P_Train, n, p, q):
    ans = 0
    for i in range(p, q + 1):
        ans += P_Train[i][q + 1] - P_Train[i][p]
    return ans


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
def LILI(n): return [LI() for _ in range(n)]
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main_old(): 
    N, M, queries = LI()
    L = []
    R = []
    for _ in range(M):
        l, r = LI()
        L.append(l)
        R.append(r)
    P = []
    Q = []
    for _ in range(queries):
        p, q = LI()
        P.append(p)
        Q.append(q)

    regions = [[] for _ in range(N+1)]
    for l, r in zip(L, R):
        regions[l].append(r-l)
    regions_cumsum = [[0]*(N+1) for _ in range(N+1)]
    for i in range(len(regions)):  
        
        cnt = [0]*(N+1)
        for j in range(N+1):
            cnt[regions[i][j]] += 1
        
        regions_cumsum[0] = cnt[0]
        for j in range(1, N+1):
            regions_cumsum[j] = regions_cumsum[j-1] + cnt[j]

    from bisect import bisect_right
    
    for p, q in zip(P, Q):
        counter = 0
        length = q-p
        print(regions_cumsum[q] - regions_cumsum[p])




    
    
    
    



def main(): 
    N, M, queries = LI()
    L = []
    R = []
    for _ in range(M):
        l, r = LI()
        L.append(l)
        R.append(r)
    P = []
    Q = []
    for _ in range(queries):
        p, q = LI()
        P.append(p)
        Q.append(q)

    trains = [[0]*(N+1) for _ in range(N+1)]
    for l, r in zip(L,R):
        trains[l][r] += 1

    cumsum = [[0]*(N+1) for _ in range(N+1)]
    for i in range(1, N+1):
        for j in range(1, N+1):
            cumsum[i][j] = cumsum[i][j-1] + trains[i][j]

    for p, q in zip(P, Q):
        range_sum = 0
        for i in range(p, q+1):
            range_sum += cumsum[i][q] - cumsum[i][p-1]
        print(range_sum)

main()

Solution (Python):
import sys
input = sys.stdin.readline
n,m,q = map(int,input().split())
def func(a):
    return int(a)-1
grid = [[0]*n for i in range(n)]
pq = [0]*q
for i in range(m):
    l,r = map(func,input().split())
    grid[l][r] += 1
for i in range(q):
    pq[i] = list(map(func,input().split()))

s = [[0]*n for i in range(n)]
s[0][0] = grid[0][0]
for i in range(1,n):
    s[i][0] = s[i-1][0] + grid[i][0]
for i in range(1,n):
    s[0][i] = s[0][i-1] + grid[0][i]

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

anslist = [0]*q
for i in range(q):
    anslist[i] = s[pq[i][1]][pq[i][1]] - s[pq[i][1]][pq[i][0]-1] - s[pq[i][0]-1][pq[i][1]] + s[pq[i][0]-1][pq[i][0]-1] if pq[i][0] > 0 else s[pq[i][1]][pq[i][1]]


for ans in anslist:
    print(ans)

Solution (Python):
def main():
    n, m, Q = map(int, input().split())
    Train= [[0 for _ in range(n)] for _ in range(n)]
    
    for i in range(m):
        l, r = map(int, input().split())
        l, r = l - 1, r - 1
        Train[l][r] += 1
    
    P_Train = [[0 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += Train[i][j]
            P_Train[i][j] = tmp

    for i in range(Q):
        p, q = map(int, input().split())
        p, q = p - 1, q - 1
        ans = solve(P_Train, n, p, q)
        print(ans)

def solve(P_Train, n, p, q):
    ans = 0
    for i in range(p, q + 1):
        if  p - 1 < 0:
            ans += P_Train[i][q]
        else:
            ans += P_Train[i][q] - P_Train[i][p - 1]
    return ans


if __name__ == '__main__':
    main()

Solution (Python):
N,M,Q=map(int,input().split())
x=[[0]*N for i in range(N)]
for i in range(M):
    L,R=map(lambda x:int(x)-1,input().split())
    x[L][R]+=1
cum=[[0]*(N+1) for i in range(N+1)]
for l in range(N):
    for r in range(N):
        cum[l][r+1]=cum[l][r]+x[l][r]
for i in range(Q):
    p,q=map(lambda x:int(x)-1,input().split())
    res=0
    for l in range(p,q+1):
        res+=cum[l][q+1]-cum[l][p]
    print(res)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, M, Q = map(int, input().split())
    LR = [LI_() for _ in range(M)]
    pq = [LI_() for _ in range(Q)]

    cnt = [[0]*N for _ in range(N)]
    for i in LR:
        cnt[i[0]][i[1]] += 1

    acc = [[0]*(N+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(N):
            acc[i+1][j+1] = acc[i+1][j] + cnt[i][j]
    for i in range(N):
        for j in range(N):
            acc[i+1][j+1] += acc[i][j+1]

    for i in pq:
        p = i[0]
        q = i[1]
        ans = acc[q+1][q+1] - acc[p][q+1] - acc[q+1][p] + acc[p][p]
        print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
n, m, q = map(int, input().split())

train = [[0 for _ in range(501)] for _ in range(501)]
train_total = [[0 for _ in range(501)] for _ in range(501)]
query = []

for i in range(m):
    a, b = map(int, input().split())
    train[a][b] += 1

for j in range(q):
    x, y = map(int, input().split())
    query.append([x, y])

for left in range(0, 501):
    cnt = 0
    for right in range(0, 501):
        cnt += train[left][right]
        train_total[left][right] = cnt

for left, right in query:
    total = 0
    for row in range(left, right+1):
        num2 = train_total[row][right]
        num1 = train_total[row][left-1]
        total += (num2 - num1)
    print(total)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)



def da_generate(h, w, a):
    da = [[0] * w for j in range(h)]
    da[0][0] = a[0][0]
    for i in range(1, w):
        da[0][i] = da[0][i-1] + a[0][i]
    for i in range(1, h):
        cnt_w = 0
        for j in range(w):
            cnt_w += a[i][j]
            da[i][j] = da[i-1][j] + cnt_w
    return da



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


n, m, q = map(int, readline().split())
lr = [[0] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
    l, r = map(int, readline().split())
    lr[l][r] += 1
da = da_generate(n + 1, n + 1, lr)
for _ in range(q):
    p, q = map(int, readline().split())
    print(da_calc(p, p, q, q))

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m,q = inpl()
g = [[0]*n for _ in range(n)]
gg = [[0]*n for _ in range(n)]
for _ in range(m):
    l,r = inpl()
    l,r = l-1, r-1
    g[l][r] += 1
for i in range(n)[::-1]:
    for j in range(n):
        
        if i == n-1:
            gg[i][j] += g[i][j]
            continue
        gg[i][j] += g[i][j] + gg[i+1][j]

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


for _ in range(q):
    a,b = inpl()
    a,b = a-1,b-1
    print(gg[a][b])

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

A = [[0] * (N + 1) for _ in range(N + 1)]
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 j in range(1, N + 1):
    for i in range(1, N + 1):
        A[i][j] += A[i - 1][j]

ans = []
for _ in range(Q):
    p, q = map(int, input().split())
    p -= 1
    ans.append(A[q][q] - A[q][p] - A[p][q] + A[p][p])
print(*ans, sep='\n')

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

A = [[0] * (N + 1) for _ in range(N + 1)]
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 j in range(1, N + 1):
    for i in range(1, N + 1):
        A[i][j] += A[i - 1][j]

ans = []
for _ in range(Q):
    p, q = map(int, input().split())
    p -= 1
    ans.append(A[q][q] - A[q][p] - A[p][q] + A[p][p])
print(*ans, sep='\n')

Solution (Python):
n, m, q = map(int, input().split())

dia = [[0]*(n+1) for i in range(n+1)]
for i in range(m):
  l, r = map(int, input().split())
  dia[l][r] += 1
  
lst = [[0]*(n+1) for i in range(n+1)]
for i in range(1, n+1):
  for j in range(i, n+1):
    lst[i][j] = lst[i][j-1] + dia[i][j]
    
for i in range(q):
  l, r = map(int, input().split())
  count = 0
  for j in range(l, r+1):
    count += lst[j][r]
  print(count)

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):
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[N][1] = A[N][1]
for j in range(2,N+1):
    dp[N][j] = dp[N][j-1]+A[N][j]
for i in range(N-1,0,-1):
    dp[i][1] = dp[i+1][1]+A[i][1]
for i in range(N-1,0,-1):
    for j in range(2,N+1):
        dp[i][j] = A[i][j]+dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]
for _ in range(Q):
    p,q = map(int,input().split())
    print(dp[p][q])

Solution (Python):
n,m,q = map(int,input().split())
trains = [[0]*(n+1) for _ in range(n+1)]
ans = [[0]*(n+1) for _ in range(n+1)]
for i in range(m):
    l,r = map(int,input().split())
    trains[l][r] += 1
for i in range(1,n+1):
    cur = 0
    for j in range(i,n+1):
        cur += trains[i][j]
        ans[i][j] = cur
for i in range(q):
    p,q = map(int,input().split())
    ansi = 0
    for j in range(p,q+1):
        ansi += ans[j][q]
    print(ansi)

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

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

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

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

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

    
    

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

if __name__ == '__main__':
    main()

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

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

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

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

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




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

Solution (Python):
N, M, Q = map(int, input().split())
wa = [[0]*(N+1) for _ in range(N+1)]
import numpy as np

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

cwa = np.cumsum(np.cumsum(wa, axis=0), axis=1).tolist()
for i in range(Q):
    p, q = map(int, input().split())
    ans = cwa[N][q]-cwa[p-1][q]
    print(ans)

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

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

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

    for L in range(1, N + 1):
        for R in range(1, N + 1):
            tb[L][R] += tb[L - 1][R]
            tb[L][R] += tb[L][R - 1]
            tb[L][R] -= tb[L - 1][R - 1]

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

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Solution (Python):
n,m,q = map(int, input().split())

A = [[0 for i in range(n+1)] for j in range(n+1)]
Acum = [[0 for i in range(n+1)] for j in range(n+1)]

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


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


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

Solution (Python):
N,M,Q = map(int,input().split())
m = [[0 for i in range(N+1)] for i in range(N+1)]
for i in range(M):
  l,r = map(int,input().split())
  m[l][r] += 1
for i in range(N+1):
  for j in range(N):
    m[i][j+1] += m[i][j]
for i in range(N):
  for j in range(N+1):
    m[i+1][j] += m[i][j]    

p = [0]
q = [0]
for i in range(Q):
  a,b = map(int,input().split())
  p.append(a)
  q.append(b)


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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n,m,q = map(int,input().split())
d = [{} for _ in range(n)]
for i in range(m):
    l,r = map(int,input().split())
    l -= 1
    r -= 1
    if r not in d[l].keys():
        d[l][r] = 0
    d[l][r] += 1
for i in range(n-1)[::-1]:
    for k,v in d[i+1].items():
        if k not in d[i]:d[i][k] = 0
        d[i][k] += v
ans = [0]*q
for i in range(q):
    P,Q = map(int,input().split())
    P -= 1
    Q -= 1
    for k,v in d[P].items():
        if k <= Q:
            ans[i] += v
print('\n'.join(str(i) for i in ans))

Solution (Python):
N,M,Q = map(int,input().split())
c_sum = [[0 for j in range(N+1)] for i in range(N+1)]

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



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

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

Solution (Python):
class Ruiseki2D:
    """二次元累積和を構築する"""
    def __init__(self, matrix):
        self.h = len(matrix)
        self.w = len(matrix[0])
        self.ru = [[0] * (self.w + 1) for _ in range(self.h + 1)]

        for i in range(self.h):
            for j in range(self.w):
                self.ru[i + 1][j + 1] = self.ru[i + 1][j] + matrix[i][j]
        for i in range(self.h):
            for j in range(self.w):
                self.ru[i + 1][j + 1] += self.ru[i][j + 1]

    def get_sum(self, hl, hr, wl, wr):
        """[hl, hr), [wl, wr) で囲まれた部分の和を求める"""
        return (self.ru[hr][wr] + self.ru[hl][wl]
                - self.ru[hr][wl] - self.ru[hl][wr])

n, m, q = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
query = [list(map(int, input().split())) for i in range(q)]

matrix = [[0] * (n + 1) for i in range(n + 1)]
for l, r in info:
    matrix[l][r] += 1

ru = Ruiseki2D(matrix)

for q1, q2 in query:
    print(ru.get_sum(q1, q2 + 1, q1, q2 + 1))

Solution (Python):
import sys
input = sys.stdin.readline
N,M,Q = map(int,input().split())
LR = [tuple(map(int,input().split())) for i in range(M)]
PQ = [tuple(map(int,input().split())) for i in range(Q)]

imos = [[0]*(N+1) for _ in range(N+1)]
for l,r in LR:
    imos[l][r] += 1
for i in range(N+1):
    for j in range(N):
        imos[i][j+1] += imos[i][j]
for j in range(N+1):
    for i in range(N):
        imos[i+1][j] += imos[i][j]

ans = []
for p,q in PQ:
    p -= 1
    a = imos[q][q] + imos[p][p] - imos[p][q] - imos[q][p]
    ans.append(a)
print(*ans, sep='\n')

Solution (Python):
n,m,q = map(int,input().split())

M = [[0]*(n+1) for _ in range(n+1)]
for i in range(m):
  l,r = map(int,input().split())
  M[r][l] += 1
  
D = [[0]*(n+1) for _ in range(n+1)]
for i in range(1,n+1):
  for j in range(1,n+1):
    D[i][j] = M[i][j] + D[i][j-1] + D[i-1][j] - D[i-1][j-1]
    
for i in range(q):
  p,q = map(int,input().split())
  ans = D[q][q] - D[q][p-1] - D[p-1][p] + D[p-1][p-1]
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, M, Q = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
PQ = [list(map(int, input().split())) for _ in range(Q)]


X = [[0 for _ in range(N+1)] for _ in range(N+1)]
for l, r in LR:
    X[l][r] += 1
cum = [[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(1, N+1):
    for j in range(1, N+1):
        cum[i][j] = cum[i][j-1] + X[i][j]

outputs = []
for p, q in PQ:
    ans = 0
    for i in range(p, q+1):
        ans += cum[i][q] - cum[i][p-1]
    outputs.append(ans)

print('\n'.join(map(str, outputs)))

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


X = [[0 for _ in range(N+1)] for _ in range(N+1)]
for l, r in LR:
    X[l][r] += 1
cum = [[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(1, N+1):
    for j in range(1, N+1):
        cum[i][j] = cum[i][j-1] + X[i][j]

for p, q in PQ:
    ans = 0
    for i in range(p, q+1):
        ans += cum[i][q] - cum[i][p-1]
    print(ans)

Solution (Python):
N, M, Q = map(int, input().split())
import numpy as np
LR = [[0]*(N+1) for _ in range(N+1)]

for i in range(1, M+1):
    l, r = map(int, input().split())
    LR[l][r] += 1
cLR = np.cumsum(np.cumsum(LR, axis=0), axis=1)

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

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

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
dp = []
train = []

def subq(l, r):
    global dp, train
    if l == 0 or r == 0:
        return 0
    else:
        tmp1 = dp[l][r]
        if tmp1 != -1:
            return tmp1
        else:
            tmp2 = train[l][r] + subq(l - 1, r) + subq(l, r - 1) -  subq(l - 1, r - 1)
            dp[l][r] = tmp2
            return tmp2

def main():
    global dp, train
    n, m, qn = map(int, input().split())
    dp = [[-1] * (n+1) for _ in range(n+1)]
    train = [[0] * (n+1) for _ in range(n+1)]
    for _ in range(m):
        l, r = map(int, input().split())
        train[int(l)][int(r)] += 1

    for _ in range(qn):
        p, q = map(int, input().split())
        r = subq(q, q) - subq(p-1, q) - subq(q, p-1) + subq(p-1, p-1)
        print(r)

if __name__ == '__main__':
    main()

Solution (Python):

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


lr = [[0 for i in range(N + 1)] for j in range(N + 1)]

imos = [[0 for i in range(N + 1)] for j in range(N + 1)]

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

for i in range(1, N + 1):
    for j in range(1, N + 1):
        imos[i][j] = imos[i][j-1] + lr[i][j]
res = []
for i in range(Q):
    ans = 0
    p, q = map(int, input().split())
    for i in range(p, q + 1):
        ans += imos[i][q]
    res.append(ans)

for row in res:
    print(row)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
P = []
LR = [[0] * (N+1) for _ in range(N+1)] 
for m in range(M):
    l, r = map(int, input().split())
    LR[l][r] += 1

for q in range(Q):
    p = list(map(int, input().split()))
    P.append(p)

csum = []
for L in LR:
    a = list(itertools.accumulate(L))
    csum.append(a)

for p in P:
    ans = 0
    l, r = p
    for c in csum[l:r+1]:
        ans += (c[r] - c[l-1])
    print (ans)

Solution (Python):





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


trains = [[0 for _ in range(N)] for _ in range(N)]

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



accum = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(i,N):
        if i == j:
            accum[i][j] = trains[i][j]
        else:
            accum[i][j] = accum[i][j-1] + trains[i][j]

for p, q in query:
    cnt = 0
    for i in range(p-1, q):
        cnt += accum[i][q-1]

    print(cnt)

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

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

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