#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 0x3f3f3f3f;
template <class T>
inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T>
inline void PT(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) PT(x / 10);
  putchar(x % 10 + '0');
}
const int N = 1e5 + 100;
const int BLK = 350;
long long a[N];
int t[N];
map<long long, int> mp;
long long cnt[N];
int top;
int nxt[N], pre[N];
pair<pair<int, int>, int> qry[N];
long long res[N];
bool cmp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) {
  if (a.first.first / BLK == b.first.first / BLK) {
    return a.first.second < b.first.second;
  }
  return a.first.first / BLK < b.first.first / BLK;
}
int main() {
  int n, K;
  cin >> n >> K;
  for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
  mp[0] = ++top;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (t[i] == 1)
      a[i] = a[i] + a[i - 1];
    else
      a[i] = a[i - 1] - a[i];
    if (mp[a[i]] == 0) mp[a[i]] = ++top;
    if (mp[a[i] - K]) {
      pre[mp[a[i]]] = mp[a[i] - K];
      nxt[mp[a[i] - K]] = mp[a[i]];
    }
    if (mp[a[i] + K]) {
      nxt[mp[a[i]]] = mp[a[i] + K];
      pre[mp[a[i] + K]] = mp[a[i]];
    }
  }
  for (int i = 0; i <= n; i++) a[i] = mp[a[i]];
  int Q;
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    scanf("%d %d", &qry[i].first.first, &qry[i].first.second);
    qry[i].second = i;
  }
  sort(qry + 1, qry + 1 + Q, cmp);
  cnt[mp[0]]++;
  int L = 0, R = 1;
  long long ans = 0;
  for (int i = 1; i <= Q; i++) {
    int l = qry[i].first.first, r = qry[i].first.second;
    int now = 0;
    while (R <= r) {
      now = a[R];
      if (pre[now]) ans += cnt[pre[now]];
      cnt[now]++;
      R++;
    }
    while (R > r + 1) {
      R--;
      now = a[R];
      cnt[now]--;
      if (pre[now]) ans -= cnt[pre[now]];
    }
    while (L >= l) {
      L--;
      now = a[L];
      if (nxt[now]) ans += cnt[nxt[now]];
      cnt[now]++;
    }
    while (L < l - 1) {
      now = a[L];
      cnt[now]--;
      if (nxt[now]) ans -= cnt[nxt[now]];
      L++;
    }
    res[qry[i].second] = ans;
  }
  for (int i = 1; i <= Q; i++) printf("%lld\n", res[i]);
}
