#pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx") #include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// struct SegTree { using T_NODE = vector; using T_QUERY = int; using T_RETVAL = int; // number T_NODE ZeroNode = vector{}; T_RETVAL ZeroRetval = 0; int N; int head; vector x; static T_NODE node_merger(const T_NODE &l, const T_NODE &r) { /* node merging */ if (l.empty()) return r; T_NODE ret = l; REP(i, r.size()) if (r[i] > ret.back()) { ret.insert(ret.end(), r.begin() + i, r.end()); break; } return ret; } static T_RETVAL get_query(const T_NODE &node, T_QUERY &q) { /* retval calculating */ auto itr = upper_bound(node.begin(), node.end(), q); if (itr == node.end()) return 0; else { q = *prev(node.end()); return node.end() - itr; } } T_RETVAL _get(int begin, int end, int pos, int l, int r, T_QUERY &q) const { if (r <= begin or l >= end) return ZeroRetval; else if (l >= begin and r <= end) return get_query(x[pos], q); else { int a = _get(begin, end, 2 * pos + 1, l, (l + r) / 2, q); int b = _get(begin, end, 2 * pos + 2, (l + r) / 2, r, q); return a + b; } } SegTree(int N = 0) : N(N) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp * 2, ZeroNode), head = N_tmp - 1; } SegTree(const vector &vals) : N(vals.size()) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, ZeroNode), head = N_tmp - 1; copy(vals.begin(), vals.end(), x.begin() + head); IREP(i, head) x[i] = node_merger(x[i * 2 + 1], x[i * 2 + 2]); } void update(int pos, T_NODE val) { pos += head, x[pos] = val; while (pos) pos = (pos - 1) / 2, x[pos] = node_merger(x[pos*2+1], x[pos*2+2]); } T_RETVAL get(int begin, int end, T_QUERY q) const { return _get(begin, end, 0, 0, (int)x.size() / 2, q); } // friend ostream &operator<<(ostream &os, const SegTree &s) { os << "["; REP(i, s.N) os << s.get(i, i + 1) << ","; os << "]"; return os; } }; int main() { int N, Q; cin >> N >> Q; vector> A(N); REP(i, N) { lint a; cin >> a; A[i].emplace_back(a); } SegTree st(A); REP(_, Q) { int q, l, r; cin >> q >> l >> r; int x = 0; printf("%d\n", st.get(l - 1, r, x)); } }