#include <bits/stdc++.h>
using namespace std;
const int Max = 3e5 + 100;
long long data[Max], Hash[Max], swi[Max], las[Max], fir[Max], cnt[Max], ans;
int vis[Max], block, sum[Max];
struct node {
  int l, r, id;
  bool operator<(const node& s) const {
    return l / block == s.l / block ? r < s.r : l / block < s.l / block;
  }
} Q[Max];
void add1(long long x) {
  ans += sum[swi[x]];
  sum[fir[x]]++;
}
void add2(long long x) {
  ans += sum[cnt[x]];
  sum[fir[x]]++;
}
void del1(long long x) {
  sum[fir[x]]--;
  ans -= sum[cnt[x]];
}
void del2(long long x) {
  sum[fir[x]]--;
  ans -= sum[swi[x]];
}
int main() {
  long long n, k, mem = 0;
  scanf("%lld%lld", &n, &k);
  Hash[mem++] = 0;
  Hash[mem++] = k;
  Hash[mem++] = -k;
  block = sqrt(n);
  for (int a = 1; a <= n; a++) {
    scanf("%d", &vis[a]);
  }
  for (int a = 1; a <= n; a++) {
    scanf("%lld", &data[a]);
    if (vis[a] == 1)
      data[a] = data[a - 1] + data[a];
    else
      data[a] = data[a - 1] - data[a];
    Hash[mem++] = data[a];
    Hash[mem++] = data[a] - k;
    Hash[mem++] = data[a] + k;
  }
  sort(Hash, Hash + mem);
  mem = unique(Hash, Hash + mem) - Hash;
  for (int a = 0; a <= n; a++) {
    swi[a] = lower_bound(Hash, Hash + mem, data[a] - k) - Hash;
    fir[a] = lower_bound(Hash, Hash + mem, data[a]) - Hash;
    cnt[a] = lower_bound(Hash, Hash + mem, data[a] + k) - Hash;
  }
  int m;
  scanf("%d", &m);
  for (int a = 0; a < m; a++) scanf("%d%d", &Q[a].l, &Q[a].r), Q[a].id = a;
  sort(Q, Q + m);
  int le = 0, re = 0;
  for (int a = 0; a < m; a++) {
    int x = Q[a].l - 1, y = Q[a].r + 1;
    while (re < y) add1(re++);
    while (re > y) del2(--re);
    while (le < x) del1(le++);
    while (le > x) add2(--le);
    las[Q[a].id] = ans;
  }
  for (int a = 0; a < m; a++) printf("%lld\n", las[a]);
  return 0;
}
