diff --git a/hard/day_5/solution.cpp b/hard/day_5/solution.cpp index d593d78..fe27722 100644 --- a/hard/day_5/solution.cpp +++ b/hard/day_5/solution.cpp @@ -1,49 +1,55 @@ -//Write your code here -// LUOGU_RID: 147799314 -#include +#include // Include necessary header files using namespace std; -typedef long long ll; +typedef long long ll; // Define a shorthand for long long -const int MAXN = 5e5 + 5; +const int MAXN = 5e5 + 5; // Define maximum array size -ll a[MAXN], dif[MAXN]; -map mp; -int l_[MAXN], r_[MAXN]; ll x_[MAXN]; +ll initialArray[MAXN], differences[MAXN]; // Declare arrays for storing initial values and differences +map valueChanges; // Declare a map to store the changes in values +int queryLeft[MAXN], queryRight[MAXN]; ll queryValue[MAXN]; // Arrays to store left, right, and value changes for queries -int main() { ios::sync_with_stdio(0); cin.tie(0); - int t; cin >> t; while (t--) { - int n; cin >> n; - for (int i = 1; i <= n; i++) - cin >> a[i]; - int ans = 0; - int q; cin >> q; for (int i = 1; i <= q; i++) { - int l, r; ll x; cin >> l >> r >> x; l_[i] = l; r_[i] = r, x_[i] = x; - mp[l] += x; - if (r+1 <= n) - mp[r+1] -= x; - while (!mp.empty() && mp.begin()->second == 0) - mp.erase(mp.begin()); - if (!mp.empty() && mp.begin()->second < 0) { - ans = i; - mp.clear(); +int main() { + ios::sync_with_stdio(0); cin.tie(0); // Fast I/O + + int t; cin >> t; // Input number of test cases + while (t--) { // Iterate over each test case + int n; cin >> n; // Input array size + for (int i = 1; i <= n; i++) // Input the initial array + cin >> initialArray[i]; + int ans = 0; // Initialize the answer variable + + int q; cin >> q; // Input number of queries + for (int i = 1; i <= q; i++) { // Loop over each query + int left, right; ll value; cin >> left >> right >> value; // Input query parameters + queryLeft[i] = left; queryRight[i] = right, queryValue[i] = value; // Store query parameters + valueChanges[left] += value; // Update the map with the value change + if (right + 1 <= n) + valueChanges[right + 1] -= value; // Adjust for the end of the range + while (!valueChanges.empty() && valueChanges.begin()->second == 0) // Remove zero values from the map + valueChanges.erase(valueChanges.begin()); + if (!valueChanges.empty() && valueChanges.begin()->second < 0) { // If there's a negative value in the map + ans = i; // Update the answer with the current query index + valueChanges.clear(); // Clear the map } } - for (int i = 1; i <= ans; i++) { - int l = l_[i], r = r_[i]; ll x = x_[i]; - dif[l] += x; - if (r+1 <= n) - dif[r+1] -= x; + + for (int i = 1; i <= ans; i++) { // Process queries up to the answer + int left = queryLeft[i], right = queryRight[i]; ll value = queryValue[i]; // Retrieve query parameters + differences[left] += value; // Update difference array at start index + if (right + 1 <= n) + differences[right + 1] -= value; // Adjust difference array at end index } - for (int i = 1; i <= n; i++) { - dif[i] += dif[i-1]; - a[i] += dif[i]; - cout << a[i] << " \n"[i == n]; + + for (int i = 1; i <= n; i++) { // Apply differences to the array + differences[i] += differences[i - 1]; // Accumulate differences + initialArray[i] += differences[i]; // Update array value + cout << initialArray[i] << " \n"[i == n]; // Output array value } - // init + // Initialize arrays and map for the next test case for (int i = 1; i <= n; i++) - dif[i] = 0; - mp.clear(); + differences[i] = 0; + valueChanges.clear(); } - return 0; + return 0; // End of program } \ No newline at end of file