Let's denote prefix sum as Pi=a1+a2+⋯+ai and suffix sum as Si=ai+ai+1+⋯+an.
Now elaborate the given operation.
∣i=l∑kai−j=p∑raj∣
=∣(al+al+1+⋯+ak)−(ap+ap+1+⋯+ar)∣
=∣{(a1+a2+⋯+ak)−(a1+a2+⋯+al−1)}−{(ap+ap+1+⋯+an)−(ar+1+ar+2+⋯+an)}∣
=∣(Pk−Pl−1)−(Sp−Sr+1)∣
=∣(Pk−Sp)−(Pl−1−Sr+1)∣
Here, as Pl−1 and Sr+1 are constant, the difference depends on Pk and Sp.
Mathematically, ∣Pk−Sp∣=max(Pk−Sp,Sp−Pk). So, the maximum difference can be found in two cases.
Pk−Sp where Pk is maximum prefix sum and Sp is minimum suffix sum.
Sp−Pk where Sp is maximum suffix sum and Pk is minimum prefix sum.
So, take the maximum and the minimum prefix sum and suffix sum in the range [l,r] and check which one gives the maximum difference.
Now, how can you find the maximum and the minimum prefix sum and suffix sum in the range [l,r]?
The answer is Segment Tree. Build a segment tree to find the maximum and the minimum prefix sum. And build another segment tree to find the maximum and the minimum suffix sum. Note that, in the segment trees, you have to keep the indices not the value. Complexity will be O(q×logn)per testcase.