#include <bits/stdc++.h>
using namespace std;
long long n, k, m;
long long t[300005];
long long a[300005];
long long s;
long long res[300005], ans[300005];
long long cnt[300005];
long long pre[300005], p[300005], pp[300005];
struct query {
  long long l;
  long long r;
  long long id;
} q[300005];
bool cmp(query a, query b) {
  if (a.l / s != b.l / s) return a.l / s < b.l / s;
  return a.r / s < b.r / s;
}
void in() {
  scanf("%lld%lld", &n, &k);
  s = sqrt(n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &t[i]);
  }
  pre[0] = 0;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (t[i] == 1) {
      pre[i] = pre[i - 1] + a[i];
    } else {
      pre[i] = pre[i - 1] - a[i];
    }
  }
  set<long long> al;
  map<long long, long long> idx;
  long long cnt = 0;
  for (long long i = 0; i <= n; i++) {
    if (al.empty() || al.find(pre[i] - k) == al.end()) {
      al.insert(pre[i] - k);
      cnt++;
      idx[pre[i] - k] = cnt;
      p[i] = cnt;
    } else {
      p[i] = idx[pre[i] - k];
    }
    if (al.empty() || al.find(pre[i] + k) == al.end()) {
      al.insert(pre[i] + k);
      cnt++;
      idx[pre[i] + k] = cnt;
      pp[i] = cnt;
    } else {
      pp[i] = idx[pre[i] + k];
    }
    if (al.empty() || al.find(pre[i]) == al.end()) {
      al.insert(pre[i]);
      cnt++;
      idx[pre[i]] = cnt;
      pre[i] = cnt;
    } else {
      pre[i] = idx[pre[i]];
    }
  }
  scanf("%lld", &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%lld%lld", &q[i].l, &q[i].r);
    q[i].id = i;
  }
}
void solve() {
  sort(q + 1, q + m + 1, cmp);
  long long l = 1, r = 0;
  long long rf = 0;
  for (long long i = 1; i <= m; i++) {
    long long cur = 0;
    while (l < q[i].l) {
      cnt[pre[l]]--;
      cur -= cnt[pp[l]];
      l++;
    }
    while (l > q[i].l) {
      l--;
      cur += cnt[pp[l]];
      cnt[pre[l]]++;
    }
    while (r < q[i].r) {
      r++;
      cur += cnt[p[r]];
      cnt[pre[r]]++;
    }
    while (r > q[i].r) {
      cnt[pre[r]]--;
      cur -= cnt[p[r]];
      r--;
    }
    rf += cur;
    res[q[i].id] = rf + cnt[pp[l - 1]];
  }
}
void ou() {
  for (long long i = 1; i <= m; i++) {
    printf("%lld\n", res[i]);
  }
}
signed main() {
  in();
  solve();
  ou();
}
