/* -*- coding: utf-8 -*- * * 878.cc: No.878 Range High-Element Query - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int K = 18; const int MAX_E2 = 1 << K; // = 262144 const int INF = 1 << 30; /* typedef */ template struct SegTreeMin { int n, e2; T nodes[MAX_E2], inf; SegTreeMin() {} void init(int _n, T _inf) { n = _n; inf = _inf; for (e2 = 1; e2 < n; e2 <<= 1); fill(nodes, nodes + MAX_E2, inf); } T &get(int i) { return nodes[e2 - 1 + i]; } void set(int i, T v) { int j = e2 - 1 + i; nodes[j] = v; while (j > 0) { j = (j - 1) / 2; nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]); } } void setall() { for (int j = e2 - 2; j >= 0; j--) nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]); } T min_range(int r0, int r1, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return inf; if (r0 <= i0 && i1 <= r1) return nodes[k]; int im = (i0 + i1) / 2; T v0 = min_range(r0, r1, k * 2 + 1, i0, im); T v1 = min_range(r0, r1, k * 2 + 2, im, i1); return min(v0, v1); } T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); } }; /* global variables */ int as[MAX_N], nxts[MAX_N][K]; SegTreeMin st; /* subroutines */ /* main */ int main() { int n, qn; scanf("%d%d", &n, &qn); for (int i = 0; i < n; i++) scanf("%d", as + i), as[i]--; st.init(n, INF); for (int i = n - 1; i >= 0; i--) { nxts[i][0] = st.min_range(as[i] + 1, n); for (int k = 0; k + 1 < K; k++) { int &x = nxts[i][k]; nxts[i][k + 1] = (x < INF) ? nxts[x][k] : INF; } st.set(as[i], i); } while (qn--) { int c, l, r; scanf("%d%d%d", &c, &l, &r); l--; int cnt = 1; for (;;) { int k = K - 1; for (; k >= 0 && nxts[l][k] >= r; k--); if (k < 0) break; cnt += (1 << k); l = nxts[l][k]; } printf("%d\n", cnt); } return 0; }