#include <bits/stdc++.h>
using namespace std;
int Size, belong[100010];
struct Question {
  int l, r, id;
  Question() {}
  Question(int _, int __, int ___) : l(_), r(__), id(___) {}
};
inline bool cmp(const Question& a, const Question& b) {
  if (belong[a.l] == belong[b.l]) return a.r < b.r;
  return belong[a.l] < belong[b.l];
}
int type[100010], a[100010], n, k;
long long sum[100010], Ans[100010];
long long ma[100010 * 3], all[100010 * 3], pre[100010], nxt[100010],
    now[100010], cnt;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  Question q[n + 1];
  Size = (int)sqrt(n);
  for (int i = 1; i < (n + 1); i++) {
    scanf("%d", &type[i]);
  }
  for (int i = 1; i < (n + 1); i++) {
    scanf("%d", &a[i]);
    if (type[i] == 2) a[i] = -a[i];
    belong[i] = (i - 1) / Size + 1;
  }
  all[++cnt] = 0;
  all[++cnt] = -k;
  all[++cnt] = k;
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
    all[++cnt] = sum[i] - k;
    all[++cnt] = sum[i] + k;
    all[++cnt] = sum[i];
  }
  sort(all + 1, all + 1 + cnt);
  int m = unique(all + 1, all + 1 + cnt) - all - 1;
  now[0] = lower_bound(all + 1, all + 1 + m, 0) - all;
  pre[0] = lower_bound(all + 1, all + 1 + m, -k) - all;
  nxt[0] = lower_bound(all + 1, all + 1 + m, k) - all;
  for (int i = 1; i <= n; i++) {
    pre[i] = lower_bound(all + 1, all + 1 + m, sum[i] - k) - all;
    nxt[i] = lower_bound(all + 1, all + 1 + m, sum[i] + k) - all;
    now[i] = lower_bound(all + 1, all + 1 + m, sum[i]) - all;
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    q[i] = Question(l, r, i);
  }
  sort(q + 1, q + 1 + Q, cmp);
  int l = 1, r = 0;
  long long ans = 0;
  now[0] = lower_bound(all + 1, all + 1 + m, 0) - all;
  pre[0] = lower_bound(all + 1, all + 1 + m, -k) - all;
  nxt[0] = lower_bound(all + 1, all + 1 + m, k) - all;
  ma[now[0]]++;
  for (int i = 1; i < (Q + 1); i++) {
    while (l < q[i].l) {
      ma[now[l - 1]]--;
      ans -= ma[nxt[l - 1]];
      l++;
    }
    while (l > q[i].l) {
      l--;
      ans += ma[nxt[l - 1]];
      ma[now[l - 1]]++;
    }
    while (r < q[i].r) {
      r++;
      ans += ma[pre[r]];
      ma[now[r]]++;
    }
    while (r > q[i].r) {
      ma[now[r]]--;
      ans -= ma[pre[r]];
      r--;
    }
    Ans[q[i].id] = ans;
  }
  for (int i = 1; i < (Q + 1); i++) printf("%I64d\n", Ans[i]);
}
