#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1001000;
const int HASH = 2333333;
int maxxxx = 0;
struct HASHMAP {
  int head[HASH], next[MAXN], ip;
  long long num[MAXN];
  long long state[MAXN];
  void init() {
    memset(head, -1, sizeof(head));
    ip = 0;
  }
  long long check(long long val, int h) {
    for (int i = head[h]; i != -1; i = next[i])
      if (state[i] == val) return num[i];
    return 0;
  }
  void insert(long long val, int h, long long x) {
    for (int i = head[h]; i != -1; i = next[i]) {
      if (val == state[i]) {
        num[i] += x;
        return;
      }
    }
    num[ip] = x;
    state[ip] = val, next[ip] = head[h], head[h] = ip++;
  }
} h1;
const int N = 1e5 + 100;
const int B = 333;
struct node {
  int l, r, id;
  bool operator<(const node& p) const {
    if (l / B == p.l / B) return r < p.r;
    return l < p.l;
  }
};
node pt[N];
long long ans[N];
long long now = 0;
long long num[N];
int ha[3][N];
int n, q, k;
void Move(int x, int op) {
  if (op == 1) {
    h1.insert(num[x], ha[0][x], 1);
    now += h1.check(num[x] - k, ha[1][x]);
    if (k == 0) now--;
  } else if (op == 2) {
    now -= h1.check(num[x] - k, ha[1][x]);
    h1.insert(num[x], ha[0][x], -1);
    if (k == 0) now++;
  } else if (op == 3) {
    h1.insert(num[x - 1], ha[0][x - 1], 1);
    now += h1.check(num[x - 1] + k, ha[2][x - 1]);
    if (k == 0) now--;
  } else {
    now -= h1.check(num[x - 1] + k, ha[2][x - 1]);
    h1.insert(num[x - 1], ha[0][x - 1], -1);
    if (k == 0) now++;
  }
}
int main() {
  h1.init();
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &num[i]);
  for (int i = 1; i <= n; i++) {
    if (num[i] == 2) num[i] = -1;
    int now;
    scanf("%d", &now);
    num[i] *= now;
  }
  for (int i = 1; i <= n; i++) num[i] += num[i - 1];
  for (int i = 0; i <= n; i++) {
    ha[0][i] = (num[i] % HASH + HASH) % HASH;
    ha[1][i] = ((num[i] - k) % HASH + HASH) % HASH;
    ha[2][i] = ((num[i] + k) % HASH + HASH) % HASH;
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &pt[i].l, &pt[i].r);
    pt[i].id = i;
  }
  sort(pt + 1, pt + 1 + q);
  int L = 1, R = 1;
  h1.insert(0, ha[0][0], 1);
  h1.insert(num[1], ha[0][1], 1);
  if (num[1] == k) now++;
  for (int i = 1; i <= q; i++) {
    while (R < pt[i].r) Move(++R, 1);
    while (R > pt[i].r) Move(R--, 2);
    while (L > pt[i].l) Move(--L, 3);
    while (L < pt[i].l) Move(L++, 4);
    ans[pt[i].id] = now;
  }
  for (int i = 1; i <= q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
