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:
import itertools
import sys

input = sys.stdin.readline

n, m, q = map(int, input().split())
ls = [[0] * n for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    ls[l - 1][r - l] += 1
for i in range(n):
    ls[i] = list(itertools.accumulate(ls[i]))
for _ in range(q):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    res = 0
    for i in range(r - l + 1):
        res += ls[l + i][r - l - i]
    print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, q = LI()
L = LIR(m)
query = LIR(q)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for l, r in L:
    dp[l][r] += 1


cumsum = [list(accumulate(l)) for l in dp]
cumsum = list(zip(*[list(accumulate(l)) for l in zip(*cumsum)]))
for l, r in query:
    print(cumsum[r][r] - cumsum[l - 1][r] - cumsum[r][l - 1] + cumsum[l - 1][l - 1])

Submission status: Accepted

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

def cumsum(inlist):
    s = 0
    outlist = []
    for i in inlist:
        s += i
        outlist.append(s)
    return outlist

n, m, q = [ int(v) for v in input().split() ]

train_list = [ [ 0 for i in range(n) ] for i in range(n) ]
for i in range(m):
    a, b = [ int(v)-1 for v in input().split() ]
    train_list[a][b] += 1

train_cumsumx_list = [ cumsum(i) for i in train_list ]
train_cumsumx_list_t = [ [ train_cumsumx_list[i][j] for i in range(n) ] for j in range(n) ]
train_cumsum_list_t = [ cumsum(i) for i in train_cumsumx_list_t ]
train_cumsum_list = [ [0] * n ] + [ [ train_cumsum_list_t[i][j] for i in range(n) ] for j in range(n) ]


for i in range(q):
    c, d = [ int(v)-1 for v in input().split() ]
    print(train_cumsum_list[-1][d] - train_cumsum_list[c][d])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print((a-1)*(b-1))
    return

#B
def B():
    def est(n):
        res = 0
        for i in range(1, int(n ** 0.5) + 1):
            if not n % i:
                res += 2
        res -= float.is_integer(n ** 0.5)
        return res == 8
    n = II()
    ans = 0
    for i in range(1, n + 1, 2):
        ans += est(i)
    print(ans)
    return

#C
def C():
    s = S()
    k = II()
    for i in range(k):
        if s[i] != "1":
            print(s[i])
            return
    print(s[k-1])
    return

# D
# 解説AC
# 始点と終点を2次元座標に落とし込むことで解ける
# 難しい
def D():
    n, m, q = LI()
    c = [[0] * (n+1) for i in range(n+1)]
    for _ in range(m):
        l, r = LI()
        c[r][l] += 1
    for y in range(1, n + 1):
        cy = c[y]
        for x in range(1, n + 1):
            cy[x] += cy[x - 1]
    for x in range(1, n + 1):
        for y in range(1, n + 1):
            c[y][x] += c[y - 1][x]
    for _ in range(q):
        p, q = LI()
        ans = c[q][q] - c[q][p - 1] - c[p - 1][q] + c[p - 1][p - 1]
        print(ans)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print((a-1)*(b-1))
    return

#B
def B():
    def est(n):
        res = 0
        for i in range(1, int(n ** 0.5) + 1):
            if not n % i:
                res += 2
        res -= float.is_integer(n ** 0.5)
        return res == 8
    n = II()
    ans = 0
    for i in range(1, n + 1, 2):
        ans += est(i)
    print(ans)
    return

#C
def C():
    s = S()
    k = II()
    for i in range(k):
        if s[i] != "1":
            print(s[i])
            return
    print(s[k-1])
    return

# D
# 解説AC
# 始点と終点を2次元座標に落とし込むことで解ける
# 難しい
def D():
    n, m, q = LI()
    c = [[0] * (n+1) for i in range(n+1)]
    for _ in range(m):
        l, r = LI()
        c[r][l] += 1
    for y in range(1, n + 1):
        for x in range(1, n + 1):
            c[y][x] += c[y][x - 1]
    for x in range(1, n + 1):
        for y in range(1, n + 1):
            c[y][x] += c[y - 1][x]
    for _ in range(q):
        p, q = LI()
        ans = c[q][q] - c[q][p - 1] - c[p - 1][q] + c[p - 1][p - 1]
        print(ans)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    print((a-1)*(b-1))
    return

#B
def B():
    def est(n):
        res = 0
        for i in range(1, int(n ** 0.5) + 1):
            if not n % i:
                res += 2
        res -= float.is_integer(n ** 0.5)
        return res == 8
    n = II()
    ans = 0
    for i in range(1, n + 1, 2):
        ans += est(i)
    print(ans)
    return

#C
def C():
    s = S()
    k = II()
    for i in range(k):
        if s[i] != "1":
            print(s[i])
            return
    print(s[k-1])
    return

# D
# 解説AC
# 始点と終点を2次元座標に落とし込むことで解ける
# 難しい
def D():
    n, m, q = LI()
    c = [[0] * n for i in range(n)]
    for _ in range(m):
        l, r = LI_()
        c[r][l] += 1
    for y in range(n):
        for x in range(1, n):
            c[y][x] += c[y][x - 1]
    for _ in range(q):
        p, q = LI_()
        ans = 0
        for qi in range(p, q + 1):
            ans += c[qi][q] - (c[qi][p - 1] if p != 0 else 0)
        print(ans)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
lst = []
cnt = [0]*N
ans = [0]*Q
for i in range(M):
    a, b = map(int, input().split())
    lst.append([b, a, 't'])
for i in range(Q):
    a, b = map(int, input().split())
    lst.append([b, a+500, i])
lst.sort()
for i in lst:
    if i[2] == 't':
        cnt[i[1]-1] += 1
    else:
        ans[i[2]] = sum(cnt[i[1]-501:i[0]])
for j in ans:
    print(j)

Submission status: Accepted

Solution code:
import numpy as np
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N,M,Q = map(int,readline().split())
lines = readlines()
L,R = np.array([line.split() for line in lines[:M]],np.int32).T
P,Q = np.array([line.split() for line in lines[M:]],np.int32).T

cnt = np.zeros((N+1,N+1),dtype=np.int32)
np.add.at(cnt,(L,R),1)

np.cumsum(cnt,axis=0,out=cnt)
np.cumsum(cnt,axis=1,out=cnt)

P -= 1
x = cnt[Q,Q] - cnt[Q,P] - cnt[P,Q] + cnt[P,P]
print('\n'.join(x.astype(str)))

Submission status: Accepted

Solution code:
# i が与えられたとき a1~aiまでの和を計算する( O(log n) )
def bitsum(bit,i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= i&(-i)
    return s
    
# i と x が与えられたとき ai += x とする ( O(log n) )
def bitadd(bit,i,x):
    while i <= N:
        bit[i] += x
        i += i&(-i)
    return bit
import sys
import math
input = sys.stdin.readline
N,M,Q = [int(x) for x in input().split()]
qinfo = []
for i in range(M+Q):
  if i<M:
    L,R = [int(x) for x in input().split()]
    qinfo.append([L,R,-1])
  if i>=M:
    L,R = [int(x) for x in input().split()]
    qinfo.append([L,R,i-M])
qinfo = sorted(qinfo,key=lambda x : x[1])
#print(qinfo)
n_ = int(math.log2(N)) + 1
bit = [0] * (2**n_)
bit.insert(0,0)
ans = [0]*(Q+1)
for i in range(M+Q):
  if qinfo[i][2]==-1:
    bit = bitadd(bit,qinfo[i][0],1)
  else:
    ans[qinfo[i][2]]=bitsum(bit,qinfo[i][1])-bitsum(bit,qinfo[i][0]-1)
for i in range(Q):
  print(ans[i])

Submission status: Accepted

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

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

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

for x,y in [map(int,input().split()) for i in range(q)]:
    print(book[y][y]-book[y][x-1]-book[x-1][y]+book[x-1][x-1])

Submission status: Accepted

Solution code:
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)]
dual = [[0]*(N+1) for _ in range(N+1)]

