#include <bits/stdc++.h>
using namespace std;
long long n, k, sqtn, q;
long long pre[100010];
vector<long long> all;
long long addk[100010];
long long mink[100010];
long long self[100010];
long long cntpre[300010];
struct query {
  long long l;
  long long r;
  long long id;
} Q[100010];
bool operator<(query a, query b) {
  if (a.l / sqtn == b.l / sqtn) {
    return a.r < b.r;
  }
  return a.l / sqtn < b.l / sqtn;
}
long long ans[100010];
signed main() {
  long long tmp;
  cin >> n >> k;
  sqtn = sqrt(n);
  for (long long i = 1; i <= n; i++) {
    cin >> pre[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> tmp;
    if (pre[i] == 1) {
      pre[i] = pre[i - 1] + tmp;
    } else {
      pre[i] = pre[i - 1] - tmp;
    }
  }
  all.push_back(k);
  all.push_back(-k);
  all.push_back(0);
  for (long long i = 1; i <= n; i++) {
    all.push_back(pre[i] + k);
    all.push_back(pre[i] - k);
    all.push_back(pre[i]);
  }
  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());
  self[0] = lower_bound(all.begin(), all.end(), pre[0]) - all.begin();
  addk[0] = lower_bound(all.begin(), all.end(), pre[0] + k) - all.begin();
  mink[0] = lower_bound(all.begin(), all.end(), pre[0] - k) - all.begin();
  for (long long i = 1; i <= n; i++) {
    self[i] = lower_bound(all.begin(), all.end(), pre[i]) - all.begin();
    addk[i] = lower_bound(all.begin(), all.end(), pre[i] + k) - all.begin();
    mink[i] = lower_bound(all.begin(), all.end(), pre[i] - k) - all.begin();
  }
  long long l = 1, r = 0, now = 0;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].id = i;
  }
  sort(Q, Q + q);
  for (long long i = 0; i < q; i++) {
    while (l < Q[i].l) {
      cntpre[self[l]]--;
      now -= cntpre[addk[l]];
      l++;
    }
    while (l > Q[i].l) {
      l--;
      now += cntpre[addk[l]];
      cntpre[self[l]]++;
    }
    while (r < Q[i].r) {
      r++;
      now += cntpre[mink[r]];
      cntpre[self[r]]++;
    }
    while (r > Q[i].r) {
      cntpre[self[r]]--;
      now -= cntpre[mink[r]];
      r--;
    }
    ans[Q[i].id] = now;
  }
  for (long long i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
