The only difference between easy and hard versions is constraints.

If you write a solution in Python, then prefer to send it in PyPy to speed up execution time.

A session has begun at Beland State University. Many students are taking exams.

Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following:  The $i$-th student randomly chooses a ticket.  if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam.  if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. 

Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.

The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam.

For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam.

For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home.


-----Input-----

The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively.

The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket.

It's guaranteed that all values of $t_i$ are not greater than $M$.


-----Output-----

Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam.


-----Examples-----
Input
7 15
1 2 3 4 5 6 7

Output
0 0 0 0 0 2 3 
Input
5 100
80 40 40 40 60

Output
0 1 1 2 3 


-----Note-----

The explanation for the example 1.

Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$.

In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$).

In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).

Difficulty: interview

Starter code:

Solutions:
import sys
input = sys.stdin.readline
n, M = list(map(int, input().split()))
t = list(map(int, input().split()))
ans = []
tmp = [0] * 101
for i in range(n):
    num = 0
    T = t[i]
    for j in range(1, 101):
        if T + j * tmp[j] <= M:
            num += tmp[j]
            T += j * tmp[j]
        else:
            m = M - T
            num += m // j
            break

    ans.append(i - num)
    tmp[t[i]] += 1

print(*ans)


import math

def main():
    n,m = map(int,input().split())
    time = list(map(int,input().split()))
    ans = []
    total = 0
    counts = [0]*100
    for i in range(n):
        if m-total >= time[i]:
            ans.append(0)
        else:
            count = 0
            new = total
            for j in range(99,-1,-1):
                if counts[j] > 0:
                    new -= counts[j]*(j+1)
                    if m-new < time[i]:
                        count += counts[j]
                    else:
                        new += counts[j]*(j+1)
                        count += math.ceil((time[i]+new-m)/(j+1))
                        break
                    #print(new,j+1,counts[j],count)
            ans.append(count)
                
        counts[time[i]-1] += 1
        total += time[i]

    for i in ans:
        print(i,end = ' ')
                

main()


'''input
5 100
80 100 100 40 60
'''
# problem solving is essentially pattern recognition
from sys import stdin, stdout
import math
import collections

# main starts
n, m = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
aux = dict()
for i in range(1, 101):
	aux[i] = 0
cur_sum = 0

for i in range(len(arr)):
	count = 0
	if cur_sum + arr[i] <= m:
		count = 0
	else:
		temp = 0
		for k in range(100, 0, -1):
			temp += k * aux[k]
			if cur_sum - temp + arr[i] <= m:
				temp -= k * aux[k]
				start = 1
				end = aux[k]
				while True:
					if start >= end:
						if cur_sum - temp - ((start - 1) * k) + arr[i] <= m:
							start -= 1
							break
						break

					mid = (start + end) //2 
					if cur_sum - (mid * k) - temp + arr[i] <= m:
						end = mid - 1
					else:
						start  = mid + 1

				if cur_sum - (start * k) - temp + arr[i] <= m:
					count += start
					break
				else:
					count += start + 1
					break
			else:
				count += aux[k]

	cur_sum += arr[i]
	print(count, end = ' ')
	aux[arr[i]] += 1

a = [int(i) for i in input().split()]
n = a[0]
M = a[1]
x = [int(i) for i in input().split()]
pref = [0 for i in x]
pref[0] = x[0]
freq = [0 for i in range(100)]
for i in range(1,n):
	pref[i] = pref[i-1] + x[i] 
for j in range(n):
	ans = pref[j]
	if(ans<=M):
		print(0,end=' ')
	else:
		k = 0
		while(ans>M):
			for i in range(99,-1,-1):
				if(freq[i]==0):
					continue
				y = (ans-M)//(i+1)
				# print(y)
				if(y==0):
					ans = ans - (i+1)
					k+=1
				elif(freq[i]<=y):
					ans = ans - freq[i]*(i+1)
					k+=freq[i]
				else:
					if(ans - (y)*(i+1)<=M):
						ans = ans - (y)*(i+1)
						k+=(y)
					else:
						ans = ans - (y+1)*(i+1)
						k+=(y+1)
				# print(ans,j)
				if(ans<=M):
					break
		print(k,end=' ')
		# print(freq)
	freq[x[j]-1]+=1