for l,r in LR:
    dual[l][r]+=1

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

for p,q in pq:
    #print(dp[q][q],dp[q-1][p-1],dp[p-1][q-1],dp[p-1][p-1])
    print(dp[q][q]-dp[q][p-1]-dp[p-1][q]+dp[p-1][p-1])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N, M, Q = map(int, input().split())

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

lst = np.array(lst)
# print (lst)

#2次元累積和
for i in range(1, N + 1):
    lst[:,i] += lst[:, i - 1]
for j in range(1, N + 1):
    lst[j,:] += lst[j - 1, :]


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

# print (lst)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

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



main()

Submission status: Accepted

Solution code:
def main():
    N, M, Q = map(int, input().split())
    S = [[0] * (N+1) for _ in range(N+1)]
    for i in range(M):
        L, R = map(int, input().split())
        S[L][R] += 1

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

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



main()

Submission status: Accepted

Solution code:
def main():

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

    accum = [[0 for _ in range(N+1)]]
    for i in range(N):
        temp = [0]
        v = 0
        for j in range(N):
            v += train[i][j]
            temp.append(accum[-1][j+1]+v)
        accum.append(temp)
        # print(temp)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#17:43
n,m,q = map(int,input().split())
imos = [[0 for _ in range(n+1)] for _ in range(n+1)]
for _ in range(m):
  l,r = map(int,input().split())
  r -= 1
  imos[0][r] += 1
  imos[l][r] -= 1
