#include using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long LL; typedef pair P; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template T gcd(T a,T b){return b?gcd(b,a%b):a;} const LL mod=1000000007; const LL LINF=1LL<<60; const int INF=1<<30; int dx[]={1,0,-1,0,1,-1,1,-1}; int dy[]={0,1,0,-1,1,-1,-1,1}; struct SegmentTree{ private: int n; vector node; public: SegmentTree(vector v){ int sz = v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1,-INF); for (int i = 0; i < sz; i++) node[i+n-1] = v[i]; for (int i = n-2; i >= 0; i--) node[i] = max(node[2*i+1],node[2*i+2]); } void update(int x,int val){ x += n-1; node[x] = val; while(x > 0){ x = (x - 1) / 2; node[x] = max(node[2*x+1],node[2*x+2]); } } int getmax(int a, int b, int k=0, int l=0, int r=-1){ if (r < 0) r = n; if(r <= a || b <= l) return -INF; if(a <= l && r <= b) return node[k]; int vl = getmax(a, b, 2*k+1, l , (l+r)/2); int vr = getmax(a, b, 2*k+2, (l+r)/2,r); return max(vl, vr); } }; template< typename T > struct BinaryIndexedTree{ //0-indexed vector< T > bit; BinaryIndexedTree(int sz){ bit.assign(sz, 0); } //[0,k)までの総和を返す T sum(int k){ T ret = 0; for (--k; k >= 0; k = (k&(k+1))-1) ret += bit[k]; return ret; } //[l,r)の総和を返す T sum(int l, int r){ return sum(r) - sum(l); } //kにx加算する void add(int k, T x){ for (; k < bit.size(); k |= k+1) bit[k] += x; } }; int main(){ int n,q;cin >> n >> q; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } SegmentTree seg(a); vector> Q(q); for (int i = 0; i < q; i++) { int t;cin >> t; cin >> Q[i].fs.fs >> Q[i].fs.sc; Q[i].fs.fs--; Q[i].sc = i; } sort(ALL(Q)); vector

mxid(n); for (int i = 0; i < n; i++) { int l = -1, r = i; while(r - l > 1){ int m = (r + l) / 2; if(seg.getmax(m, i+1) == a[i]) r = m; else l = m; } mxid[i] = mp(l + 1, i); } sort(ALL(mxid)); BinaryIndexedTree bit(n); int idx = 0, idq = 0; vector ans(q); for (int i = 0; i < n; i++) { while(idx < n && mxid[idx].fs == i){ bit.add(mxid[idx].sc, 1); idx++; } while(idq < q && Q[idq].fs.fs == i){ int id = Q[idq].sc; int l = Q[idq].fs.fs, r = Q[idq].fs.sc; ans[id] = bit.sum(l, r); idq++; } } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }