bzoj4836-分治FFT

题意

$$x \ opt \ y = \begin{cases}
x+y&, &x<y \\
x-y&,&x≥y
\end{cases}$$

现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问。每次询问给定一个数字 c

你需要求出有多少对 (i, j) 使得 $a_i\ opt\ b_j=c$

分析

对于 x+y 的操作,有可能会算到 $x \leq y$的情况,所以我们选择分治 $x \in [l,mid]$ 的情况和$y \in (mid,r]$ 会有贡献

对于x-y的操作,则不会算到x < y的情况, 所以算一遍就可以啦

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 5e4 + 5;

namespace fft {
typedef double db;

struct cp {
db x, y;
cp() { x = y = 0; }
cp(db x, db y) : x(x), y(y) {}
};

inline cp operator+(cp a, cp b) { return cp(a.x + b.x, a.y + b.y); }
inline cp operator-(cp a, cp b) { return cp(a.x - b.x, a.y - b.y); }
inline cp operator*(cp a, cp b) {
return cp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline cp conj(cp a) { return cp(a.x, -a.y); }

int base = 1;
vector<cp> roots;
vector<int> rev;

const db PI = acosl(-1.0);
void init() {
roots.pb(cp(0, 0));
roots.pb(cp(1, 0));
rev.pb(0);
rev.pb(1);
}

void ensure_base(int nbase) {
if (nbase <= base) return;
rev.resize(static_cast<unsigned long>(1 << nbase));
for (int i = 0; i < (1 << nbase); i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
roots.resize(static_cast<unsigned long>(1 << nbase));
while (base < nbase) {
db angle = 2 * PI / (1 << (base + 1));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
roots[i << 1] = roots[i];
db angle_i = angle * (2 * i + 1 - (1 << base));
roots[(i << 1) + 1] = cp(cos(angle_i), sin(angle_i));
}
base++;
}
}

void fft(vector<cp>& a, int n = -1) {
if (n == -1) n = a.size();
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++)
if (i < (rev[i] >> shift)) swap(a[i], a[rev[i] >> shift]);
for (int k = 1; k < n; k <<= 1)
for (int i = 0; i < n; i += 2 * k)
for (int j = 0; j < k; j++) {
cp z = a[i + j + k] * roots[j + k];
a[i + j + k] = a[i + j] - z;
a[i + j] = a[i + j] + z;
}
}

vector<cp> fa, fb;

vector<ll> multiply(vector<ll>& a, vector<ll>& b) {
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
if (sz > (int)fa.size()) fa.resize(static_cast<unsigned long>(sz));
for (int i = 0; i < sz; i++) {
ll x = (i < (int)a.size() ? a[i] : 0);
ll y = (i < (int)b.size() ? b[i] : 0);
fa[i] = cp(x, y);
}
fft(fa, sz);
cp r(0, -0.25 / sz);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
cp z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
if (i != j) {
fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
}
fa[i] = z;
}
fft(fa, sz);
vector<ll> res(static_cast<unsigned long>(need));
for (int i = 0; i < need; i++) {
res[i] = fa[i].x + 0.5;
}
return res;
}
}; // namespace fft
ll a[maxn], b[maxn], c[maxn << 3];
const int offset = maxn;
void solve(int l, int r) {
if (l == r) {
c[0] += a[l] * b[r];
return;
}
int m = l + r >> 1;
vector<ll> s1(r - l + 1), s2(r - l + 1);
for (int i = 0; i <= m - l; i++) s1[i] = a[i + l];
for (int i = 1; i <= r - m; i++) s2[i] = b[i + m];
vector<ll> ret = fft::multiply(s1, s2);
for (int i = 1; i <= r - l; i++) c[i + l + m + offset] += ret[i];
solve(l, m);
solve(m + 1, r);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
fft::init();
int T;
scanf("%d", &T);
while (T--) {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
clr(c, 0);
clr(a, 0);
clr(b, 0);
for (int i = 0, x; i < n; i++) {
scanf("%d", &x);
a[x]++;
}
for (int i = 0, x; i < m; i++) {
scanf("%d", &x);
b[x]++;
}
vector<ll> s1(maxn), s2(maxn + offset);
for (int i = 0; i < maxn; i++) s1[i] = a[i];
for (int i = 0; i < maxn; i++) s2[offset - i] = b[i];
vector<ll> ret = fft::multiply(s1, s2);
for (int i = 0; i < maxn; i++) c[i + offset] += ret[i + offset];
solve(0, 50000);
for (int i = 0, x; i < q; i++) {
scanf("%d", &x);
printf("%lld\n", c[x + offset]);
}
}
}
Thank you for your support!