#include <bits/stdc++.h>
using namespace std;
int t[100005];
int a[100005];
struct Query {
  int l;
  int r;
  int id;
  long long sol;
};
Query qu[100005];
long long ps[100005];
int cnt[300005];
int block;
bool cmp1(Query x, Query y) {
  if (x.l / block != y.l / block) {
    return x.l < y.l;
  }
  return x.r < y.r;
}
bool cmp2(Query x, Query y) { return x.id < y.id; }
int code[100005][5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> t[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (t[i] == 2) {
      a[i] *= -1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    ps[i] = ps[i - 1] + a[i];
  }
  vector<pair<long long, pair<int, int>>> vk;
  for (int i = 0; i <= n; ++i) {
    vk.push_back({ps[i], {i, 0}});
    vk.push_back({ps[i] + k, {i, 1}});
    vk.push_back({ps[i] - k, {i, 2}});
  }
  sort(vk.begin(), vk.end());
  int nInd = 0;
  int strt = 0;
  for (int i = 0; i < (int)vk.size(); ++i) {
    auto x = vk[i];
    if (!i || vk[i].first != vk[i - 1].first) ++nInd;
    if (x.first == 0) strt = nInd;
    code[x.second.first][x.second.second] = nInd;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> qu[i].l >> qu[i].r;
    qu[i].l--;
    qu[i].id = i;
  }
  block = sqrt(n);
  sort(qu, qu + q, cmp1);
  int l = 0;
  int r = 0;
  long long ans = 0;
  cnt[strt] = 1;
  for (int z = 0; z < q; ++z) {
    int x = qu[z].l;
    int y = qu[z].r;
    while (l < x) {
      cnt[code[l][0]]--;
      ans -= cnt[code[l][1]];
      l++;
    }
    while (l > x) {
      l--;
      ans += cnt[code[l][1]];
      cnt[code[l][0]]++;
    }
    while (r < y) {
      ++r;
      ans += cnt[code[r][2]];
      cnt[code[r][0]]++;
    }
    while (r > y) {
      cnt[code[r][0]]--;
      ans -= cnt[code[r][2]];
      --r;
    }
    qu[z].sol = ans;
  }
  sort(qu, qu + q, cmp2);
  for (int i = 0; i < q; ++i) {
    cout << qu[i].sol << "\n";
  }
  return 0;
}
