#include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = s; i <= (ll)(n); i++) #define rep3(i, s, n, d) for (ll i = s; i <= (ll)(n); i += d) #define rep4(i, s, n, d) for (ll i = s; i >= (ll)(n); i += d) typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef vector vi; typedef vector vvi; typedef vector vvvi; typedef vector vvvvi; typedef vector vs; typedef vector vvs; typedef vector vvvs; typedef vector vc; typedef vector vvc; typedef vector vvvc; typedef vector vll; typedef vector vvll; typedef vector vvvll; typedef vector vvvvll; typedef vector vd; typedef vector vvd; typedef vector vvvd; typedef vector vld; typedef vector vvld; typedef vector vvvld; typedef vector vb; typedef vector vvb; typedef vector vvvb; typedef vector> vpi; typedef vector> vpll; typedef pair pi; typedef vector vpi; typedef vector vvpi; typedef pair pll; typedef vector vpll; typedef vector vvpll; typedef tuple tui3; typedef tuple tull3; typedef priority_queue, greater> pqi; typedef priority_queue, greater> pqvi; typedef priority_queue, greater> pqll; typedef priority_queue, greater> pqvll; typedef priority_queue, greater> pqpll; typedef priority_queue, less> rpqpll; typedef priority_queue, less> rpqi; typedef priority_queue, less> rpqvi; typedef priority_queue, greater> pqtui3; typedef priority_queue, less> rpqtui3; typedef priority_queue, greater> pqtull3; typedef priority_queue, less> rpqtull3; #define yes(ans) if(ans)cout << "yes"<< endl; else cout << "no" << endl #define Yes(ans) if(ans)cout << "Yes"<< endl; else cout << "No" << endl #define YES(ans) if(ans)cout << "YES"<< endl ;else cout << "NO" << endl #define printv(vec) {rep(i, vec.size()) cout << vec[i] << ' '; cout << endl;} #define printvv(vec) rep(i, vec.size()) {rep(j, vec[i].size()) cout << vec[i][j] << ' '; cout << endl;}; #define printvvv(vec) rep(i, vec.size()) { rep(j, vec[i].size()) { rep(k, vec[i][j].size()) cout << vec[i][j][k] << ' '; cout << " "; }cout << endl; }; #define all1(x) x.begin(),x.end() #define all2(x) x.rbegin(), x.rend() #define so(x) sort(all1(x)) #define re(x) reverse(all1(x)) #define rso(x) sort(all2(x)) #define vco(x, a) count(all1(x), a) #define per(x) next_permutation(all1(x)) #define iINF 2147483647 #define llINF 9223372036854775807 #define INF 1000000000000000000 #define mod 998244353 #define mod2 1000000007 template struct SegTree { using FX = function; // X•X -> X となる関数の型 int n; FX fx; const X ex; vector dat; SegTree(int n_, FX fx_, X ex_) : n(), fx(fx_), ex(ex_), dat(n_ * 4, ex_) { int x = 1; while (n_ > x) { x *= 2; } n = x; } void set(int i, X x) { dat[i + n - 1] = x; } void build() { for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]); } void update(int i, X x) { i += n - 1; dat[i] += x; while (i > 0) { i = (i - 1) / 2; // parent dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]); } } X query(int a, int b) { return query_sub(a, b, 0, 0, n); } X query_sub(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return ex; } else if (a <= l && r <= b) { return dat[k]; } else { X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return fx(vl, vr); } } }; int main() { // アルゴリズム一覧を見る // 嘘解法ですか ll n, q; cin >> n >> q; vvll vec(n, vll(2)); rep(i, n) cin >> vec[i][0]; rep(i, n) vec[i][1] = i; so(vec); vll num(n); rep(i, n) num[vec[i][1]] = i; vll list(n); rep(i, n) list[i] = vec[i][0]; list.erase(unique(all1(list)), list.end()); rep(i, n) vec[i][0] = lower_bound(all1(list), vec[i][0])-list.begin(); auto fx = [](ll x1, ll x2) -> ll {return x1+x2;}; SegTree seg(n, fx, 0); rep(i, n) seg.set(vec[i][0], 1); seg.build(); while(q--){ ll x, y; cin >> x >> y; x--, y--; x = num[x]; y = num[y]; cout << seg.query(vec[y][0]+1, vec[x][0]) << endl; // cout << vec[y][0] << ' ' << vec[x][0] << endl; } }