-
Notifications
You must be signed in to change notification settings - Fork 0
/
q.py
42 lines (31 loc) · 786 Bytes
/
q.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
input = sys.stdin.readline
class BIT:
def __init__(self, n):
self.__n = n
self.__data = [0] * n
def add(self, i, x):
i += 1
while i <= self.__n:
self.__data[i - 1] += x
i += i & -i
def _sum(self, r):
s = 0
while r > 0:
s += self.__data[r - 1]
r -= r & -r
return s
def sum(self, l, r):
return self._sum(r) - self._sum(l)
def main():
n, m = map(int, input().split())
p = [tuple(int(x) - 1 for x in input().split()) for _ in range(m)]
p.sort(key=lambda x: (x[0], -x[1]))
bit = BIT(n)
ans = 0
for l, r in p:
ans += bit.sum(l + 1, r)
bit.add(r, 1)
print(ans)
if __name__ == "__main__":
main()