Skip to content

Commit

Permalink
chore: added comments and changed variable name in hard day 5 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutosh-3474 committed Apr 13, 2024
1 parent ce470a5 commit b85eb7d
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions hard/day_5/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
//Write your code here
// LUOGU_RID: 147799314
#include <bits/stdc++.h>
#include <bits/stdc++.h> // 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<int, ll> mp;
int l_[MAXN], r_[MAXN]; ll x_[MAXN];
ll initialArray[MAXN], differences[MAXN]; // Declare arrays for storing initial values and differences
map<int, ll> 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
}

0 comments on commit b85eb7d

Please # to comment.