C2. Exam in BerSU (hard version)

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 11-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 ??ti 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??≤?maxti≤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 ?1i1 some student ?j should leave, then while finding the answer for ?2i2 (?2>?1i2>i1) the student ?j student does not have to go home.

Input

The first line of the input contains two integers ?n and ?M (1≤?≤2⋅1051≤n≤2⋅105, 1≤?≤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 ?n integers ??ti (1≤??≤1001≤ti≤100) — time in minutes that ?i-th student spends to answer to a ticket.

It's guaranteed that all values of ??ti 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.

思路:虽然说 本题的数据范围m,n都很大,但是本身数值ti范围只有1~100,因此我们可以记录到第i个位置有(1~100)出现的次数,遍历ti。

#include 
#include 
#include 
#include 
#include
#define ll long long
using namespace std;
ll n,m;
ll cnt[200],ans[1008611];
ll num[1008611],sum=0;
int main(){
    cin>>n>>m;
    ll tmp,t;
    for(int i=0;i>num[i];
        sum+=num[i];
        tmp=sum;
        for(int j=100;j>=1;j--){
            if(tmp

 

你可能感兴趣的:(codeforces)