AtCoder Regular Contest 101 - Robots and Exits

There are N robots and M exits on a number line.
The N + M coordinates of these are all integers and all distinct.
For each i (1 \leq i \leq N), the coordinate of the i-th robot from the left is x_i.
Also, for each j (1 \leq j \leq M), the coordinate of the j-th exit from the left is y_j.
Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously:
 - Increment the coordinates of all the robots on the number line by 1.
 - Decrement the coordinates of all the robots on the number line by 1.
Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit.
Snuke will continue performing operations until all the robots disappear.
When all the robots disappear, how many combinations of exits can be used by the robots?
Find the count modulo 10^9 + 7.
Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.

-----Constraints-----
 - 1 \leq N, M \leq 10^5
 - 1 \leq x_1 < x_2 < ... < x_N \leq 10^9
 - 1 \leq y_1 < y_2 < ... < y_M \leq 10^9
 - All given coordinates are integers.
 - All given coordinates are distinct.

-----Input-----
Input is given from Standard Input in the following format:
N M
x_1 x_2 ... x_N
y_1 y_2 ... y_M

-----Output-----
Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo 10^9 + 7.

-----Sample Input-----
2 2
2 3
1 4

-----Sample Output-----
3

The i-th robot from the left will be called Robot i, and the j-th exit from the left will be called Exit j.
There are three possible combinations of exits (the exit used by Robot 1, the exit used by Robot 2) as follows:
 - (Exit 1, Exit 1)
 - (Exit 1, Exit 2)
 - (Exit 2, Exit 2)

Difficulty: VERY_HARD

Starter code:

Solutions:
from bisect import bisect
from collections import defaultdict

class Bit:

	def __init__(self, n):
		self.size = n
		self.tree = [0] * (n + 1)
		self.depth = n.bit_length()

	def sum(self, i):
		s = 0
		while i > 0:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i <= self.size:
			self.tree[i] += x
			i += i & -i

	def debug_print(self):
		for i in range(1, self.size + 1):
			j = (i & -i).bit_length()
			print('  ' * j, self.tree[i])

	def lower_bound(self, x):
		sum_ = 0
		pos = 0
		for i in range(self.depth, -1, -1):
			k = pos + (1 << i)
			if k <= self.size and sum_ + self.tree[k] < x:
				sum_ += self.tree[k]
				pos += 1 << i
		return (pos + 1, sum_)
(n, m) = map(int, input().split())
xxx = list(map(int, input().split()))
yyy = list(map(int, input().split()))
ab = defaultdict(set)
coordinates = set()
for x in xxx:
	if x < yyy[0] or yyy[-1] < x:
		continue
	i = bisect(yyy, x)
	a = x - yyy[i - 1]
	b = yyy[i] - x
	ab[a].add(b)
	coordinates.add(b)
cor_dict = {b: i for (i, b) in enumerate(sorted(coordinates), start=2)}
cdg = cor_dict.get
bit = Bit(len(coordinates) + 1)
bit.add(1, 1)
for a in sorted(ab):
	bbb = sorted(map(cdg, ab[a]), reverse=True)
	for b in bbb:
		bit.add(b, bit.sum(b - 1))
print(bit.sum(bit.size) % (10 ** 9 + 7))


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect_left
MOD = 10 ** 9 + 7
(N, M) = map(int, readline().split())
X = list(map(int, readline().split()))
Y = list(map(int, readline().split()))
L = []
R = []
for x in X:
	i = bisect_left(Y, x)
	if i in [0, M]:
		continue
	(y0, y1) = Y[i - 1:i + 1]
	L.append(y0 - x)
	R.append(y1 - x)
Rtoi = {x: i for (i, x) in enumerate(sorted(set(R)), 1)}
R = [Rtoi[r] for r in R]
if len(R) == 0:
	print(1)
	exit()

class BIT:

	def __init__(self, max_n):
		self.size = max_n + 1
		self.tree = [0] * self.size

	def get_sum(self, i):
		s = 0
		while i:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i < self.size:
			self.tree[i] += x
			i += i & -i
dp = BIT(max_n=max(R))
for (_, r) in sorted(set(zip(L, R)), reverse=True):
	x = dp.get_sum(r - 1) + 1
	x %= MOD
	dp.add(r, x)
answer = 1 + dp.get_sum(max(R))
answer %= MOD
print(answer)