Source Code
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	fast
	ll n,m;
	cin>>n>>m;
	vector<ll> a(n),b(m);

	for(ll i=0;i<n;i++)cin>>a[i];
	for(ll i=0;i<m;i++)cin>>b[i];

	sort(all(a));
	// reverse(all(a));

	sort(all(b));
	reverse(all(b));

	ll q;
	cin>>q;
	while(q--){
		ll k;	
		cin>>k;

		int lo = 0,hi = n-1,to =0;
		while(lo <= hi){
			int md = (lo + hi )/2;

			vector<ll> event(n+1,0);

			ll add = 0;
			ll ans = 0,at = 0;

			for(ll i=md;i<n;i++){
				add += event[i];
				ll cur = add + a[i];

				ll need = k - cur;
				if(need <= 0){
					ans++;
					continue;
				}
				for(ll j=0;j<need && at < m;j++){
					event[min(n , i + b[at])]--;
					add++;
					at++;
				}
				if(add + a[i] >= k)ans++;
			}

			if(ans ==  n - md){
				hi = md-1;
				to = n - md;
			}else lo = md+1;
		}

		cout << to << ' ';
	}
}

/*
	1- Look at base case (n=1,m=1)
	2- Overflow 
	3- Use assert
*/
Copy
Votes Warawreh
GNU G++17
4 ms
720 KB
Wrong Answer