#include #include #include #include #include #include using namespace std; int main() { int n, q; cin >> n >> q; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; vector > qs[n]; for (int i = 0; i < q; i++) { int x, l, r; cin >> x >> l >> r; qs[l-1].push_back({r-1, i}); } stack > st; int bit[n+1]; memset(bit, 0, sizeof(bit)); vector ans(q, 0); for (int i = n-1; i >= 0; i--) { while (!st.empty() && st.top().first < a[i]) { for (int x = st.top().second+1; x <= n; x += x&-x) bit[x]--; st.pop(); } st.push({a[i], i}); for (int x = i+1; x <= n; x += x&-x) bit[x]++; for (auto query : qs[i]) { for (int x = query.first+1; x > 0; x -= x&-x) ans[query.second] += bit[x]; } } for (int a : ans) cout << a << endl; }