Segment Tree with sum

Revision en1, by PhungDucMinhSobad, 2024-11-04 18:13:16

Given an array A of N elements a1, a2, a3, ..., αγ. There are queries, each query specifies two integers l, r and requests to swap the elements at positions I and r (where 1≤ I ≤ r ≤ N). Requirement: After each query, ensure that the array does not have consecutive subarrays with the same sum of alternating signs. The objective is to find the sum of the subarray with the largest alternating-sign sum. Input The first line contains an integer T, the number of test cases. Each test case is formatted as follows: The first line contains two integers N and Q (1 ≤ N, Q≤ 3 × 10^5). The second line contains N integers ai (1 ≤ ai ≤ 10^9), representing the elements of the array. The next lines contain two integers I and r, each representing a swap operation. Output For each test case, the output should be formatted as follows: The first line should contain the sum of the largest alternating-sign subarray in the initial array. For the next Q lines, each line should contain the sum of the largest alternating-sign subarray after the respective swap operation. Example: For array A = [1, 2, 5, 4, 3, 6, 7], the initial largest alternating-sign sum is calculated for indices [3, 5, 7], resulting in a sum S5-3+7=9.

Tags segment tree

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English PhungDucMinhSobad 2024-11-04 18:14:16 107
en1 English PhungDucMinhSobad 2024-11-04 18:13:16 1248 Initial revision (published)