Submission #1310942

#TimeUsernameProblemLanguageResultExecution timeMemory
1310942thuhienneAnts and Sugar (JOI22_sugar)C++20
6 / 100
52 ms784 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define re exit(0);

int q,l;

int sz1,sz2;
pair <int,int> ant[3009],sugar[3009];

pair <int,int> temp1[3009],temp2[3009];

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

	cin >> q >> l;
	while (q--) {
		int type,pos,quan;cin >> type >> pos >> quan;
		pair <int,int> A = {pos,quan};
		
		if (type == 1) {
			ant[++sz1] = A;
			int temp = sz1;
			while (temp > 1 && ant[temp - 1].first > ant[temp].first) swap(ant[temp],ant[temp - 1]),temp--;
		} else {
			sugar[++sz2] = A;
			int temp = sz2;
			while (temp > 1 && sugar[temp - 1].first > sugar[temp].first) swap(sugar[temp],sugar[temp - 1]),temp--;
		}
		
		for (int i = 1;i <= sz1;i++) temp1[i] = ant[i];
		for (int i = 1;i <= sz2;i++) temp2[i] = sugar[i];
		
		int pt = 1;
		ll res = 0;
		for (int i = 1;i <= sz1;i++) {
			while (pt <= sz2 && (temp2[pt].first < temp1[i].first - l || temp2[pt].second == 0)) pt++;
			
			if (pt == sz2 + 1) break;
			if (temp2[pt].first > temp1[i].first + l) continue;
			
			int tmp = min(temp2[pt].second,temp1[i].second);
			res += tmp;
			
			temp2[pt].second -= tmp;
			temp1[i].second -= tmp;
			
			if (temp1[i].second) i--;
			
		}
		
		cout << res << '\n';
	}

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...