C2. Exam in BerSU (hard version)(線段樹加二分)Codeforces Round #568 (Div. 2)

 

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn students. Students will take the exam one-by-one in order from 11-th to nn-th. Rules of the exam are following:

  • The ii-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 titi 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 MM minutes (maxti≤Mmaxti≤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 ii, you should count the minimum possible number of students who need to fail the exam so the ii-th student has enough time to pass the exam.

For each student ii, find the answer independently. That is, if when finding the answer for the student i1i1 some student jj should leave, then while finding the answer for i2i2 (i2>i1i2>i1) the student jj student does not have to go home.

Input

The first line of the input contains two integers nn and MM (1≤n≤2⋅1051≤n≤2⋅105, 1≤M≤2⋅1071≤M≤2⋅107) — the number of students and the total duration of the exam in minutes, respectively.

The second line of the input contains nn integers titi (1≤ti≤1001≤ti≤100) — time in minutes that ii-th student spends to answer to a ticket.

It's guaranteed that all values of titi are not greater than MM.

Output

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

Examples

input

Copy

7 15
1 2 3 4 5 6 7

output

Copy

0 0 0 0 0 2 3 

input

Copy

5 100
80 40 40 40 60

output

Copy

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=15M=15 (the sum is 1+2+3+4+5=151+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 00.

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

In order for the 77-th student to pass the exam, it is necessary that at least 33 students must fail it before (for example, the 22-nd, 55-th and 66-th, then the 77-th will finish its exam in 1+3+4+7=151+3+4+7=15 minutes, which does not exceed MM).

題意:n個人,m的時間內。每個人有一個ti代表回答時間。第i個人可以選擇消耗ti的時間回答的問題 或者 不消耗時間 留時間給後面的人做。

問第i個人能回答問題時   前面至少有多少人要選擇不回答問題?

做法:假如這個人前面的人都選擇回答問題 消耗完m的時間, 而他自己沒有時間的時候,就考慮從最大的人一一去掉。這裏可以採用二分得到去掉的個數。二分需要查區間和,和區間個數,又是動態的更新。是不是很像線段樹的做法。

記得要加上一個離散化,沒加離散化容易T。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
using namespace std;
typedef long long ll;
const int N=2e5+10; 
int a[N],b[N],id[N],ans[N];
int sum[4*N],num[4*N];
int n,m;
int getid(int x)
{
	return lower_bound(b+1,b+1+n,x)-b;
}

int qu(int id,int l,int r,int ql,int qr,int ty)
{
	if(ql<=l&&r<=qr)  {
		if(ty==1) return sum[id];
		else return num[id];
	}
	int mid=l+r>>1;
	int ans=0;
	if(ql<=mid) ans+=qu(id<<1,l,mid,ql,qr,ty);
	if(qr>mid) ans+=qu(id<<1|1,mid+1,r,ql,qr,ty);
	return ans;
}
void up(int id,int l,int r,int pos,int val)
{
	if(l==r)
	{
		num[id]++;
		sum[id]+=val;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid) up(id<<1,l,mid,pos,val);
	else up(id<<1|1,mid+1,r,pos,val);
	sum[id]=sum[id<<1]+sum[id<<1|1];
	num[id]=num[id<<1]+num[id<<1|1];
	
}
int main()
{
	cin>>n>>m;
	rep(i,1,n)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b+1,b+1+n);
	rep(i,1,n) id[i]=getid(a[i]);
	int ss=0;
	rep(i,1,n)
	{
		if(ss+a[i]>m)//若沒有時間了 
		{
			int l=1,r=n;
			int id1=-1;
			int tt=0;
			int t1=0,t2=0;
			while(l<=r)//
			{
				int mid=l+r>>1;
				int qunum=qu(1,1,n,mid,n,1);
				if(ss-qunum+a[i]<=m)//二分查找去掉的區間和 
				{
					l=mid+1;
					tt=qunum;
					id1=mid;
				}
				else r=mid-1;
			}
			t2=qu(1,1,n,id1,n,2);//這個區間內有多少個數 
			int j=b[id1];
			t1=ceil(1.0*((ss-tt+a[i])-m)/j);//t1<=0  第id1位置上可能有不止一個 
			ans[i]=t1+t2;
		}
		up(1,1,n,id[i],a[i]);
		ss+=a[i];
	}
	rep(i,1,n) printf("%d ",ans[i]);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章