#print(imos)
for i in range(n):
  for j in range(n):
    imos[i][j+1] += imos[i][j]
for j in range(n):
  for i in range(n):
    imos[i+1][j] += imos[i][j]
#print(imos)
for _ in range(q):
  x,y = map(int,input().split())
  x -= 1
  y -= 1
  print(imos[x][y])

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

from bisect import bisect_right
from operator import itemgetter

N,M,Q = II()
x = []
for _ in range(M):
    L,R = II()
    x.append((L-1, R-1))
query = []
for _ in range(Q):
    p0,q0 = II()
    query.append((p0-1, q0-1))

x.sort(key=itemgetter(1))

y = [[10**5+1]*M for _ in range(N)]
c = [0]*N
for i in range(M):
    for j in range(x[i][0]+1):
        y[j][c[j]] = x[i][1]
        c[j] += 1

ans = 0
for q in query:
    print(bisect_right(y[q[0]], q[1]))

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,M,Q = II()
x = []
for i in range(M):
    L,R = II()
    x.append([L-1, R-1])

p = [0]*Q
q = [0]*Q
for i in range(Q):
    p0,q0 = II()
    p[i] = p0-1
    q[i] = q0-1

a = [[0]*N for _ in range(N)]
for i in range(M):
    a[x[i][0]][x[i][1]] += 1

c = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if j==0:
            temp = 0
        else:
            temp = c[i][j-1]
        c[i][j] = temp + a[i][j]

for i in range(Q):
    ans = 0
    for j in range(p[i],q[i]+1):
        ans += c[j][q[i]]
    print(ans)

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
xy = [[0 for _ in range(N)] for _ in range(N)]
for _ in range(M):
    L, R = map(int, input().split())
    L = N - L + 1
    xy[L - 1][R - 1] += 1

xy_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):
        xy_cum[i][j] = xy[i - 1][j - 1] + xy_cum[i - 1][j] + xy_cum[i][j - 1] - xy_cum[i - 1][j - 1]

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

Submission status: Accepted

Solution code:
class Acc2D:
    def __init__(self, H, W, tab=None):
        self.tab=[row[:] for row in tab] if tab else [[0]*W for _ in range(H)]
        self.H, self.W = H,W

    def add(self, x, y, val):
        self.tab[y][x]+=val
    
    def build(self):
        for y in range(self.H):
            for x in range(1,self.W):
                self.tab[y][x]+=self.tab[y][x-1]
        
        for y in range(1,self.H):
            for x in range(self.W):
                self.tab[y][x]+=self.tab[y-1][x]
    
    def query(self,x0,y0,x1,y1):
        """
        find number of points in [x0,x1)x[y0,y1)
        (x1>x0 and y1>y0)
        """
        return self.tab[y1][x1]-self.tab[y1][x0]-self.tab[y0][x1]+self.tab[y0][x0]
    
N,M,Q=map(int,input().split())
acc = Acc2D(N+1,N+1)

for _ in range(M):
    l,r = map(int,input().split())
    acc.add(l,r,1)

acc.build()

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

Submission status: Accepted

Solution code:
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)]
tab=[[0]*(N+1) for _ in range(N+1)]
for l,r in LR:
    tab[l][r]+=1

for y in range(N+1):
    for x in range(1,N+1):
        tab[y][x]+=tab[y][x-1]

for y in range(1,N+1):
    for x in range(N+1):
        tab[y][x]+=tab[y-1][x]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
