#include<bits/stdc++.h> usingnamespace std; constint N = 500010; int n, m; int a[N], b[N]; int idx[N], tt; int t[N]; int ans[N]; vector<int> tong[N]; vector<int> qus[N]; structnode { int a, b, idx; }sta[N]; structnod1 { int l, r; }q[N]; intlowbit(int x) { return x & (-x); } voidadd(int x) { for (int i = x; i <= n; i += lowbit(i)) t[i]++; } intsum(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += t[i]; return ans; } intmain() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); sta[0] = { -1, 0, 0 }; for (int i = 1; i <= n; i++) { while (((sta[tt].a == a[i]) || (sta[tt].b <= b[i])) && (tt > 0)) tt--; idx[i] = sta[tt].idx; sta[++tt] = { a[i], b[i], i }; } for (int i = 1; i <= n; i++) tong[idx[i]].push_back(i); for (int i = 1; i <= m; i++) { scanf("%d%d", &q[i].l, &q[i].r); qus[q[i].l].push_back(i); } for (int i = 1; i <= n; i++) { for (int k = 0; k < tong[i - 1].size(); k++) add(tong[i - 1][k]); for (auto now : qus[i]) { int l = q[now].l, r = q[now].r; ans[now] = sum(r) - sum(l - 1); } } for (int i = 1; i <= m; i++) cout << ans[i] << endl; return0; }