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



Sample input:
2 3 1
1 1
1 2
2 2
1 2


Sample output:
3

Submission status: Accepted

Solution code:
# D - AtCoder Express 2 
import sys

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

cusum = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    L, R = map(int, sys.stdin.readline().strip().split())
    cusum[L][R] += 1

# 累積和の計算
for i in range(N):
    for j in range(N):
        cusum[i + 1][j + 1] += cusum[i + 1][j] + cusum[i][j + 1] - cusum[i][j]


for _ in range(Q):
    p, q = map(int, sys.stdin.readline().strip().split())  # p <= q

    ans = cusum[q][q] - cusum[q][p - 1] - cusum[p - 1][q] + cusum[p- 1][p - 1]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    import numpy as np
    ikimasu = sys.stdin.buffer.readline
    ini = lambda: int(ins())
    ina = lambda: list(map(int, ikimasu().split()))
    ins = lambda: ikimasu().strip()
    
    n,m,q = ina()
    tmp = [[0]*(n+1) for _ in range(n+1)]
    for _ in range(m):
        l,r = ina()
        tmp[l][r]+=1


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

    for _ in range(q):
        p,q = ina()
        rick = tmp[q][q]+tmp[p-1][p-1]-tmp[p-1][q]-tmp[q][p-1]
        print(rick)
        


    
        


        


    




















if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
ips = lambda:input().split()
import collections
import math
import itertools
import heapq as hq
n,m,q = ma()
L_rs=[[] for i in range(n)]
for i in range(m):
    l,r=ma()
    L_rs[l-1].append(r-1)
for i in range(n):
    L_rs[i].sort()
# print(L_rs)
def isok(num,val):##適宜変更
    return num<=val
def bisect(ls,val): ##valの関数isok(x,val)がTrueとなる一番右のindex を返す 全部Falseなら-1,Trueならlen(ls)-1
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num,val):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok ##一番右のTrueのindex  Trueの個数はok+1こ

ans = [[0]*(n+1) for i in range(n+1)]
for r in range(n):
    for l in range(r,-1,-1):
        ans[l][r]=ans[l+1][r] + bisect(L_rs[l],r)+1

for i in range(q):
    l,r=ma()
    print(ans[l-1][r-1])

Submission status: Accepted

Solution code:
import numpy as np
N,M,Q=map(int, input().split())
a=np.array([[0]*(N+1) for i in range(N+1)])
for i in range(M):
  l,r=map(int, input().split())
  a[l][r]+=1
a=a.cumsum(axis=0).cumsum(axis=1)
for i in range(Q):
  p,q=map(int, input().split())
  print(a[q][q]-a[p-1][q]-a[q][p-1]+a[p-1][p-1])

Submission status: Accepted

Solution code:
import numpy as np
N,M,Q=map(int, input().split())
a=np.array([[0]*(N+1) for i in range(N+1)])
for i in range(M):
  l,r=map(int, input().split())
  a[l][r]+=1
a=a.cumsum(axis=0).cumsum(axis=1)
for i in range(Q):
  p,q=map(int, input().split())
  print(a[q][q]-a[p-1][q])

Submission status: Accepted

Solution code:
n,m,q=map(int,input().split())
import numpy as np
dim2=np.zeros((n+1,n+1),dtype='int8')

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

dim2=np.cumsum(dim2,axis=0)

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
from operator import itemgetter
#from heapq import heappush, heappop
#import numpy as np
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M, Q = na()
dp = [[0] * (N + 3) for _ in range(N + 3)]
for i in range(M):
    l, r = na()
    dp[1][r] += 1
    dp[l+1][r] -= 1
    dp[l][N+1] -= 1

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

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

for i in range(Q):
    p, q = na()
    print(dp[p][q])

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
lr = []
for _ in range(m):
    lr.append(list(map(int, input().split())))

a = [[0] * n for i in range(n)]
for l, r in lr:
    a[l - 1][r - 1] += 1

cnt = [[0]*n for i in range(n)]
for y in range(n):
    v = 0
    for x in range(y, -1, -1):
        v += a[x][y]
        cnt[x][y] = cnt[x][y - 1] + v

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, M, Q = map(int, input().split())
cum = [[0]*(N+1) for _ in range(N+1)]
for _ in range(M):
    l, r = map(int, input().split())
    cum[l][r] += 1
for i in range(1, N+1):
    for j in range(i+1, N+1):
        cum[i][j] += cum[i][j-1]
ans = []
for _ in range(Q):
    p, q = map(int, input().split())
    tot = 0
    for i in range(p, q+1):
        tot += cum[i][q]
    ans.append(tot)
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, M, Q = map(int, input().split())
cum = [[0]*(N+1) for _ in range(N+1)]
for _ in range(M):
    l, r = map(int, input().split())
    cum[l][r] += 1
for i in range(1, N+1):
    for j in range(i+1, N+1):
        cum[i][j] += cum[i][j-1]
for _ in range(Q):
    p, q = map(int, input().split())
    tot = 0
    for i in range(p, q+1):
        tot += cum[i][q]
    print(tot)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, M, Q = map(int, input().split())
cum = [[0]*(N+1) for _ in range(N+1)]
for _ in range(M):
    l, r = map(int, input().split())
    cum[l][r] += 1
for i in range(1, N+1):
    for j in range(i+1, N+1):
        cum[i][j] = cum[i][j] + cum[i][j-1]
ans = []
for _ in range(Q):
    p, q = map(int, input().split())
    tot = 0
    for i in range(p, q+1):
        tot += cum[i][q]
    ans.append(tot)
print(*ans, sep="\n")

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
def make_sumtable():
    sumtable = [[0]*(N+1) for i in range(N+1)]
    for i in range(M):
        sumtable[L[i]][R[i]] += 1
    for i in range(1,N+1):
        for j in range(1,N+1):
            sumtable[i][j] += sumtable[i-1][j]
            sumtable[i][j] += sumtable[i][j-1]
            sumtable[i][j] -= sumtable[i-1][j-1]
    return sumtable
N,M,Q = map(int,input().split())

L = []
R = []
for i in range(M):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)

sumtable = make_sumtable()

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

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
start_goal = [[0]*n for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    start_goal[l][r] += 1
#print(start_goal)

#2次元累積和
s_2 = [[0]*(n+1) for _ in range(n+1)]
for i in range(n):
    for j in range(n):
        s_2[i+1][j+1] = s_2[i][j+1] + s_2[i+1][j] - s_2[i][j] + start_goal[i][j]
#print(s_2)

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

Submission status: Accepted

Solution code:
import numpy as np 
N,M,Q=map(int, input().split())
a=np.array([[0]*N for i in range(N)])

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

for i in range(Q):
  p,q=map(int, input().split())
  if p==1:
    ans=a[q-1][q-1]
  else:
    ans=a[q-1][q-1]-a[p-2][q-1]
  print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
lr = []
for _ in range(m):
    l, r = map(int, input().split())
    lr.append((l, r))
query = []
for _ in range(q):
    p, q = map(int, input().split())
    query.append((p, q))
accu = [[0]*(n+1) for _ in range(n+1)]
for l, r in lr:
    accu[l][r] += 1
for i in range(n):
    for j in range(n):
        accu[i+1][j+1] += accu[i+1][j]
for j in range(n):
    for i in range(n):
        accu[i+1][j+1] += accu[i][j+1]
ans = []
for p, q in query:
    ans.append(accu[q][q]-accu[q][p-1]-accu[p-1][q]+accu[p-1][p-1])
print(*ans, sep='\n')