image = [[0] * n for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    image[l - 1][r - 1] += 1
sumimage = [[0] * n for _ in range(n)]
for l in range(n - 1, -1, -1):
    for r in range(l, n):
        if l == r:
            sumimage[l][r] = image[l][r]
        else:
            sumimage[l][r] = image[l][r] + sumimage[l + 1][r] + sumimage[l][r - 1] - sumimage[l + 1][r - 1]
for _ in range(q):
    a, b = map(int, input().split())
    c = sumimage[a - 1][b - 1]
    print(c)

Submission status: Accepted

Solution code:
#作っているものが違う
#作らなきゃいけないのは、「完全に被覆されている区間の数」を出すやつ
#作ったのは「部分的あるいは完全に被覆されている区間の数」を出すやつ

#いもす法で各地点における路線の通っている数をO(N + M)で配列化
#それをセグ木にしてO(logN)でクエリ1つずつ回答
#dc配列はダブルカウントを判定する
#例えば、1->2の路線があったとして、区間[1, 2]の路線数を取ると2になってしまう
#これは、自身しか通過しない路線を無視した路線数累積和でO(1)になる気がする（証明してない）

#ミスって自信満々に作ったクソ長レシートより
#数分で書いた正答コードの方が圧倒的に簡潔なの、あほですか？という感じ
#問題文と入出力例はよく読みましょう

"""
N, M, Q = map(int, input().split())
A = [0] * (N + 2)
X = [0] * (N + 2)
dc = [0] * (N + 2)
for i in range(M):
  l, r = map(int, input().split())
  A[l] += 1
  A[r + 1] -= 1
  if l == r:
    dc[l] -= 1
plus = 0
for i in range(1, N + 1):
  plus += A[i]
  X[i] += plus
  dc[i] += plus + dc[i - 1]
print(X)
ide_ele = 0
num = 2 ** (N + 2 - 1).bit_length()
seg = [ide_ele] * 2 * num

def segfunc(x, y):
  return x + y

def init(init_val):
  for i in range(N + 2):
    seg[i + num - 1] = init_val[i]    
  for i in range(num - 2, -1, -1) :
    seg[i]=segfunc(seg[2 * i + 1], seg[2 * i + 2]) 
    
def update(k, x):
  k += num - 1
  seg[k] = x
  while k + 1:
    k = (k - 1) // 2
    seg[k] = segfunc(seg[k * 2 + 1], seg[k * 2 + 2])
    
def query(p, q):
  if q <= p:
    return ide_ele
  p += num - 1
  q += num - 2
  res = ide_ele
  while q - p > 1:
    if p & 1 == 0:
      res = segfunc(res, seg[p])
    if q & 1 == 1:
      res = segfunc(res, seg[q])
      q -= 1
    p = p // 2
    q = (q - 1) // 2
  if p == q:
    res = segfunc(res, seg[p])
  else:
    res = segfunc(segfunc(res, seg[p]), seg[q])
  return res

init(X)
print(seg)
print(dc)
for i in range(Q):
  p, q = map(int, input().split())
  k = query(p, q + 1)
  print(k - dc[q] + dc[p])
"""

#二次元累積和しろ
#N = 500なので累積和をN回でも間に合う
N, M, Q = map(int, input().split())
LR = [[0] * (N + 2) for _ in range(N + 2)]
cs = [[0] * (N + 2) for _ in range(N + 2)]
for _ in range(M):
  l, r = map(int, input().split())
  LR[l][r] += 1
for i in range(N + 1):
  for j in range(N + 1):
    cs[i][j + 1] += cs[i][j] + LR[i][j + 1]
#print(cs)
#print(LR)
for _ in range(Q):
  l, r = map(int, input().split())
  temp = 0
  for i in range(l, r + 1):
    temp += cs[i][r] - cs[i][l - 1]
  print(temp)

Submission status: Accepted

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

p = [0] * Q
q = [0] * Q
info = [[0] * N for _ in range(N)]
total = [[0] * N for _ in range(N)]

intminus = lambda x:int(x)-1

for s in range(M):
    L,R = map(intminus,input().split())
    info[L][R] += 1
for t in range(Q):
    p[t], q[t] = map(intminus,input().split())

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

for t in range(Q):
    if p[t] != 0:
        print(total[q[t]][q[t]] - total[q[t]][p[t]-1] - total[p[t]-1][q[t]] + total[p[t]-1][p[t]-1])
    else:
        print(total[q[t]][q[t]])

Submission status: Accepted

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


class cumulative_sum_2d():
    def __init__(self, a):
        self.H = len(a)
        self.W = len(a[0])
        self.s = [[0]*(self.W+1) for _ in range(self.H+1)]
        for y in range(self.H):
            for x in range(self.W):
                self.s[y+1][x+1] = self.s[y][x+1] + \
                    self.s[y+1][x] - self.s[y][x] + a[y][x]

    def calc_sum(self, yh, yt, xh, xt):
        return self.s[yt][xt] - self.s[yt][xh-1] - self.s[yh-1][xt] + self.s[yh-1][xh-1]


N, M, Q = map(int, input().split())
table = [[0] * (N) for _ in range(N)]
for i in range(M):
    L, R = map(int, input().split())
    table[L-1][R-1] += 1
Sum = cumulative_sum_2d(table)
for i in range(Q):
    p, q = map(int, input().split())
    print(Sum.calc_sum(p, q, p, q))

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
image = [[0] * n for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    image[l - 1][r - 1] += 1
sumimage = [[0] * (n + 1) for _ in range(n + 1)]
for l in range(1, n + 1):
    s = 0
    for r in range(1, n + 1):
        s += image[l - 1][r - 1]
        sumimage[l][r] = sumimage[l - 1][r] + s
for _ in range(q):
    a, b = map(int, input().split())
    c = sumimage[b][b] - sumimage[a - 1][b]
    print(c)

Submission status: Accepted

Solution code:
N, M, Q = list(map(int, input().split(' ')))
st_ed = {}
for i in range(M):
    l, r = list(map(int, input().split(' ')))
    st_ed[(l, r)] = st_ed.get((l, r), 0) + 1

memo_g = {}
def g(p, q):
    if p == q:
        return st_ed.get((p, q), 0)
    if not (p, q) in memo_g:
        memo_g[(p, q)] = st_ed.get((p, q), 0) + g(p + 1, q)
    return memo_g[(p, q)]

memo_f = {}
# memo[(p, q)] = memo[(p, q-1)] + sum([st_ed.get((i, q), 0) for i in [1,2,3...p]])
def f(p, q):
    if p == q:
        return st_ed.get((p, q), 0)
    if not (p, q) in memo_f:
        temp = f(p, q - 1) + g(p, q)
        memo_f[(p, q)] = temp
    return memo_f[(p, q)]

memo = {}
for i in range(Q):  # 1e5
    p, q = list(map(int, input().split(' ')))
    if not (p, q) in memo:
        temp = f(p, q)
        memo[(p, q)] = temp
    print(memo[(p, q)])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N,M,Q = map(int,input().split())
    
    A = [[0]*(N+2) for i in range((N+2))]
    
    for _ in range(M):
        L,R = map(int,input().split())
        A[R][1] += 1
        A[R][L+1] -= 1
        A[N+1][1] -= 1
        A[N+1][L+1] += 1
    
    for i in range(1,N+2):
        for j in range(1,N+2):
            A[i][j] += A[i][j-1]
    
    for j in range(1,N+2):
        for i in range(1,N+2):
            A[i][j] += A[i-1][j]       
    
    for _ in range(Q):
        p,q = map(int,input().split())
        print(A[q][p])
    
    
main()

Submission status: Accepted

Solution code:
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
L=[[0 for i in range(n)] for j in range(n)]
L2=[[0 for i in range(n+1)] for j in range(n+1)]
for i in range(m):
  L[l[i][0]-1][l[i][1]-1]+=1
for i in range(1,n+1):
  for j in range(1,n+1):
    L2[i][j]=L2[i][j-1]+L[i-1][j-1]
for j in range(1,n+1):
  for i in range(1,n+1):
    L2[i][j]=L2[i-1][j]+L2[i][j]

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

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
G = [[0]*N for i in range(N)]
for i in range(M):
  L, R = map(int, input().split())
  L, R = L-1, R-1
  G[L][R] += 1
#print(G)
S = [[0]*N for i in range(N)]
for i in range(N):
  s = 0
  for j in range(N):
    s += G[i][j]
    S[i][j] = s
#print(S)
T = [[0]*N for i in range(N)]
for j in range(N):
  s = 0
  for i in reversed(range(N)):
    s += S[i][j]
    T[i][j] = s
#print(T)
for i in range(Q):
  p, q = map(int, input().split())
  p, q = p-1, q-1
  print(T[p][q])

Submission status: Accepted

Solution code:

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

lis = []

for i in range(N):

    lis.append([0] * N)

for i in range(M):

    L,R = map(int,input().split())

    lis[L-1][R-1] += 1

ylis = lis.copy()

for i in range(N):

    for j in range(N-1):

        ylis[i][j+1] += ylis[i][j]

tlis = lis.copy()

for i in range(N-1):

    i = N-1-i

    for j in range(N):

        tlis[i-1][j] += tlis[i][j]


for i in range(Q):

    p,q = map(int,input().split())

    print (ylis[p-1][q-1] + tlis[p-1][q-1] - lis[p-1][q-1])

Submission status: Accepted

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

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