from copy import deepcopy
iarr = list(map(int,input().split()))
n = iarr[0]
m = iarr[1]
tar = list(map(int,input().split()))
freq = [0 for i in range(101)]
for i in range(n):
	rem = m-tar[i]
	#yoar = deepcopy(tar[:i])
	#yoar.sort()
	#print(yoar)
	count = 0
	counthr = 0
	for j in range(1,101):
		afteradd = counthr+freq[j]*j
		if afteradd > rem:
			count+= (rem - counthr)//j
			counthr += count*j
			break
		elif afteradd == rem:
			counthr = afteradd
			count+=freq[j]
			break
		else:	
			counthr = afteradd
			count+=freq[j]
		
	freq[tar[i]]+=1
		# if counthr==rem:
		# count = i-count
		# elif counthr < rem:	
		# 	count = 0
		# else:
		# 	count = i-count+1		
	print(i-count,end=" ")
print()

n, m = map(int, input().split())
t = list(map(int, input().split()))
tIndex = {}
tIndexSum = {}
s = 0
for i in range(n):
    r = 0
    if s + t[i] > m:
        keys = list(tIndex.keys())
        keys.sort(reverse = True)
        s1 = s
        for k in keys:
            if s1 - tIndex[k] * k + t[i] > m:
                s1 -= tIndex[k] * k
                r += tIndex[k]
            else:
                r += (s1+t[i]-m)//k
                s1 -= ((s1+t[i]-m)//k) * k
                if s1 + t[i] > m:
                    s1 -= k
                    r += 1
            if s1 + t[i] <= m:
                break
    print(r, end = ' ')
            
    if not(t[i] in tIndex):
        tIndex[t[i]] = 0
    tIndex[t[i]] += 1
    if not(t[i] in tIndexSum):
        tIndexSum[t[i]] = 0
    tIndexSum[t[i]] += t[i]
    s += t[i]
##    print('s=',s)
##    print(tIndex)
            


#  author: ThePonyCoder
#  created: 19.06.2019, 17:45
#  filename: a.py
#  path: C:/Users/User/Desktop/python/Prog/CodeForces/rounds/cf_568/a.py

def ri():
    return [int(i) for i in input().split()]


def fl(a, b):
    return abs(a - b)


def gt_mx(d, l):
    ans = 0
    for i in reversed(range(1, 110)):
        if(l < 0):
            break
        if d[i] > 0:
            do = min(l // i, d[i])
            ans += do
            l -= do * i
            if l > 0 and do < d[i]:
                l -= i
                ans += 1
    
    return ans


def main(n, m, t):
    if m >= t[0]:
        print(0, end=' ')
    else:
        print(1, end=' ')
    d = {i: 0 for i in range(1, 110)}
    d[t[0]] += 1
    sm = t[0]
    for i in range(1, len(t)):
        sm += t[i]
        if i == 5:
            asd = 123
        if sm > m:
            delit = gt_mx(d, sm - m)
            print(delit, end=' ')
        else:
            print(0, end=' ')
        d[t[i]] += 1
        
        # print(ans, )
    # return ans


n, m = ri()
t = ri()
main(n, m, t)


n, m = map(int, input().strip().split())
a    = [int(x) for x in input().strip().split()]
t    = [0 for i in range(0, 101)]
for i in range(0, n):
    c, j, s = a[i], 1, 0
    flag = 0
    while(j < 101):
        cnt = min(t[j], (m-c)//j)
        s += cnt
        c += j*cnt
        if(cnt < t[j]):
            flag = 1
            break
        j+=1
    if(flag == 0):
        print(0, end=' ')
    else:
        print(i-s, end=' ')
    t[a[i]]+=1


#!/usr/bin/env python
n, M = list(map(int, input().split()))
t = list(map(int, input().split()))
f = [0 for _ in range(101)]
a = []
for i in range(n):
    Mi = M - t[i]
    c = 0
    for j in range(1, 101):
        k = min(Mi // j, f[j]) 
        Mi -= k * j
        c += k
    a.append(i - c)
    f[t[i]] += 1
print(' '.join(map(str, a)))



def main1():
    buf = input()
    buflist = buf.split()
    n = int(buflist[0])
    M = int(buflist[1])
    buf = input()
    buflist = buf.split()
    t = list(map(int, buflist))
    student = []
    minimum = []
    for i in range(n):
        count = 0
        pointer = 0
        while pointer < i:
            if count + student[pointer] <= M - t[i]:
                count += student[pointer]
                pointer += 1
            else:
                break
        minimum.append(i - pointer)
        student.append(t[i])
        student.sort()
    print(' '.join(list(map(str, minimum))))

def main2():
    buf = input()
    buflist = buf.split()
    n = int(buflist[0])
    M = int(buflist[1])
    buf = input()
    buflist = buf.split()
    t = list(map(int, buflist))
    bucket = []
    for i in range(101):
        bucket.append(0)
    minimum = []
    for i in range(n):
        target = M - t[i]
        count = 0
        subtotal = 0
        for j in range(1, 101):
            if subtotal + bucket[j] * j > target:
                count += (target - subtotal) // j
                break
            else:
                count += bucket[j]
                subtotal += bucket[j] * j
        minimum.append(i - count)
        bucket[t[i]] += 1
    print(' '.join(list(map(str, minimum))))

def __starting_point():
    #main1() # for C1
    main2() # for C2

__starting_point()

n, m = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
cnt = [0]*101
curr = 0
for i in range(n):
	res = 0
	val = (curr+arr[i])-m
	t = 100
	while t > 0 and val > 0:
		d = min(cnt[t], (val+t-1)//t)
		res += d
		val -= d*t
		t -= 1
	print(res, end=" ")
	curr += arr[i]
	cnt[arr[i]] += 1


import math
import heapq
import copy
from collections import OrderedDict
from collections import deque  



t=list(map(int,input().split()))
d=t[1]
k=list(map(int,input().split()))
m=[0]*101
ans=list()

time=0
for i in range(t[0]):
	time=time+k[i]
	if((d-time)>=0):
		ans.append(0)
		m[k[i]]=m[k[i]]+1
		
	else:
		count=0
		test=time
		for j in range(100,-1,-1):
			if(test<=d):
				break
			if(m[j]>0):
				cur=m[j]
				vv=cur*j
				if(abs(test-vv)<=d):
					if(vv<test or vv>test):
						x=math.ceil((test-d)/j)
						if(x<=cur):
							count=count+x
							test=test-(x*j)
						else:
							count=count+cur
							test=test-(cur*j)
					else:
						count=count
						test=test-vv

				else:
					count=count+m[j]
					test=test-vv
		ans.append(count)
		m[k[i]]=m[k[i]]+1
print(*ans)

#!/usr/bin/env python
n, M = list(map(int, input().split()))
t = list(map(int, input().split()))
f = [0 for _ in range(101)]
a = []
for i in range(n):
    Mi = M - t[i]
    c = 0
    for j in range(1, 101):
        k = min(Mi // j, f[j]) 
        Mi -= k * j
        c += k
    a.append(i - c)
    f[t[i]] += 1
print(' '.join(map(str, a)))


n, m = [int(i) for i in input().split()]
line = [int(x) for x in input().split()]

count = {i+1:0 for i in range(100)}
sum = 0

for i in line:
    p = sum + i
    t = 0
    if p > m:
        for j in range(100, 0, -1):
            if count[j] == 0:
                continue
            else:
                if p - count[j] * j > m:
                    p -= count[j] * j
                    t += count[j]
                else:
                    l = (p - m) // j
                    if p - l * j > m:
                        l += 1
                    t += l
                    break
    count[i] += 1
    sum += i
    print(t, end = " ")


n, m = list(map(int, input().split()))
t = list(map(int, input().split()))
freq = []
for i in range(101):
	freq.append(0)
for i in range(0, n):
	time=m-t[i]
	num=0
	for j in range(1, 101):
		p = int(time/j)
		if(p>=freq[j]):
			time-=freq[j]*j
			num+=freq[j]
		else:
			time-=p*j
			num+=p
	print(i-num, end=" ")
	freq[t[i]] += 1

import itertools
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline

n, m = list(map(int, input().split()))
t = list(map(int, input().split()))
tsum = list(itertools.accumulate(t))
ans = []
for i in range(n):
    if tsum[i] <= m:
        ans.append(0)
    else:
        heap = [-k for k in t[:i]]
        heapify(heap)
        remain = sum(t[:i]) - m
        count = 0
        for j in range(i, n):
            uselist = []
            remain += t[j]

            while remain > 0:
                count += 1
                use = heappop(heap)
                remain += use
                uselist.append(use)
            else:
                ans.append(count)
                uselist.sort(reverse = True)

            summ = 0
            for uses in uselist[:100]:
                heappush(heap, uses)
                remain -= uses
                summ -= uses
                count -= 1
                if summ > 100:
                    break
            heappush(heap, -t[j])

        break

print(" ".join(map(str, ans)))


import bisect
n,m=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
answer=[]
students=[0]*100
for i in range(n):
    time=m-a[i]
    counter=0
    for j in range(100):
        x=min(time//(j+1),students[j])
        counter+=x
        time-=(x*(j+1))
    answer.append(i-counter)
    students[a[i]-1]+=1
print(*answer)
        
    
    


n, m = map(int, input().split())
tot_sum, curr_sum, d = 0, 0, {}
arr = list(map(int, input().split()))
for x in arr:
    tot_sum += x
    curr_sum += x
    curr_res = 0
    for it in sorted(d.keys(), reverse=True):
        if curr_sum <= m:
            break
        take = min(d[it], (curr_sum - m + it - 1) // it)
        curr_sum -= take * it
        curr_res += take
    d[x] = 1 if x not in d else d[x] + 1
    curr_sum = tot_sum
    print(curr_res, end=' ')


arr = list(map(int, input().split()))
n, m = arr[0], arr[1]
s = str(input())
a = [int(i) for i in s.split()]
answers = []
s = 0
count = 0
max_val = 0
k = 0
arr = [0 for i in range(101)]
for elem in a:
    if s + elem > m:
        i = max_val
        s2 = s
        c2 = count
        while s2 + elem > m:
            if s2 - arr[i] * i + elem <= m:
                ans = k - c2 + (arr[i] - (m - elem - (s2 - arr[i] * i)) // i)
                s2 -= i * (arr[i] - (m - elem - (s2 - arr[i] * i)) // i)
            else:
                s2 -= arr[i] * i
                c2 -= arr[i]
                i -= 1
        if elem < max_val:
            arr[elem] += 1
            arr[max_val] -= 1
            s = s - max_val + elem
            j = max_val
            while arr[j] < 1:
                j -= 1
            max_val = j
    else:
        arr[elem] += 1
        s += elem
        ans = k - count
        count += 1
        if elem > max_val:
            max_val = elem
    answers.append(str(ans))
    k += 1

print(' '.join(answers))




n,m=list(map(int,input().split()))
if n==1:
	print(0)
	return
t=list(map(int,input().split()))
dp=[0]*101
ans=[]
for i in range(len(t)):
	s=0
	lim=m-t[i]
	f=False
	c=0
	for j in range(1,101):
		if f:
			c+=dp[j]
			continue
		if dp[j]:
			if s+dp[j]*j>lim:
				c+=dp[j]-(lim-s)//j
				f=True
			s+=dp[j]*j
	dp[t[i]]+=1
	ans.append(c)
print(*ans)


















from heapq import *
N, M = map(int, input().split())
A = [int(a) for a in input().split()]

h = []
s = 0
k = 0
ANS = []
for a in A:
    t = []
    while s + a > M:
        t.append(heappop(h))
        s += t[-1]
    ANS.append(len(t)+k)
    while t:
        s -= t[-1]
        heappush(h, t.pop())
    s += a
    heappush(h, -a)
    if s > M:
        s += heappop(h)
        k += 1
print(*ANS)

import math

nM=input().split(' ')
n = int(nM[0])
M = int(nM[1])

a = list(map(int,input().split(' ')))

carr = [0 for i in range(100)]

carr[a[0]-1]+=1
print('0',end=' ')

sum=a[0]
for i in range(1,len(a)):
    failed=0
    if sum+a[i] > M:
        A = sum+a[i]
        for itr in range(100,0,-1):
            if A - carr[itr-1]*itr > M:
                A-=carr[itr-1]*itr
                failed+=carr[itr-1]
                # print(A,itr,failed)
            else:
                failed+=math.ceil((A-M)/itr)
                # print('vade',failed)
                break
        print(failed,end=' ')
    else:
        print(failed, end=' ')
    
    sum+=a[i]
    carr[a[i]-1]+=1

import sys
def nline():
    return sys.stdin.readline()[:-1]
 
def ni():
    return int(sys.stdin.readline())
 
def na():
    return [int(v) for v in sys.stdin.readline().split()]

n,m=na()
arr=na()
sm=0
count=[0]*101
for i in range(0,n):
	diff,k=sm+arr[i]-m,0
	if(diff>0):
		for j in range(100,-1,-1):
			tmp=count[j]*j
			if(tmp>=diff):
				k+=(diff+j-1)//j
				break;
			k+=count[j]
			diff-=tmp
	print(k,end=" ")
	sm+=arr[i]
	count[arr[i]]+=1