Comments

could someone help me with the problem c. My logic was on par with the editorial, but there might be something wrong with the code.

#include <bits/stdc++.h>

using namespace std; 

/*

*/

void solve () {
	int n, q; 
	cin >> n >> q; 
	vector<int> arr(n+1), arr2; 
	for (int i = 1; i <= n; i++){
		cin >> arr[i]; 
	}
	arr2 = arr; 
	int answers[n+1]; 
	int ma = arr[1]; 
	memset(answers, 0, sizeof(answers)); 
	for (int i =2;i <= n;i++) {
		if (ma > arr[i]) {
			answers[ma] ++; 
		}else {
			answers[arr[i]]++; 
			ma = arr[i]; 
		}
	}

	while (q--) {
		int i, k; 
		cin >> i >> k; 
		if (i <= k+1) {
			int as = k - i + 1;
			if (answers[arr[i]] == 0) {
				cout << 0 << endl; 
				continue; 
			}

			if (i == 1) {
				cout << min (answers[arr[i]], as) << endl; 
				continue; 
			}
			
			int ans = 1+min(answers[arr[i]]-1, as); 
			if (arr[i] == n) {
				ans = 1+as; 
			}
			cout << ans << endl; 
		}else {
			cout << 0 << endl; 
		}
	}
}

int main () {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		solve(); 
	}
	
	return 0; 
}

kotaro lives alone