Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <iostream>#include <algorithm>int a[20000];std::pair<int,int> pa[20000];int n, m, T, l, r, i;long long res;int main() {std::cin >> T;while (T--) {std::cin >> n >> m;for (i = 0; i < n; i++) {std::cin >> pa[i].first;pa[i].second = i+1;}std::sort(pa, pa+n);while (m--) {std::cin >> l >> r;int found = r-l+1;res = 0;for (i = 0; i < n; i++) {if (pa[i].second >= l && pa[i].second <= r) {res += found--*pa[i].first;if (found == 0)break;}}std::cout << res << '\n';}