#include <bits/stdc++.h>
using namespace std;
const int sz = 317;
int t[100002], a[100002];
int l[100002], r[100002];
long long sum[2][100002];
long long ans[100002];
struct node {
  int b, r, i;
};
bool cmp(node a, node b) {
  if (a.b != b.b) return a.b < b.b;
  return a.r < b.r;
}
int n, k, q;
long long res = 0;
node block[100002];
int val[3][100002];
int dp[300006];
void add1(int i) {
  res += dp[val[1][i]];
  dp[val[0][i]]++;
}
void del1(int i) {
  dp[val[0][i]]--;
  res -= dp[val[1][i]];
}
void add2(int i) {
  res += dp[val[2][i]];
  dp[val[0][i]]++;
}
void del2(int i) {
  dp[val[0][i]]--;
  res -= dp[val[2][i]];
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &t[i]), t[i]--;
  vector<long long> v;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum[0][i] = sum[0][i - 1];
    sum[1][i] = sum[1][i - 1];
    sum[t[i]][i] += a[i];
    v.emplace_back(sum[0][i] - sum[1][i]);
    v.emplace_back(sum[0][i] - sum[1][i] - k);
    v.emplace_back(sum[0][i] - sum[1][i] + k);
  }
  v.emplace_back(0);
  v.emplace_back(-k);
  v.emplace_back(+k);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i <= n; i++) {
    val[0][i] =
        lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i]) - v.begin() + 1;
    val[1][i] = lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i] - k) -
                v.begin() + 1;
    val[2][i] = lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i] + k) -
                v.begin() + 1;
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l[i], &r[i]);
    l[i]--;
    block[i] = {l[i] / sz, r[i], i};
  }
  sort(block + 1, block + 1 + q, cmp);
  int curl = 0, curr = 0;
  dp[val[0][0]]++;
  for (int i = 1; i <= q; i++) {
    int idx = block[i].i;
    int ll = l[idx], rr = r[idx];
    while (curl < ll) del2(curl++);
    while (curl > ll) add2(--curl);
    while (curr < rr) add1(++curr);
    while (curr > rr) del1(curr--);
    ans[idx] = res;
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
