Untitled

Anonymous
c_cpp
02/21/2026 7:18 PM
796 B
27
Indexable
#include <bits/stdc++.h>
using namespace std;

// Time Complexity: O(n log n)
// Memory Complexity: O(n)

using ll = long long;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);

	int tc; cin >> tc;
	while(tc--) {
		int n, d; cin >> n >> d;
		vector<int> a(n), b(n);
		for(auto &x : a) cin >> x; 
		for(auto &x : b) cin >> x;

		priority_queue<int, vector<int>, greater<int>> pq;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < a[i]; j++) pq.push(i);
			for(int j = 0; j < b[i]; j++) pq.pop();
			while(pq.top() <= i-d) pq.pop();
		}

		cout << pq.size() << "\n";
	}

	return 0;
}
Editor is loading...
Leave a Comment