#include using namespace std; typedef pair pii; typedef long long ll; typedef vector vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() #define dump(x) cout << #x << " = " << (x) << endl constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } template ostream& operator<<(ostream& os, const pair& p) { os<<"("< ostream& operator<<(ostream& os, const vector& v) { os<<"{"; rep(i, v.size()) { if (i) os<<","; os< class BIT { T bit[MN]; int n; public: BIT() { n = MN; memset(bit, 0, sizeof(bit)); } T sum(int i) { ++i; T s = 0; while (i > 0) { s += bit[i]; i -= i & -i; } return s; } void add(int i, T x) { ++i; while (i < n) { bit[i] += x; i += i & -i; } } }; const int SZ = 1 << 17; struct segtree { vector dat; void init() { dat.resize(SZ * 2); rep(i, SZ * 2) dat[i] = INT_MIN; } void update(int pos, int x) { pos += SZ - 1; dat[pos] = x; while (pos > 0) { pos = (pos - 1) / 2; dat[pos] = max(dat[pos * 2 + 1], dat[pos * 2 + 2]); } } int get(int a, int b, int k = 0, int l = 0, int r = SZ) { if (b <= l || r <= a) return INT_MIN; if (a <= l && r <= b) return dat[k]; return max(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r)); } } seg; int main() { seg.init(); BIT T; int N, Q; cin >> N >> Q; vi a(N), tol(N); rep(i, N) { cin >> a[i]; --a[i]; int t = seg.get(a[i] + 1, N); if (t == INT_MIN) { tol[i] = -1; } else { tol[i] = t; } seg.update(a[i], i); } dump(tol); vector vals; rep(i, N) vals.eb(tol[i], i); sort(ALL(vals)); int now = 0; vector> query(N); rep(i, Q) { int t, l, r; cin >> t >> l >> r; --l; --r; query[l].eb(r, i); } vi ans(Q, -1); rep(l, N) { while (now < N) { int x = vals[now].fi, p = vals[now].se; if (x < l) { T.add(p, 1); now++; } else { break; } } for (auto pr : query[l]) { int id = pr.se, r = pr.fi; ans[id] = T.sum(r) - T.sum(l-1); } } rep(i, Q) { printf("%d\n", ans[i]); } return 0; }