#include <bits/stdc++.h>
using namespace std;
struct WindowContainer {
  vector<long long> V;
  vector<int> NEXT, PREV, ID;
  vector<int> FREC;
  int K;
  int L = 0, R = -1;
  long long cnt = 0;
  WindowContainer(vector<long long> V, int K) : V(V), K(K), FREC(V.size(), 0) {
    customBuild();
  }
  void customBuild() {
    vector<long long> C;
    NEXT = vector<int>(V.size(), -1);
    PREV = vector<int>(V.size(), -1);
    ID = vector<int>(V.size(), -1);
    for (int i = 0; i < V.size(); ++i) {
      C.push_back(V[i]);
    }
    sort(C.begin(), C.end());
    C.erase(unique(C.begin(), C.end()), C.end());
    for (int i = 0; i < V.size(); ++i) {
      int p = lower_bound(C.begin(), C.end(), V[i]) - C.begin();
      int pn = lower_bound(C.begin(), C.end(), V[i] + K) - C.begin();
      int pp = lower_bound(C.begin(), C.end(), V[i] - K) - C.begin();
      ID[i] = p;
      if (pn < C.size() && C[pn] == V[i] + K) {
        NEXT[i] = pn;
      } else {
        pn = -1;
      }
      if (pp < C.size() && C[pp] == V[i] - K) {
        PREV[i] = pp;
      } else {
        pp = -1;
      }
    }
  }
  void adjust(int i, int j) {
    while (R < j) add(++R, 0);
    while (L > i) add(--L, 1);
    while (R > j) rem(R--, 0);
    while (L < i) rem(L++, 1);
  }
  void add(int i, int left) {
    if (left && NEXT[i] != -1) {
      cnt += FREC[NEXT[i]];
    }
    if (!left && PREV[i] != -1) {
      cnt += FREC[PREV[i]];
    }
    FREC[ID[i]]++;
  }
  void rem(int i, int left) {
    FREC[ID[i]]--;
    if (left && NEXT[i] != -1) {
      cnt -= FREC[NEXT[i]];
    }
    if (!left && PREV[i] != -1) {
      cnt -= FREC[PREV[i]];
    }
  }
};
const int BSIZE = 300;
struct Query {
  int b, e;
  long long *ans;
  bool operator<(const Query &q) const {
    return b / BSIZE != q.b / BSIZE ? b / BSIZE < q.b / BSIZE : e < q.e;
  }
};
int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  vector<long long> V(N);
  for (int i = 0; i < N; ++i) {
    cin >> V[i];
  }
  for (int i = 0; i < N; ++i) {
    int n;
    cin >> n;
    if (V[i] == 1)
      V[i] = n;
    else
      V[i] = -n;
  }
  vector<long long> A(N + 1);
  for (int i = 1; i <= N; ++i) {
    A[i] = A[i - 1] + V[i - 1];
  }
  WindowContainer WC(A, K);
  int Q;
  cin >> Q;
  vector<Query> QUE(Q);
  vector<long long> ANS;
  ANS.reserve(Q);
  for (Query &q : QUE) {
    cin >> q.b >> q.e;
    q.b--;
    ANS.push_back(0);
    q.ans = &ANS.back();
  }
  sort(QUE.begin(), QUE.end());
  for (Query q : QUE) {
    WC.adjust(q.b, q.e);
    *q.ans = WC.cnt;
  }
  for (long long an : ANS) {
    cout << an << '\n';
  }
  return 0;
}
