They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i).
Now Sonam ask queries in which she gives rank of the student (rank_{i}) and you have to tell marks obtained by that student. Simply, for each query output marks obtain by student whose rank is rank_{i}(1<=rank_{i}<=10^{18}).
Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1.
Example 1:
Input:
n=3, q=3 
l[] = {1, 12, 22} 
r[] = {10, 20, 30} 
rank[] = {5, 15, 25}
Output:
5 16 27
Intervals are from 1 to 10, second
interval from 12 to 20 and third 22 to 30.
In this test case, from 1 to 10 , they
are given the ranks from 1 to 10 but
in the second interval, it is starting
from 12 , so we will have to give its rank
11 and so on like this.
Rank:    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15......
Marks: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16.....
So 5th rank will score 5 marks,15th rank will
score 16 marks and 25th rank will score 27 marks.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getTestMarks() which takes the array l[], r[], its size n and array rank[], StoreAnswer[], its size q as inputs and fill the StoreAnswer[] array. This array stores the answer of every query.
Expected Time Complexity: O(n. log(n))
Expected Auxiliary Space: O(n.q)
Constraints:
1<=n<=10^{5}
1<=q<=10^{5}
1<= l(i) < r(i) <=10^{18}
1<=rank_{i}<=10^{18}

Difficulty: MEDIUM
Tags: Data structures

Complete search
Skill types: Data structures

Complete search
Expected Time Complexity: O(n. log(n))
Expected Auxiliary Space: O(n.q)

Starter code:
#User function Template for python3



class Solution:

    def getTestMarks(self, n, q, r, l, rank, storeAnswer):

        # every array is 1-indexed

        

        

        

        

Solutions:
class Solution:

	def getTestMarks(self, n, q, r, l, rank, storeAnswer):
		score = []
		for i in range(1, n + 1):
			score.append((l[i], r[i]))
		score.sort()
		cnt = []
		for i in range(n):
			curr = score[i][1] - score[i][0] + 1
			if i == 0:
				cnt.append(curr)
			else:
				cnt.append(cnt[i - 1] + curr)
		for i in range(1, q + 1):
			(lo, hi) = (0, n - 1)
			ans = -1
			while lo <= hi:
				mi = (lo + hi) // 2
				if cnt[mi] >= rank[i]:
					ans = mi
					hi = mi - 1
				else:
					lo = mi + 1
			if ans == -1:
				storeAnswer[i] = score[n - 1][1] + rank[i] - cnt[n - 1]
			else:
				before = cnt[ans - 1] if ans > 0 else 0
				storeAnswer[i] = rank[i] - before + score[ans][0] - 1


class Solution:

	def fun(self, arr, num):
		i = 0
		j = len(arr) - 1
		mid = (i + j) // 2
		while i <= j:
			mid = (i + j) // 2
			if arr[mid] > num:
				j = mid - 1
			elif arr[mid] < num:
				i = mid + 1
			else:
				return mid
		return mid

	def getTestMarks(self, n, q, r, l, rank, storeAnswer):
		temp = []
		temp.append(r[1] - l[1] + 1)
		for i in range(2, n + 1):
			temp.append(r[i] - l[i] + 1 + temp[-1])
		miss = []
		miss.append(0)
		for i in range(2, n + 1):
			miss.append(miss[-1] + l[i] - r[i - 1] - 1)
		for i in range(1, q + 1):
			t = self.fun(temp, rank[i])
			if t + 1 < n and temp[t] < rank[i] and (temp[t + 1] > rank[i]):
				t += 1
			storeAnswer[i] = miss[t] + rank[i]


def construct(groups: list):
	sum_ = 0
	data = []
	for group in groups:
		data.append((sum_ + 1, group[0], group[1]))
		sum_ += group[1] - group[0] + 1
	return data

def search(tgt: int, data: list):
	l = 0
	r = len(data)
	while l < r:
		mid = l + (r - l) // 2
		if data[mid][0] < tgt:
			l = mid + 1
		else:
			r = mid
	if l < len(data) and data[l][0] == tgt:
		tgt_i = l
	else:
		tgt_i = l - 1
	tgt_group = data[tgt_i]
	rank_in_group = tgt - tgt_group[0]
	return tgt_group[1] + rank_in_group

class Solution:

	def getTestMarks(self, n, q, r, l, rank, storeAnswer):
		new_l = []
		new_r = []
		for i in r:
			if i != 0:
				new_r.append(i)
		for j in l:
			if j != 0:
				new_l.append(j)
		groups = list(zip(new_l, new_r))
		data = construct(groups)
		for i in range(len(rank)):
			storeAnswer[i] = search(rank[i], data)