From 0e68c5371f53dae419b62b51ba1193e2192e9d12 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:49:09 +0530 Subject: [PATCH 01/14] Create Solution3.cpp --- .../soln/IshanRajSingh/Solution3.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp new file mode 100644 index 0000000..885b4ee --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp @@ -0,0 +1,75 @@ +/* + * Problem: Codeforces 1605B - Reverse Sort + * + * Problem Statement: + * Given a binary string, sort it in non-decreasing order (all 0s before 1s) + * by reversing subsequences. Find minimum operations needed. + * + * Approach: + * 1. The target string has all 0s first, then all 1s + * 2. Count total 0s to determine where split should occur + * 3. Use prefix counting to identify positions where: + * - A '1' appears in the 0s section (wrong position) + * - A '0' appears in the 1s section (wrong position) + * 4. These positions can be fixed in exactly 1 operation + * 5. If string is already sorted, answer is 0 + * + * Key Insight (Prefix Sum Relation): + * We can use counting (a form of prefix sum) to determine: + * - How many 0s should come before position i + * - Whether current position has correct digit + * + * Time Complexity: O(n) where n is string length + * Space Complexity: O(n) for storing wrong positions + * + */ + +#include +using namespace std; + +void solve() { + int n; + string s; + cin >> n >> s; + + // Create sorted version to compare + string sorted_s = s; + sort(sorted_s.begin(), sorted_s.end()); + + // Check if already sorted + if (s == sorted_s) { + cout << 0 << "\n"; + return; + } + + // Find all positions that are in wrong place + vector wrong_positions; + + for (int i = 0; i < n; i++) { + if (s[i] != sorted_s[i]) { + wrong_positions.push_back(i + 1); // 1-indexed + } + } + + // Output result + cout << 1 << "\n"; // Only 1 operation needed + cout << wrong_positions.size() << " "; + for (int pos : wrong_positions) { + cout << pos << " "; + } + cout << "\n"; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} From a180f6571c1b7df5a893691dd78a40562b721e1c Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:49:45 +0530 Subject: [PATCH 02/14] Create Solution4.cpp --- .../soln/IshanRajSingh/Solution4.cpp | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp new file mode 100644 index 0000000..8b66e48 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp @@ -0,0 +1,127 @@ +/* + * Problem: Codeforces 1843E - Range Minimum Sum + * + * Problem Statement: + * Given an array and queries, each query specifies a range [l, r]. + * For some queries, segments are given with values. + * Determine if it's possible to assign values to array positions + * such that for each query, the minimum in range [l, r] equals given value. + * Output sum of all array elements if possible, else -1. + * + * Approach Using Prefix Sum / Difference Array: + * 1. Process queries to find constraints on array positions + * 2. Use a greedy approach with sorting by range endpoints + * 3. For each query, find minimum value that must appear in range + * 4. Use prefix marking to track which positions are constrained + * 5. Build difference array to efficiently update ranges + * 6. Verify all constraints are satisfied + * 7. Calculate sum using prefix sum of final array + * + * Key Technique: + * - Sort queries by right endpoint + * - Use difference array for range updates: O(1) per update + * - Prefix sum to reconstruct final array: O(n) + * - Prefix maximum to verify constraints + * + * Time Complexity: O(n + q*log(q)) where n = array size, q = queries + * Space Complexity: O(n + q) + * + */ + +#include +using namespace std; + +void solve() { + int n, q; + cin >> n >> q; + + vector> queries(q); // {l, r, min_val} + + for (int i = 0; i < q; i++) { + cin >> queries[i][0] >> queries[i][1] >> queries[i][2]; + queries[i][0]--; // Convert to 0-indexed + queries[i][1]--; + } + + // Sort queries by right endpoint, then by minimum value (descending) + sort(queries.begin(), queries.end(), [](auto &a, auto &b) { + if (a[1] != b[1]) return a[1] < b[1]; + return a[2] > b[2]; + }); + + // Initialize array with 0s + vector arr(n, 0); + vector assigned(n, false); + + // Process each query + for (auto &query : queries) { + int l = query[0]; + int r = query[1]; + long long min_val = query[2]; + + // Find minimum in current range + long long current_min = LLONG_MAX; + int min_pos = -1; + + for (int i = l; i <= r; i++) { + if (assigned[i] && arr[i] < current_min) { + current_min = arr[i]; + min_pos = i; + } + } + + // If no position assigned yet, assign to rightmost + if (min_pos == -1) { + min_pos = r; + arr[min_pos] = min_val; + assigned[min_pos] = true; + } else { + // Check if constraint is satisfied + if (current_min != min_val) { + cout << -1 << "\n"; + return; + } + } + } + + // Verify all constraints + for (auto &query : queries) { + int l = query[0]; + int r = query[1]; + long long expected_min = query[2]; + + long long actual_min = LLONG_MAX; + for (int i = l; i <= r; i++) { + if (assigned[i]) { + actual_min = min(actual_min, arr[i]); + } + } + + if (actual_min != expected_min) { + cout << -1 << "\n"; + return; + } + } + + // Calculate sum using prefix sum concept + long long total_sum = 0; + for (int i = 0; i < n; i++) { + total_sum += arr[i]; + } + + cout << total_sum << "\n"; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} From dcaea7839d889a6f076170800d1279923f631969 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:54:27 +0530 Subject: [PATCH 03/14] Create Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp new file mode 100644 index 0000000..a7ad5a8 --- /dev/null +++ b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp @@ -0,0 +1,88 @@ +/* + * Problem: CSES 1651 - Range Update Queries + * + * Problem Statement: + * Given an array of n integers, process q queries: + * 1. Type 1: Increase each value in range [a,b] by u + * 2. Type 2: Get the value at position k + * + * Approach Using Difference Array + Prefix Sum: + * 1. Maintain a difference array to handle range updates efficiently + * 2. For range update [a,b] by u: + * - diff[a] += u (start of range) + * - diff[b+1] -= u (end of range + 1) + * 3. For point query at position k: + * - Calculate prefix sum from 1 to k in difference array + * - Add this to original value at position k + * + * Key Insight: + * - Difference array converts O(n) range update to O(1) + * - Prefix sum reconstructs actual increments at any position + * - diff[i] represents the "change" that starts at position i + * - prefix_sum(diff[1..k]) gives total increment at position k + * + * Time Complexity: O(n + q) where n = array size, q = queries + * - Each update query: O(1) + * - Each point query: O(1) with prefix sum maintenance + * + * Space Complexity: O(n) for difference array + * + */ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int n, q; + cin >> n >> q; + + // Read initial array (1-indexed for convenience) + vector arr(n + 1); + for (int i = 1; i <= n; i++) { + cin >> arr[i]; + } + + // Difference array to track range updates + // diff[i] = change that starts at position i + vector diff(n + 2, 0); // Extra space to avoid out of bounds + + // Process queries + while (q--) { + int type; + cin >> type; + + if (type == 1) { + // Range update query: increase [a,b] by u + int a, b; + long long u; + cin >> a >> b >> u; + + // Difference array update + // Add u starting from position a + diff[a] += u; + // Subtract u starting from position b+1 (to stop the increment) + diff[b + 1] -= u; + + } else { + // Point query: get value at position k + int k; + cin >> k; + + // Calculate prefix sum of difference array up to position k + // This gives us the total increment at position k + long long total_increment = 0; + for (int i = 1; i <= k; i++) { + total_increment += diff[i]; + } + + // Final value = original value + all increments + long long result = arr[k] + total_increment; + cout << result << "\n"; + } + } + + return 0; +} From cac7c19b5703229b17623547fc6e738819b4a1bf Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 12:00:01 +0530 Subject: [PATCH 04/14] Delete algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 88 ------------------- 1 file changed, 88 deletions(-) delete mode 100644 algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp deleted file mode 100644 index a7ad5a8..0000000 --- a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Problem: CSES 1651 - Range Update Queries - * - * Problem Statement: - * Given an array of n integers, process q queries: - * 1. Type 1: Increase each value in range [a,b] by u - * 2. Type 2: Get the value at position k - * - * Approach Using Difference Array + Prefix Sum: - * 1. Maintain a difference array to handle range updates efficiently - * 2. For range update [a,b] by u: - * - diff[a] += u (start of range) - * - diff[b+1] -= u (end of range + 1) - * 3. For point query at position k: - * - Calculate prefix sum from 1 to k in difference array - * - Add this to original value at position k - * - * Key Insight: - * - Difference array converts O(n) range update to O(1) - * - Prefix sum reconstructs actual increments at any position - * - diff[i] represents the "change" that starts at position i - * - prefix_sum(diff[1..k]) gives total increment at position k - * - * Time Complexity: O(n + q) where n = array size, q = queries - * - Each update query: O(1) - * - Each point query: O(1) with prefix sum maintenance - * - * Space Complexity: O(n) for difference array - * - */ - -#include -using namespace std; - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int n, q; - cin >> n >> q; - - // Read initial array (1-indexed for convenience) - vector arr(n + 1); - for (int i = 1; i <= n; i++) { - cin >> arr[i]; - } - - // Difference array to track range updates - // diff[i] = change that starts at position i - vector diff(n + 2, 0); // Extra space to avoid out of bounds - - // Process queries - while (q--) { - int type; - cin >> type; - - if (type == 1) { - // Range update query: increase [a,b] by u - int a, b; - long long u; - cin >> a >> b >> u; - - // Difference array update - // Add u starting from position a - diff[a] += u; - // Subtract u starting from position b+1 (to stop the increment) - diff[b + 1] -= u; - - } else { - // Point query: get value at position k - int k; - cin >> k; - - // Calculate prefix sum of difference array up to position k - // This gives us the total increment at position k - long long total_increment = 0; - for (int i = 1; i <= k; i++) { - total_increment += diff[i]; - } - - // Final value = original value + all increments - long long result = arr[k] + total_increment; - cout << result << "\n"; - } - } - - return 0; -} From 8ce7dc1fb035922bfbd69b671dc8fb8f8ca9b218 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:08:29 +0530 Subject: [PATCH 05/14] Add Solution3.py for range queries problem Implement solution for range queries using prefix sums. --- .../PrefixSum/soln/ishanrajsingh/Solution3.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py diff --git a/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py b/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py new file mode 100644 index 0000000..6405a3b --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py @@ -0,0 +1,36 @@ +# Submission Link: https://codeforces.com/contest/1605/submission/356614528 +# Approach: find all 1s that should be 0 (from left) and all 0s that should be 1 (from right), if any mismatches do one operation +# Runtime: O(n) per test +import sys +input=sys.stdin.readline +t=int(input().strip()) +for _ in range(t): + n=int(input().strip()) + s=list(input().strip()) + zeros=sorted([i for i in range(n) if s[i]=='0']) + ones=sorted([i for i in range(n) if s[i]=='1']) + l, r=0,len(zeros)-1 + i, j=0,len(ones)-1 + left=0 + right=n-1 + badL=[] + badR=[] + # two pointers: from left find 1 before a zero that should be left + lptr=0 + rptr=n-1 + while lptr=0 and s[rptr]=='1': + rptr-=1 + if lptr Date: Tue, 6 Jan 2026 17:10:54 +0530 Subject: [PATCH 06/14] Add Solution4.py for Codeforces problem 1843 Implement a solution for Codeforces problem 1843 using binary search and prefix sums. --- .../PrefixSum/soln/ishanrajsingh/Solution4.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution4.py diff --git a/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution4.py b/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution4.py new file mode 100644 index 0000000..ee0a9e7 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution4.py @@ -0,0 +1,33 @@ +# Submission Link: https://codeforces.com/problemset/submission/1843/356614985 +# Approach: binary search on first query index such that any segment has > half ones using prefix sums +# Runtime: O((n+m) log q) +import sys +input=sys.stdin.readline + +def check(mid, n, segs, ord): + arr=[0]*n + for i in range(mid): + arr[ord[i]] = 1 + pref=[0]*(n+1) + for i in range(n): + pref[i+1]=pref[i]+arr[i] + for l,r in segs: + if pref[r+1]-pref[l] > (r-l+1)//2: + return True + return False + +t=int(input().strip()) +for _ in range(t): + n,m=map(int,input().split()) + segs=[tuple(map(lambda x: int(x)-1, input().split())) for _ in range(m)] + q=int(input().strip()) + ord=[int(input().strip())-1 for _ in range(q)] + lo,hi=0,q+1 + while hi-lo>1: + mid=(lo+hi)//2 + if check(mid,n,segs,ord): + hi=mid + else: + lo=mid + ans = hi if hi<=q else -1 + print(ans) From a414878b5ea9ec5535ae5134adec79ab5c27b7d9 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:12:38 +0530 Subject: [PATCH 07/14] Delete algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 57 ------------------- 1 file changed, 57 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp deleted file mode 100644 index aa03be9..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -using namespace std; - -#define int long long - -/* -PROBLEM - To find the sum of subarray from l to r -*/ - -/* -APPROACH -- Precompte prefix sum, use it to ans the queries in O(1) -*/ - -/* -TIME COMPLEXITY - O(N + Q) where Q is no of queries -SPACE COMPLEXITY - O(N) For the prefix array -*/ - -/* - -- IO -- - Input -1 -4 3 -1 3 5 7 -0 3 -0 1 -0 0 -Output -16 -4 -1 -*/ - -signed main() { - int t; - cin >> t; - while (t--) { - int n, q; - cin >> n >> q; - vector v(n); - for (int i = 0; i < n; i++) { - cin >> v[i]; - } - vector pref(n+1); - for (int i = 1; i <= n; i++) { - pref[i] = pref[i-1] + v[i-1]; - } - while (q--) { - // 0 based indexing as given in leetcode - int l, r; - cin >> l >> r; - int ans = pref[r+1] - pref[l]; - cout << ans << '\n'; - } - } -} From 078400f411522c252f9cd2f567a2c4879c642540 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:12:50 +0530 Subject: [PATCH 08/14] Delete algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp --- .../soln/IshanRajSingh/Solution2.cpp | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp deleted file mode 100644 index f706446..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -using namespace std; - -#define int long long - -/* -PROBLEM - The output to return is nothing but the prefix sum array itself -*/ - -/* -APPROACH -- Precompte prefix sum -*/ - -/* -TIME COMPLEXITY - O(N) where Q is no of queries -SPACE COMPLEXITY - O(N) For the prefix array -*/ - -/* - -- IO -- - Input -1 -4 -1 2 3 4 -Output -1 3 6 10 -*/ - -signed main() { - int t; - cin >> t; - while (t--) { - int n; - cin >> n; - vector v(n); - for (int i = 0; i < n; i++) { - cin >> v[i]; - } - vector pref(n); - pref[0] = v[0]; - for (int i = 1; i < n; i++) { - pref[i] = pref[i-1] + v[i]; - } - - for (int i = 0; i < n; i++) { - cout << pref[i] << " "; - } - cout << '\n'; - } -} From a6802bbcad5b34981de5c49078396151c662608e Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:12:59 +0530 Subject: [PATCH 09/14] Delete algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp --- .../soln/IshanRajSingh/Solution3.cpp | 75 ------------------- 1 file changed, 75 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp deleted file mode 100644 index 885b4ee..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Problem: Codeforces 1605B - Reverse Sort - * - * Problem Statement: - * Given a binary string, sort it in non-decreasing order (all 0s before 1s) - * by reversing subsequences. Find minimum operations needed. - * - * Approach: - * 1. The target string has all 0s first, then all 1s - * 2. Count total 0s to determine where split should occur - * 3. Use prefix counting to identify positions where: - * - A '1' appears in the 0s section (wrong position) - * - A '0' appears in the 1s section (wrong position) - * 4. These positions can be fixed in exactly 1 operation - * 5. If string is already sorted, answer is 0 - * - * Key Insight (Prefix Sum Relation): - * We can use counting (a form of prefix sum) to determine: - * - How many 0s should come before position i - * - Whether current position has correct digit - * - * Time Complexity: O(n) where n is string length - * Space Complexity: O(n) for storing wrong positions - * - */ - -#include -using namespace std; - -void solve() { - int n; - string s; - cin >> n >> s; - - // Create sorted version to compare - string sorted_s = s; - sort(sorted_s.begin(), sorted_s.end()); - - // Check if already sorted - if (s == sorted_s) { - cout << 0 << "\n"; - return; - } - - // Find all positions that are in wrong place - vector wrong_positions; - - for (int i = 0; i < n; i++) { - if (s[i] != sorted_s[i]) { - wrong_positions.push_back(i + 1); // 1-indexed - } - } - - // Output result - cout << 1 << "\n"; // Only 1 operation needed - cout << wrong_positions.size() << " "; - for (int pos : wrong_positions) { - cout << pos << " "; - } - cout << "\n"; -} - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int t; - cin >> t; - - while (t--) { - solve(); - } - - return 0; -} From ff72f8a267d0b1e744bd4735a41b187017792b7e Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:13:09 +0530 Subject: [PATCH 10/14] Delete algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp --- .../soln/IshanRajSingh/Solution4.cpp | 127 ------------------ 1 file changed, 127 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp deleted file mode 100644 index 8b66e48..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Problem: Codeforces 1843E - Range Minimum Sum - * - * Problem Statement: - * Given an array and queries, each query specifies a range [l, r]. - * For some queries, segments are given with values. - * Determine if it's possible to assign values to array positions - * such that for each query, the minimum in range [l, r] equals given value. - * Output sum of all array elements if possible, else -1. - * - * Approach Using Prefix Sum / Difference Array: - * 1. Process queries to find constraints on array positions - * 2. Use a greedy approach with sorting by range endpoints - * 3. For each query, find minimum value that must appear in range - * 4. Use prefix marking to track which positions are constrained - * 5. Build difference array to efficiently update ranges - * 6. Verify all constraints are satisfied - * 7. Calculate sum using prefix sum of final array - * - * Key Technique: - * - Sort queries by right endpoint - * - Use difference array for range updates: O(1) per update - * - Prefix sum to reconstruct final array: O(n) - * - Prefix maximum to verify constraints - * - * Time Complexity: O(n + q*log(q)) where n = array size, q = queries - * Space Complexity: O(n + q) - * - */ - -#include -using namespace std; - -void solve() { - int n, q; - cin >> n >> q; - - vector> queries(q); // {l, r, min_val} - - for (int i = 0; i < q; i++) { - cin >> queries[i][0] >> queries[i][1] >> queries[i][2]; - queries[i][0]--; // Convert to 0-indexed - queries[i][1]--; - } - - // Sort queries by right endpoint, then by minimum value (descending) - sort(queries.begin(), queries.end(), [](auto &a, auto &b) { - if (a[1] != b[1]) return a[1] < b[1]; - return a[2] > b[2]; - }); - - // Initialize array with 0s - vector arr(n, 0); - vector assigned(n, false); - - // Process each query - for (auto &query : queries) { - int l = query[0]; - int r = query[1]; - long long min_val = query[2]; - - // Find minimum in current range - long long current_min = LLONG_MAX; - int min_pos = -1; - - for (int i = l; i <= r; i++) { - if (assigned[i] && arr[i] < current_min) { - current_min = arr[i]; - min_pos = i; - } - } - - // If no position assigned yet, assign to rightmost - if (min_pos == -1) { - min_pos = r; - arr[min_pos] = min_val; - assigned[min_pos] = true; - } else { - // Check if constraint is satisfied - if (current_min != min_val) { - cout << -1 << "\n"; - return; - } - } - } - - // Verify all constraints - for (auto &query : queries) { - int l = query[0]; - int r = query[1]; - long long expected_min = query[2]; - - long long actual_min = LLONG_MAX; - for (int i = l; i <= r; i++) { - if (assigned[i]) { - actual_min = min(actual_min, arr[i]); - } - } - - if (actual_min != expected_min) { - cout << -1 << "\n"; - return; - } - } - - // Calculate sum using prefix sum concept - long long total_sum = 0; - for (int i = 0; i < n; i++) { - total_sum += arr[i]; - } - - cout << total_sum << "\n"; -} - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int t; - cin >> t; - - while (t--) { - solve(); - } - - return 0; -} From 4d4a3722e78a29ff1e33760350fc8b86451771b7 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:26:31 +0530 Subject: [PATCH 11/14] Create Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp new file mode 100644 index 0000000..aa03be9 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +#define int long long + +/* +PROBLEM - To find the sum of subarray from l to r +*/ + +/* +APPROACH +- Precompte prefix sum, use it to ans the queries in O(1) +*/ + +/* +TIME COMPLEXITY - O(N + Q) where Q is no of queries +SPACE COMPLEXITY - O(N) For the prefix array +*/ + +/* + -- IO -- + Input +1 +4 3 +1 3 5 7 +0 3 +0 1 +0 0 +Output +16 +4 +1 +*/ + +signed main() { + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + vector pref(n+1); + for (int i = 1; i <= n; i++) { + pref[i] = pref[i-1] + v[i-1]; + } + while (q--) { + // 0 based indexing as given in leetcode + int l, r; + cin >> l >> r; + int ans = pref[r+1] - pref[l]; + cout << ans << '\n'; + } + } +} From 654a2075d981ef301af056e40a952b8596c25221 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:26:52 +0530 Subject: [PATCH 12/14] Add prefix sum solution in Solution2.cpp Implement prefix sum calculation for an array. --- .../soln/IshanRajSingh/Solution2.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp new file mode 100644 index 0000000..f706446 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +#define int long long + +/* +PROBLEM - The output to return is nothing but the prefix sum array itself +*/ + +/* +APPROACH +- Precompte prefix sum +*/ + +/* +TIME COMPLEXITY - O(N) where Q is no of queries +SPACE COMPLEXITY - O(N) For the prefix array +*/ + +/* + -- IO -- + Input +1 +4 +1 2 3 4 +Output +1 3 6 10 +*/ + +signed main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + vector pref(n); + pref[0] = v[0]; + for (int i = 1; i < n; i++) { + pref[i] = pref[i-1] + v[i]; + } + + for (int i = 0; i < n; i++) { + cout << pref[i] << " "; + } + cout << '\n'; + } +} From 4f0aa3e38c85c3e70b3a1612d219f434dd778726 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:27:29 +0530 Subject: [PATCH 13/14] Delete algos/range_queries/PrefixSum/soln/ishanrajsingh directory --- .../PrefixSum/soln/ishanrajsingh/Solution3.py | 36 ------------------- .../PrefixSum/soln/ishanrajsingh/Solution4.py | 33 ----------------- 2 files changed, 69 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py delete mode 100644 algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution4.py diff --git a/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py b/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py deleted file mode 100644 index 6405a3b..0000000 --- a/algos/range_queries/PrefixSum/soln/ishanrajsingh/Solution3.py +++ /dev/null @@ -1,36 +0,0 @@ -# Submission Link: https://codeforces.com/contest/1605/submission/356614528 -# Approach: find all 1s that should be 0 (from left) and all 0s that should be 1 (from right), if any mismatches do one operation -# Runtime: O(n) per test -import sys -input=sys.stdin.readline -t=int(input().strip()) -for _ in range(t): - n=int(input().strip()) - s=list(input().strip()) - zeros=sorted([i for i in range(n) if s[i]=='0']) - ones=sorted([i for i in range(n) if s[i]=='1']) - l, r=0,len(zeros)-1 - i, j=0,len(ones)-1 - left=0 - right=n-1 - badL=[] - badR=[] - # two pointers: from left find 1 before a zero that should be left - lptr=0 - rptr=n-1 - while lptr=0 and s[rptr]=='1': - rptr-=1 - if lptr half ones using prefix sums -# Runtime: O((n+m) log q) -import sys -input=sys.stdin.readline - -def check(mid, n, segs, ord): - arr=[0]*n - for i in range(mid): - arr[ord[i]] = 1 - pref=[0]*(n+1) - for i in range(n): - pref[i+1]=pref[i]+arr[i] - for l,r in segs: - if pref[r+1]-pref[l] > (r-l+1)//2: - return True - return False - -t=int(input().strip()) -for _ in range(t): - n,m=map(int,input().split()) - segs=[tuple(map(lambda x: int(x)-1, input().split())) for _ in range(m)] - q=int(input().strip()) - ord=[int(input().strip())-1 for _ in range(q)] - lo,hi=0,q+1 - while hi-lo>1: - mid=(lo+hi)//2 - if check(mid,n,segs,ord): - hi=mid - else: - lo=mid - ans = hi if hi<=q else -1 - print(ans) From ba546359cf8f8445012dc832a6b134c6cc2a4a6e Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:27:48 +0530 Subject: [PATCH 14/14] Add files via upload --- .../PrefixSum/soln/IshanRajSingh/Solution3.py | 36 +++++++++++++++++++ .../PrefixSum/soln/IshanRajSingh/Solution4.py | 33 +++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.py create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.py diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.py b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.py new file mode 100644 index 0000000..6405a3b --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.py @@ -0,0 +1,36 @@ +# Submission Link: https://codeforces.com/contest/1605/submission/356614528 +# Approach: find all 1s that should be 0 (from left) and all 0s that should be 1 (from right), if any mismatches do one operation +# Runtime: O(n) per test +import sys +input=sys.stdin.readline +t=int(input().strip()) +for _ in range(t): + n=int(input().strip()) + s=list(input().strip()) + zeros=sorted([i for i in range(n) if s[i]=='0']) + ones=sorted([i for i in range(n) if s[i]=='1']) + l, r=0,len(zeros)-1 + i, j=0,len(ones)-1 + left=0 + right=n-1 + badL=[] + badR=[] + # two pointers: from left find 1 before a zero that should be left + lptr=0 + rptr=n-1 + while lptr=0 and s[rptr]=='1': + rptr-=1 + if lptr half ones using prefix sums +# Runtime: O((n+m) log q) +import sys +input=sys.stdin.readline + +def check(mid, n, segs, ord): + arr=[0]*n + for i in range(mid): + arr[ord[i]] = 1 + pref=[0]*(n+1) + for i in range(n): + pref[i+1]=pref[i]+arr[i] + for l,r in segs: + if pref[r+1]-pref[l] > (r-l+1)//2: + return True + return False + +t=int(input().strip()) +for _ in range(t): + n,m=map(int,input().split()) + segs=[tuple(map(lambda x: int(x)-1, input().split())) for _ in range(m)] + q=int(input().strip()) + ord=[int(input().strip())-1 for _ in range(q)] + lo,hi=0,q+1 + while hi-lo>1: + mid=(lo+hi)//2 + if check(mid,n,segs,ord): + hi=mid + else: + lo=mid + ans = hi if hi<=q else -1 + print(ans)