Output

Example

Ohhhaaa This one is tough man,Specially for me who dont know how the f we can store 3,4 values simultaneously in a node.Took me 4-5 days understanding and solving.Can This problem be solved using BITs?

@anurag_333
No, i tried solving it using kadane but it gave me tle for obvious reasons that it can be O(n) in worst case !!
Also you have to check every time you query so it exceeds the time limit and ultimately you are better with seg_trees..