#include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<vector> #include<set> #define int long long usingnamespace std; constint N = 1e6 + 10; int n, m; int val1[N], val2[N]; int a[N]; int q[N], top; vector<pair<int, int> > l[N]; intlowbit(int x){ return x & (-x); } voidchange(int x, int y, int v){ for (int i = x; i <= n; i += lowbit(i)) { val1[i] += v, val2[i] += v * x; } for (int i = y + 1; i <= n; i += lowbit(i)) { val1[i] -= v, val2[i] -= v * (y + 1); } } intquery(int x, int y){ if (x > y) return0; int res = 0; for (int i = y; i; i -= lowbit(i)) { res -= val2[i], res += val1[i] * (y + 1); } for (int i = x - 1; i; i -= lowbit(i)) { res += val2[i], res -= val1[i] * (x); } return res; } int ans[N]; int x[N], y[N]; signedmain(){ scanf("%lld%lld", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); } for (int i = 1; i <= m; ++i) { scanf("%lld", &x[i]); } for (int i = 1; i <= m; ++i) { scanf("%lld", &y[i]); ans[i] = y[i] - x[i] + 1; } for (int i = 1; i <= m; ++i) { l[y[i]].push_back(make_pair(x[i], i)); } for (int i = 1; i <= n; ++i) { int las = 0, fir = 0; fir = q[top]; while (top && a[i] > a[q[top]]) las = q[--top]; if (top) change(i, i, query(q[top], q[top]) + 1); elsechange(i, i, 1); if (fir != las) { change(q[top] + 1, i - 1, 1); } q[++top] = i; for (auto it : l[i]) { int w = *lower_bound(q + 1, q + top + 1, it.first); ans[it.second] += query(w + 1, i) - (i - w) * query(w, w); } } top = 0; for (int i = 1; i <= n; ++i) { val1[i] = val2[i] = 0; } reverse(a + 1, a + n + 1); for (int i = 1; i <= m; ++i) { swap(x[i], y[i]); x[i] = n - x[i] + 1; y[i] = n - y[i] + 1; } for (int i = 1; i <= n; ++i) { l[i].clear(); } for (int i = 1; i <= m; ++i) { l[y[i]].push_back(make_pair(x[i], i)); } for (int i = 1; i <= n; ++i) { int las = 0, fir = 0; fir = q[top]; while (top && a[i] > a[q[top]]) las = q[--top]; if (top) change(i, i, query(q[top], q[top]) + 1); elsechange(i, i, 1); if (fir != las) { change(q[top] + 1, i - 1, 1); } q[++top] = i; for (auto it : l[i]) { int w = *lower_bound(q + 1, q + top + 1, it.first); ans[it.second] += query(w + 1, i) - (i - w) * query(w, w); } } for (int i = 1; i <= m; ++i) { printf("%lld ", ans[i]); } return0; }