-
Notifications
You must be signed in to change notification settings - Fork 0
/
q_jitclass.py
55 lines (42 loc) · 1.11 KB
/
q_jitclass.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import numpy as np
import numba
try:
from numba.experimental import jitclass
except ImportError:
from numba import jitclass
input = sys.stdin.readline
@jitclass([("n", numba.int32), ("data", numba.int64[:]), ])
class BIT:
def __init__(self, n):
self.n = n
self.data = np.zeros(n, dtype=np.int64)
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)
@numba.njit('int64(int32, int64[:, :])', cache=True)
def solve(n, p):
bit = BIT(n)
ans = 0
for i in range(len(p)):
l, r = p[i]
ans += bit.sum(l + 1, r)
bit.add(r, 1)
return ans
def main():
n, m = map(int, input().split())
p = [[int(x) - 1 for x in input().split()] for _ in range(m)]
p.sort(key=lambda x: (x[0], -x[1]))
print(solve(n, np.array(p, dtype=np.int64)))
if __name__ == "__main__":
main()