<p>There are <code>n</code> different online courses numbered from <code>1</code> to <code>n</code>. You are given an array <code>courses</code> where <code>courses[i] = [duration<sub>i</sub>, lastDay<sub>i</sub>]</code> indicate that the <code>i<sup>th</sup></code> course should be taken <b>continuously</b> for <code>duration<sub>i</sub></code> days and must be finished before or on <code>lastDay<sub>i</sub></code>.</p>

<p>You will start on the <code>1<sup>st</sup></code> day and you cannot take two or more courses simultaneously.</p>

<p>Return <em>the maximum number of courses that you can take</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
<strong>Output:</strong> 3
Explanation: 
There are totally 4 courses, but you can take 3 courses at most:
First, take the 1<sup>st</sup> course, it costs 100 days so you will finish it on the 100<sup>th</sup> day, and ready to take the next course on the 101<sup>st</sup> day.
Second, take the 3<sup>rd</sup> course, it costs 1000 days so you will finish it on the 1100<sup>th</sup> day, and ready to take the next course on the 1101<sup>st</sup> day. 
Third, take the 2<sup>nd</sup> course, it costs 200 days so you will finish it on the 1300<sup>th</sup> day. 
The 4<sup>th</sup> course cannot be taken now, since you will finish it on the 3300<sup>th</sup> day, which exceeds the closed date.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> courses = [[1,2]]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> courses = [[3,2],[4,3]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= courses.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= duration<sub>i</sub>, lastDay<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
</ul>

#define pb push_back
#define ll long long
using namespace std;
const int N = 5e5 + 5;
const ll inf = 1e15;
vector<pair<pair<int, int>, pair<int, int>>> mon;
vector<int> idx;
int n, tab[N], k;
ll d[4*N], push[4*N];
bitset<N> odw;
void add(int x,  ll ile){
	d[x]+=ile;
	push[x]+=ile;
}
void pushuj(int x){
	add(x*2, push[x]);
	add(x*2+1, push[x]);
	push[x]=0;
}
void build(int x, int l, int r){
	if(l==r){
		if(l<=n) d[x]=tab[l];
		if(!d[x]) d[x]=inf;
		return;
	}
	int sr = (l+r)/2;
	build(x*2, l, sr);
	build(x*2+1, sr+1, r);
	d[x]=min(d[x*2], d[x*2+1]);
}
void update(int x, int l, int r, int a, int b, ll val){
	if(a<=l&&r<=b){
		add(x, val);
		return;
	}
	pushuj(x);
	int sr=(l+r)/2;
	if(a<=sr){
		update(x*2, l, sr, a, b, val);
	}
	if(sr<b){
		update(x*2+1, sr+1, r, a, b, val);
	}
	d[x]=min(d[x*2], d[x*2+1]);
}
ll query(int x, int l, int r, int a, int b){
	if(a<=l&&r<=b){
		return d[x];
	}
	pushuj(x);
	ll res=1e15;
	int sr=(l+r)/2;
	if(a<=sr){
		res=min(res, query(x*2, l, sr, a, b));
	}
	if(sr<b){
		res=min(res, query(x*2+1, sr+1, r, a, b));
	}
	d[x]=min(d[x*2], d[x*2+1]);
	return res;
}
bool cmp(pair<pair<int, int>, pair<int, int>>a, pair<pair<int, int>, pair<int, int>>b){
	if(a.first.second==b.first.second){
		return a.first.first < b.first.first;
	}
	return a.first.second < b.first.second;
}
class Solution {
public:
    int scheduleCourse(vector<vector<int>>& courses) {
    n=courses.size();
    k=0;
    mon.clear();
    idx.clear();
    odw.reset();
	int rozm=1;
    for(int i=0;i<N;i++) tab[i]=0;
    for(int i=0;i<N*4;i++) d[i]=push[i]=0;
	for(int i=1;i<=n;i++){
		int a=courses[i-1][0], b=courses[i-1][1];
		if(a<=b){
			mon.pb({{a, b}, {0, i}});
		}
	}
	sort(mon.begin(), mon.end(), cmp);
	for(int i=0;i<(int)mon.size();i++){
		if(i&&mon[i].first.second==mon[i-1].first.second) k++;
		mon[i]={{mon[i].first.first, mon[i].first.second}, {i+1-k, mon[i].second.second}};
	}
	mon.pb({{0, 0}, {0, 0}});
	sort(mon.begin(), mon.end());
	n=mon.size()-1;
	for(int i=1;i<=n;i++){
		tab[mon[i].second.first]=mon[i].first.second;
	}
	while(rozm<n+1){
		rozm*=2;
	}
	build(1, 1, rozm);
	for(int i=1;i<=n;i++){
		update(1, 1, rozm, mon[i].second.first, rozm, -mon[i].first.first);
		if(d[1]<0){
			update(1, 1, rozm, mon[i].second.first, rozm, mon[i].first.first);
			continue;
		}
		idx.pb(mon[i].second.second);
		odw[mon[i].second.second] = true;
	}
	sort(mon.begin(), mon.end(), cmp);
	return(int)idx.size();
    }
};
86908