#include <bits/stdc++.h>
const int M = 1e5 + 5;
int len;
long long lsh[M * 3];
int n, m, k, p, t[M], v[M], nxt[M], pre[M];
long long CB[M * 3];
long long now, val[M], ans[M];
struct Query {
  int L, R, p, id;
  inline bool operator<(const Query& it) const {
    return p == it.p ? p & 1 ? R < it.R : R > it.R : L < it.L;
  }
} q[M];
inline void Add(const int& id, const int& to) {
  now += CB[to];
  ++CB[v[id]];
}
inline void Del(const int& id, const int& to) {
  --CB[v[id]];
  now -= CB[to];
}
signed main() {
  register int i, L = 1, R = 0;
  scanf("%d%d", &n, &k);
  lsh[++len] = 0;
  lsh[++len] = k;
  lsh[++len] = -k;
  for (i = 1; i <= n; ++i) scanf("%d", t + i);
  for (i = 1; i <= n; ++i) {
    scanf("%lld", val + i);
    if (t[i] == 1)
      val[i] = val[i - 1] + val[i];
    else
      val[i] = val[i - 1] - val[i];
    lsh[++len] = val[i];
    lsh[++len] = val[i] - k;
    lsh[++len] = val[i] + k;
  }
  std::sort(lsh + 1, lsh + len + 1);
  len = std::unique(lsh + 1, lsh + len + 1) - lsh - 1;
  for (i = 0; i <= n; ++i) {
    v[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i]) - lsh;
    pre[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i] - k) - lsh;
    nxt[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i] + k) - lsh;
  }
  scanf("%d", &m);
  p = ceil(n / sqrt(m * 2.0 / 3));
  for (i = 1; i <= m; ++i) {
    scanf("%d%d", &q[i].L, &q[i].R);
    --q[i].L;
    q[i].id = i;
    q[i].p = q[i].L / p;
  }
  std::sort(q + 1, q + m + 1);
  for (i = 1; i <= m; ++i) {
    const int &QL = q[i].L, &QR = q[i].R;
    while (R < QR) ++R, Add(R, pre[R]);
    while (L > QL) --L, Add(L, nxt[L]);
    while (L < QL) Del(L, nxt[L]), ++L;
    while (R > QR) Del(R, pre[R]), --R;
    ans[q[i].id] = now;
  }
  for (i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
}
