fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. // Function to calculate the smallest maximum element after skipping one update
  7. long long skipUpdate(int N, int Q, vector<int>& A, vector<vector<int>>& updates) {
  8. // Step 1: Create a difference array for all updates
  9. vector<long long> diff(N + 2, 0);
  10.  
  11. // Apply all updates to the difference array
  12. for (const auto& update : updates) {
  13. int L = update[0];
  14. int R = update[1];
  15. int X = update[2];
  16. diff[L] += X;
  17. if (R + 1 <= N) {
  18. diff[R + 1] -= X;
  19. }
  20. }
  21.  
  22. // Step 2: Compute the fully updated array
  23. vector<long long> fullArray(N + 1, 0);
  24. fullArray[1] = A[0] + diff[1];
  25. for (int i = 2; i <= N; ++i) {
  26. diff[i] += diff[i - 1];
  27. fullArray[i] = A[i - 1] + diff[i];
  28. }
  29.  
  30. // Step 3: Calculate prefix and suffix maximums
  31. vector<long long> prefixMax(N + 2, LLONG_MIN);
  32. vector<long long> suffixMax(N + 2, LLONG_MIN);
  33.  
  34. // Prefix maximums: max up to index i
  35. for (int i = 1; i <= N; ++i) {
  36. prefixMax[i] = max(prefixMax[i - 1], fullArray[i]);
  37. }
  38.  
  39. // Suffix maximums: max from index i to end
  40. for (int i = N; i >= 1; --i) {
  41. suffixMax[i] = max(suffixMax[i + 1], fullArray[i]);
  42. }
  43.  
  44. // Step 4: Iterate through each update and calculate the result
  45. long long minMaxValue = LLONG_MAX;
  46.  
  47. for (const auto& update : updates) {
  48. int L = update[0];
  49. int R = update[1];
  50. int X = update[2];
  51.  
  52. // Calculate the maximum without applying this update
  53. long long currentMax = LLONG_MIN;
  54.  
  55. // Left part before L
  56. if (L > 1) {
  57. currentMax = max(currentMax, prefixMax[L - 1]);
  58. }
  59.  
  60. // Right part after R
  61. if (R < N) {
  62. currentMax = max(currentMax, suffixMax[R + 1]);
  63. }
  64.  
  65. minMaxValue = min(minMaxValue, currentMax);
  66. }
  67.  
  68. return minMaxValue;
  69. }
  70.  
  71. int main() {
  72. int T;
  73. cin >> T;
  74.  
  75. while (T--) {
  76. int N, Q;
  77. cin >> N >> Q;
  78.  
  79. vector<int> A(N);
  80. for (int i = 0; i < N; ++i) {
  81. cin >> A[i];
  82. }
  83.  
  84. vector<vector<int>> updates(Q, vector<int>(3));
  85. for (int i = 0; i < Q; ++i) {
  86. cin >> updates[i][0] >> updates[i][1] >> updates[i][2];
  87. }
  88.  
  89. cout << skipUpdate(N, Q, A, updates) << endl;
  90. }
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty