#include #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr double eps = 1e-9; constexpr ll MOD = 1000000007LL; template bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }; template bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }; template ostream &operator<<(ostream &os, vector v) { for (int i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "\n" : " "); } return os; } template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } template typename enable_if::value == 0>::type fill_v(T &t, const V &v) { t = v; } template typename enable_if::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) { fill_v(e, v); } }; struct Segtree { int n; vector> dat; Segtree(int n_) { n = 1; while (n < n_) { n <<= 1; } dat.resize(2 * n); } /* void upd(int k, int x) { k += n; dat[k] = x; k >>= 1; while (k > 0) { dat[k] = dat[k << 1] + dat[k << 1 | 1]; k >>= 1; } }*/ void build(vector &a) { for (int i = n; i < 2 * n; i++) { if (i - n >= a.size()) { break; } dat[i].emplace_back(a[i - n]); } for (int i = n - 1; i >= 1; i--) { for (auto &e : dat[2 * i]) { dat[i].emplace_back(e); } for (auto &e : dat[2 * i + 1]) { dat[i].emplace_back(e); } sort(all(dat[i])); } } int get(int a, int b, int x, int k, int l, int r) { if (b <= l || r <= a) { return 0; } if (a <= l && r <= b) { int s = lower_bound(all(dat[k]), x) - dat[k].begin(); return s; } return get(a, b, x, 2 * k, l, (l + r) / 2) + get(a, b, x, 2 * k + 1, (l + r) / 2, r); } int get(int a, int b, int x) { if (a >= b) { return 0; } return get(a, b, x, 1, 0, n); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector a(n), b(n, -1); vector

v; for (int i = 0; i < n; i++) { cin >> a[i]; v.push_back(P(a[i], i)); } sort(all(v)); reverse(all(v)); set st; for (int i = 0; i < n; i++) { int id = v[i].second; if (st.empty() || (*st.begin()) > id) { st.insert(id); continue; } auto it = st.lower_bound(id); --it; b[id] = *it; st.insert(id); } Segtree seg(n); seg.build(b); while (q--) { int t, l, r; cin >> t >> l >> r; --l; cout << seg.get(l, r, l) << endl; } }