Problem:
Sum of Sequences

Problem

n個の要素を持つ数列A、m個の要素を持つ数列B、
それぞれが整数cからなるq個のクエリが与えられる。

各クエリについて、

数列Aの[la,ra]を全て足した数と数列Bの[lb,rb]を全て足した数の差の絶対値がcになるla,ra,lb,rb (0 ≤ la ≤ ra ≤ n−1, 0 ≤ lb ≤ rb ≤ m−1, 配列の番号は0番から始まる) の組み合わせの数を求めよ。

Input

n m q
a0 a1 ... an−1
b0 b1 ... bm−1
c0
c1
...
cq−1

入力は全て整数で与えられる。

1行目に数列の要素数nとm、クエリ数qが与えられる。

2行目に数列Aの要素、3行目に数列Bの要素が空白区切りで与えられる。

4行目からq行に各クエリの値ciが与えられる。

Constraints

1 ≤ n, m ≤ 4×104

1 ≤ q ≤ 105

1 ≤ ai, bi ≤ 5

0 ≤ ci ≤ 2×105

Output

出力はq行からなる。各クエリの組み合わせの数を順番に一行に出力せよ。

Sample Input 1

3 3 1
1 2 3
3 1 2
3

Sample Output 1

6

Sample Input 2

5 4 2
1 2 3 4 5
2 2 2 2
11
12

Sample Output 2

3
4

Solution in C++14 (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
/*
?????????????????¨?????? FFT ???????????????
n?¬??????????g(x) = \sum_{i=0}^n a_i x^i , h(x) = \sum_{i=0}^n b_i x^i ???????????????
f(x) = (g*h)(x) = \sum_{k=0}^{2n} \sum_{i=0}^{k} a_i b_{k-i} x^k
???O(n log n)??§?±??????????

?????????:
g(x)????????°??? vector<T> g
h(x)????????°??? vector<T> h
??¨????????¨???
f = multiply(g, h);
??§f(x)????????°vector<T> f????±??????????

verified by: http://atc001.contest.atcoder.jp/tasks/fft_c
(AtCoder Typical Contest 001 C)

complex, vector, algorithm ?????????????????????????????????????????????
*/
#include<complex>
#include<vector>
#include<algorithm>
using namespace std;
const double PI = 3.141592653589793;
/*
*
 * n?¬??????????f????????????????????¨????????????
 * is_inv = false => ?????????
 * is_inv = true  => ?????????
 * ?????????????????¨??????multiply????????§n??§?????£?????????
 */
using comp=complex<double>;
void fft(vector<comp> &f, int n, bool is_inv) {
	if (n == 1) return;
	if (n == 2) {
		comp zeta = comp(-1,0);
		comp pow_zeta = 1.0;

		comp f0(f[0]);
		comp f1(f[1]);

		f[0]=f0+f1;
		f[1]=f0-f1;
		return;
	}
	vector<comp> f0(n / 2), f1(n / 2);
	for (int i = 0; i < n / 2; i++) {
		f0[i] = f[2 * i];
		f1[i] = f[2 * i + 1];
	
	}
	fft(f0, n / 2, is_inv);
	fft(f1, n / 2, is_inv);
	comp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));
	if (is_inv) zeta = 1.0 / zeta;
	comp pow_zeta = 1.0;
	for (int i = 0; i < n; i++) {
		f[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];
		pow_zeta *= zeta;
	}
}

template<typename T>
vector<T> multiply(vector<T> &g, vector<T>& h) {
	int n = 1;
	int gs = g.size();
	int hs = h.size();
	int fs = gs + hs - 1;
	while (n <= fs + 1) n *= 2;
	vector<comp> gg(n), hh(n), ff(n);
	for (int i = 0; i < n; i++) {
		gg[i] = (i < gs) ? g[i] : 0;
		hh[i] = (i < hs) ? h[i] : 0;
	}
	fft(gg, n, false);
	fft(hh, n, false);
	for (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];
	fft(ff, n, true);
	
	vector<T> f(fs);
	for (int i = 0; i < fs; i++) {
		f[i] = (T)round(ff[i].real() / n);
	}
	
	return f;
	
}
const int asize = 2e5+100;
vector<long long int> get_mp(const vector<int>v) {
	vector<long long int>sums(v.size()+1);
	for (int i = 0; i < v.size(); ++i) {
		sums[i+1]=sums[i]+v[i];
	}

	vector<long long int>a(asize);
	vector<long long int>b(asize);
	for (auto sum : sums) {
		a[sum]++;
		b[asize-sum-1]++;
	}
	vector<long long int> c=multiply(a,b);
	vector<long long int>ans(asize);
	for (int i = asize; i < 2 * asize-1; ++i) {
		if (c[i]) {
			ans[i - asize+1] += c[i];
		}
	}
	return ans;
}
vector<long long int>solve(vector<long long int>a, vector<long long int>b) {
	reverse(a.begin(),a.end());

	vector<long long >c=multiply<long long int>(a,b);
	vector<long long int>ans(asize);
	ans[0]=c[asize-1];
	for (int i = 1; i < asize; ++i) {
		ans[i]=c[asize-1-i]+c[asize-1+i];
	}
	return ans; 
}

int main() {
	int N,M,Q;cin>>N>>M>>Q;
	vector<int>as(N),bs(M);
	for (int i = 0; i < N; ++i) {
		cin>>as[i];
	}
	for (int i = 0; i < M; ++i) {
		cin>>bs[i];
	}
	long long int aaa=static_cast<long long int>(N)*(N+1)/2;
	long long int bbb=static_cast<long long int>(M)*(M+1)/2;
	auto a_mp(get_mp(as));
	assert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));
	auto b_mp(get_mp(bs));
	assert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));
	auto ans(solve(a_mp,b_mp));
	assert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e9);
	while (Q--) {
		int n;cin>>n;
		cout<<ans[n]<<endl;
	}
	return 0;
}