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
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
const int lim = (1 << 18), len = 18; const int N = lim; const double pi = acos(-1.0); const int maxn = N + 20;
struct Complex { double x, y; Complex(double a = 0, double b = 0) : x(a), y(b) {} Complex operator + (const Complex &z) const { return Complex(x + z.x, y + z.y); } Complex operator - (const Complex &z) const { return Complex(x - z.x, y - z.y); } Complex operator * (const Complex &z) const { return Complex(x * z.x - y * z.y, x * z.y + y * z.x); } Complex & operator += (const Complex &z) { return *this = *this + z, *this; } Complex & operator -= (const Complex &z) { return *this = *this - z, *this; } Complex & operator *= (const Complex &z) { return *this = *this * z, *this; } }tmp[maxn];
Complex mnA[maxn], mnB[maxn];
Complex mxA[maxn], mxB[maxn];
Complex nA[maxn], nB[maxn];
int rev[maxn];
void FFT(Complex *A,int opt) { for(int i = 0; i < lim; ++ i) if(i < rev[i]) swap(A[i], A[rev[i]]); for(int mid = 1; mid < lim; mid <<= 1) { Complex wn(cos(pi / mid), opt * sin(pi / mid)); for(int j = 0, c = (mid << 1); j < lim; j += c) { Complex W(1, 0); for(int k = 0; k < mid; ++ k, W *= wn) { Complex x = A[j + k], y = W * A[j + k + mid]; A[j + k] = x + y; A[j + k + mid] = x - y; } } } if(opt == -1) for(int i = 0; i < lim; ++ i) A[i].x /= lim; }
int n, m, Q; typedef long long ll; int a[maxn], b[maxn];
void Mul(ll *ans,Complex *A, Complex *B) { for(int i = 0; i < lim; ++ i) tmp[i] = A[i] * B[i]; FFT(tmp, -1); for(int i = 0; i < lim; ++ i) ans[i] += round(tmp[i].x); }
ll V[maxn << 1], mxE[maxn << 1], mnE[maxn << 1], mxD[maxn << 1], mnD[maxn << 1];
signed main() {
int i, j; for(i = 0; i < lim; ++ i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1)); r1(n, m, Q); for(i = 1; i <= n; ++ i) { r1(a[i]); nA[a[i]].x ++; if(i > 1) mnA[min(a[i - 1], a[i])].x ++, mxA[max(a[i - 1], a[i])].x ++; } for(i = 1; i <= m; ++ i) { r1(b[i]); nB[b[i]].x ++; if(i > 1) mnB[min(b[i - 1], b[i])].x ++, mxB[max(b[i - 1], b[i])].x ++; } FFT(nA, 1), FFT(nB, 1); FFT(mnA, 1), FFT(mxA, 1); FFT(mnB, 1), FFT(mxB, 1);
Mul(V, nA, nB); for(i = 1; i <= N; ++ i) V[i] += V[i - 1];
Mul(mxE, mnA, nB); Mul(mxE, nA, mnB); for(i = N; i >= 1; -- i) mxE[i] += mxE[i + 1];
Mul(mnE, nA, mxB); Mul(mnE, mxA, nB); for(i = 1; i <= N; ++ i) mnE[i] += mnE[i - 1];
Mul(mxD, mnA, mnB); for(i = N; i >= 1; -- i) mxD[i] += mxD[i + 1];
Mul(mnD, mxA, mxB); for(i = 1; i <= N; ++ i) mnD[i] += mnD[i - 1];
while(Q --) { int x; r1(x); long long ans(0); ans = 1ll * n * m - 2 * V[x - 1] + mnE[x - 1] - mxE[x] + mxD[x] - mnD[x - 1]; printf("%lld\n", ans); }
return 0; }
|