You have one array and two query types. For type queries, you use the original array. For type queries, you use the sorted (ascending) array. Build prefix sums for both: on the original, on the sorted copy.
Each query gives you and . The answer is using the appropriate prefix array. Sort once at the start in , then answer each query in .