April lunch time FAPF

why taking upper limit and lower limit is not working here??
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
int main(){
int t; cin >> t;
while(t–){
int n, q; cin >> n >> q;
vector v(n);
vector vsort(n);
for(int i=0;i<n;i++){
cin >> v[i];
vsort[i]=v[i];
}

	sort(vsort.begin(), vsort.end());
	//map<ll, pair<ll,ll> > valueCounts;

	while(q--){
		ll x,y;
		cin >> x >> y;
		ll coins = abs(v[x-1] - v[y-1]) + y-x;
  //ll len;
  vector<ll>::iterator lower,upper;
  lower = lower_bound (vsort.begin(), vsort.end(), v[x-1]);
  upper = upper_bound (vsort.begin(), vsort.end(), v[y-1]);

		cout << coins << " " << abs(upper-lower) << endl;
	}

}
return 0;

}

You are assuming that v[y-1] >= v[x-1] ,
But what if v[y-1] < v[x-1] ??

1 Like

got the mistake, silly mistake: ((