Quailty and Binary Operation

题意

  • 分别给\(N,M(N,M \le 50000)\)两个数组\(A\)和\(B\),满足\(0 \le A_i,B_i \le 50000\)。
  • 有\(Q(Q \le 50000)\)次询问,每次求\(a_i \ opt\ b_j = c\)的对数\((i,j)\)。
  • \[x\ opt\ y = \begin{cases} x+y,\ if\ x<y, \\ x-y,\ otherwise. \end{cases}
    \]

思路

  • 分治+FFT

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<time.h>
#include<stdlib.h>
#include<assert.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) ((int)(x).size())
#define rep(i,l,r) for(int i=(l);i<(r);++i)
typedef long long ll;
typedef pair<int, int> pii;
const int N = (1 << 17) + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double Pi = acos(-1.0);
const double EPS = 1e-8;
//--------head--------
struct FastIO {
static const int S = 1310720;
int wpos;
char wbuf[S];
FastIO() :
wpos(0) {
}
inline int xchar() {
static char buf[S];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = 0;
while (c <= 32)
c = xchar();
for (; '0' <= c && c <= '9'; c = xchar())
x = x * 10 + c - '0';
return x;
}
inline int xint() {
int s = 1, c = xchar(), x = 0;
while (c <= 32)
c = xchar();
if (c == '-')
s = -1, c = xchar();
for (; '0' <= c && c <= '9'; c = xchar())
x = x * 10 + c - '0';
return x * s;
}
inline void xstring(char *s) {
int c = xchar();
while (c <= 32)
c = xchar();
for (; c > 32; c = xchar())
*s++ = c;
*s = 0;
}
inline void wchar(int x) {
if (wpos == S)
fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos++] = x;
}
inline void wint(int x) {
if (x < 0)
wchar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
wchar(s[n]);
}
inline void wstring(const char *s) {
while (*s)
wchar(*s++);
}
~FastIO() {
if (wpos)
fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
int n, m, q, a[N], b[N];
ll ans[N << 1];
int ca[N], cb[N];
struct C {
double r, i;
C() {
r = i = 0;
}
C(double _r, double _i) {
r = _r, i = _i;
}
C operator+(const C &p) const {
return C(r + p.r, i + p.i);
}
C operator-(const C &p) const {
return C(r - p.r, i - p.i);
}
C operator*(const C &p) const {
return C(r * p.r - i * p.i, r * p.i + i * p.r);
}
};
void fft(C x[], int n, int rev) {
int i, j, k, t;
for (i = 1; i < n; ++i) {
for (j = 0, k = n >> 1, t = i; k; k >>= 1, t >>= 1)
j = j << 1 | (t & 1);
if (i < j)
swap(x[i], x[j]);
}
int s, ds;
for (s = 2, ds = 1; s <= n; ds = s, s <<= 1) {
C w = C(1, 0), t;
C wn = C(cos(2.0 * rev * Pi / s), sin(2.0 * rev * Pi / s));
for (k = 0; k < ds; ++k, w = w * wn)
for (i = k; i < n; i += s) {
t = w * x[i + ds];
x[i + ds] = x[i] - t;
x[i] = x[i] + t;
}
}
if (rev == -1)
for (i = 0; i < n; ++i)
x[i].r /= n;
}
C A[N], B[N], R[N];
void solve(int l, int r) {
if (l > r) {
return ;
}
if(r - l <= 30){
for(int i=l;i<=r;++i){
for(int j=l;j<=i;++j)
ans[i-j] += 1ll * ca[i] * cb[j];
for(int j=i+1;j<=r;++j)
ans[i+j] += 1ll * ca[i] * cb[j];
}
return ;
}
int m = (l + r) >> 1, L = 1;
while (L < r - l + 1)
L <<= 1;
solve(l, m), solve(m + 1, r);
// x + y
rep(i, 0, L) {
A[i] = (l + i <= m ? C(ca[l + i], 0) : C(0, 0));
B[i] = (m + i + 1 <= r ? C(cb[m + 1 + i], 0) : C(0, 0));
}
fft(A, L, 1), fft(B, L, 1);
rep(i, 0, L)
R[i] = A[i] * B[i];
fft(R, L, -1);
rep(i, 0, L)
ans[i + l + m + 1] += (ll) (R[i].r + 0.5);
// x - y
rep(i, 0, L) {
A[i] = (m + 1 + i <= r ? C(ca[m + 1 + i], 0) : C(0, 0));
B[i] = (m - i >= l ? C(cb[m - i], 0) : C(0, 0));
}
fft(A, L, 1), fft(B, L, 1);
rep(i, 0, L)
R[i] = A[i] * B[i];
fft(R, L, -1);
rep(i, 0, L)
ans[i + 1] += (ll) (R[i].r + 0.5);
}
int main() {
int T = 10;
T = io.xuint();
rep(cas, 0, T) {
n = io.xuint();
m = io.xuint();
q = io.xuint();
// n = m = q = 50000;
// rep(i, 0, n) a[i] = i, ++ca[a[i]];
// rep(i, 0, m) b[i] = i, ++cb[b[i]];
rep(i, 0, n) a[i] = io.xuint(), ++ca[a[i]];
rep(i, 0, m) b[i] = io.xuint(), ++cb[b[i]];
int mn = min(*min_element(a, a + n), *min_element(b, b + m));
int mx = max(*max_element(a, a + n), *max_element(b, b + m));
// printf("mn = %d, mx = %d\n", mn, mx);
solve(mn, mx);
rep(_q, 0, q) {
int x;
x = io.xuint();
printf("%lld\n", ans[x]);
}
rep(i, 0, 1 + (mx << 1))
ans[i] = 0;
rep(i, 0, n)
ca[a[i]] = 0;
rep(i, 0, m)
cb[b[i]] = 0;
}
return 0;
}

Quailty and Binary Operation的更多相关文章

  1. [玲珑OJ1044] Quailty and Binary Operation (FFT+cdq分治)

    题目链接 题意:给定两个长度为n的数组a与长度为m的数组b, 给定一个操作符op满足 x op y = x < y ? x+y : x-y.  有q个询问,每次给出询问c,问:有多少对(i, j ...

  2. Data structure alignment by binary operation

    在寫C的過程中,我們會很自然地以為,我連續宣告一堆大小不一的char array. 經過Complier之後這些char array未必是連續擺放.至於為什麼就要談到我們今天的主角了alignment ...

  3. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  4. matlab进阶:常用功能的实现,常用函数的说明

    常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...

  5. reduce方法

    API里面这样写 reduce(initial, sym) → obj                              reduce(初始值,符号) reduce(sym) → obj re ...

  6. Scalaz(8)- typeclass:Monoid and Foldable

    Monoid是种最简单的typeclass类型.我们先看看scalaz的Monoid typeclass定义:scalaz/Monoid.scala trait Monoid[F] extends S ...

  7. 我们的动机(Our motivation)

    我们的动机(Our motivation) There are many PHP frameworks nowadays, but none of them is like Phalcon (Real ...

  8. bzoj2548[Cstc2002]灭鼠行动

    Description 最近,有一些繁殖力很强的老鼠在下水道非常猖獗,灭鼠特工队正在计划消灭这些老鼠.下水道只有东西方向和南北方向的管道,如图所示. 灭鼠特工队的队员拥有强大的武器.他们将在某些时刻t ...

  9. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

随机推荐

  1. Android ContentProvider的实现

    当Android中的应用需要访问其他应用的数据时,用ContentProvider可以很好的解决这个问题.今天介绍一下ContentProvider的用法. 首先开发ContentProvider有两 ...

  2. linux卸载挂载点显示device is busy

    在做umount设备时, device is busy是令人头痛的提示: [root@delphi /]# umount /dev/cdrom umount: /mnt/cdrom: device i ...

  3. ansible quick start

    1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...

  4. stm32 dac库函数解读

    1.简述: 12位数字输入,电压输出,DAC可以配置为8位或12位模式.有2个输出通道.在双DAC模式下,两个通道可以独立地工作. 特殊功能: 噪声波形生成,三角波形生成,外部触发转换,双DAC同时或 ...

  5. windows8.1 plsql连接oracle

    http://pan.baidu.com/share/link?shareid=3782452820&uk=3557941237 http://pan.baidu.com/share/link ...

  6. hdu 2047

    PS:又是上课偷懒..去递推.. 代码: #include "stdio.h"#include "math.h"long long dp[55];long lo ...

  7. 树莓派安装kali后的简单配置

    树莓派可以说是极客的最爱,可以根据不同的需求去做定制. 前文<使用树莓派和kali Linux打造便携式渗透套件>讲了一些使用树莓派的基础,主要侧重于将树莓派当作一个物理后门使用.我则更喜 ...

  8. http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

    http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

  9. js 微信分享

    一. //js接口 var shareme; var urls = window.location.href; if(isWeiXin()){   var weifileref=document.cr ...

  10. iOS常用设计模式:MVC、单例、代理、观察者。

    MVC 模型-视图-控制器(MVC)设计模式 MVC根据角色划分类,涉及到三个角色: Model:模型保存应用程序的数据. View:视图是模型的可视化表示以及用户交互的控件. Controller: ...