#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int n, k, m;
int Belong[maxn], op[maxn];
struct node {
  int l, r, id;
  bool operator<(const node &A) const {
    if (Belong[l] != Belong[A.l]) return Belong[l] < Belong[A.l];
    return r < A.r;
  }
} Q[maxn];
long long Ans, ans[maxn];
long long sum[maxn];
vector<long long> V;
int getid(long long x) {
  return lower_bound(V.begin(), V.end(), x) - V.begin() + 1;
}
int cnt[maxn * 3], id[maxn * 3];
void addL(int i) {
  Ans += cnt[id[i + n + n + 2]];
  cnt[id[i]]++;
}
void delL(int i) {
  cnt[id[i]]--;
  Ans -= cnt[id[i + n + n + 2]];
}
void addR(int i) {
  Ans += cnt[id[i + n + 1]];
  cnt[id[i]]++;
}
void delR(int i) {
  cnt[id[i]]--;
  Ans -= cnt[id[i + n + 1]];
}
int main() {
  scanf("%d%d", &n, &k);
  int sz = sqrt(n + 0.5);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &op[i]);
    Belong[i] = i / sz;
  }
  V.push_back(0);
  V.push_back(k);
  V.push_back(-k);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &sum[i]);
    if (op[i] == 2) sum[i] = -sum[i];
    sum[i] += sum[i - 1];
    V.push_back(sum[i] + k);
    V.push_back(sum[i] - k);
    V.push_back(sum[i]);
  }
  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  for (int i = 0; i <= n; i++) {
    id[i] = getid(sum[i]);
    id[i + n + 1] = getid(sum[i] - k);
    id[i + n + n + 2] = getid(sum[i] + k);
  }
  scanf("%d", &m);
  cnt[getid(0)] = 1;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &Q[i].l, &Q[i].r);
    Q[i].id = i;
  }
  sort(Q, Q + m);
  int L = 1, R = 0;
  for (int i = 0; i < m; i++) {
    while (L < Q[i].l) {
      delL(L - 1);
      L++;
    }
    while (L > Q[i].l) {
      L--;
      addL(L - 1);
    }
    while (R < Q[i].r) {
      R++;
      addR(R);
    }
    while (R > Q[i].r) {
      delR(R);
      R--;
    }
    ans[Q[i].id] = Ans;
  }
  for (int i = 0; i < m; i++) printf("%I64d\n", ans[i]);
  return 0;
}
