#include <bits/stdc++.h>
using namespace std;
long long n, m, k, l, r, c[200000], t[200000], i, j, CNT, cnt, Count[400000];
long long p[200000], ans, K, L, R, a, b, answer[200000];
long long cnt1[200000], cnt2[200000], cnt3[200000];
struct query {
  long long l, r, ind;
} Q, q[200000];
struct st {
  long long x, ind, type;
} h[400000];
bool cmp(query a, query b) {
  if (a.l / K != b.l / K) return a.l < b.l;
  return a.r < b.r;
}
bool cmp2(st a, st b) { return a.x < b.x; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  K = int(sqrt(n));
  for (i = 1; i <= n; i++) cin >> t[i];
  for (i = 1; i <= n; i++) {
    cin >> c[i];
    if (t[i] == 2) c[i] = -c[i];
  }
  for (i = 0; i <= n; i++) {
    if (i) p[i] = p[i - 1] + c[i];
    h[cnt].type = 1;
    h[cnt].x = p[i];
    h[cnt].ind = h[cnt + 1].ind = h[cnt + 2].ind = i;
    h[cnt + 1].x = p[i] + k;
    h[cnt + 2].x = p[i] - k;
    h[cnt + 1].type = 2;
    h[cnt + 2].type = 3;
    cnt += 3;
  }
  stable_sort(h, h + cnt, cmp2);
  for (i = 0; i < cnt; i++) {
    if (i && h[i - 1].x != h[i].x) CNT++;
    if (h[i].type == 1) cnt1[h[i].ind] = CNT;
    if (h[i].type == 2) cnt2[h[i].ind] = CNT;
    if (h[i].type == 3) cnt3[h[i].ind] = CNT;
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].l--;
    q[i].ind = i;
  }
  stable_sort(q, q + m, cmp);
  a = 0;
  b = -1;
  for (i = 0; i < m; i++) {
    L = q[i].l;
    R = q[i].r;
    while (a > L) {
      ans += Count[cnt2[a - 1]];
      Count[cnt1[a - 1]]++;
      a--;
    }
    while (b < R) {
      ans += Count[cnt3[b + 1]];
      Count[cnt1[b + 1]]++;
      b++;
    }
    while (a < L) {
      Count[cnt1[a]]--;
      ans -= Count[cnt2[a]];
      a++;
    }
    while (b > R) {
      Count[cnt1[b]]--;
      ans -= Count[cnt3[b]];
      b--;
    }
    answer[q[i].ind] = ans;
  }
  for (i = 0; i < m; i++) cout << answer[i] << endl;
  return 0;
}
