Quailty and Binary Operation
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的更多相关文章
- [玲珑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 ...
- Data structure alignment by binary operation
在寫C的過程中,我們會很自然地以為,我連續宣告一堆大小不一的char array. 經過Complier之後這些char array未必是連續擺放.至於為什麼就要談到我們今天的主角了alignment ...
- uestc 1709 Binary Operations 位运算的灵活运用
Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674 Description B ...
- matlab进阶:常用功能的实现,常用函数的说明
常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...
- reduce方法
API里面这样写 reduce(initial, sym) → obj reduce(初始值,符号) reduce(sym) → obj re ...
- Scalaz(8)- typeclass:Monoid and Foldable
Monoid是种最简单的typeclass类型.我们先看看scalaz的Monoid typeclass定义:scalaz/Monoid.scala trait Monoid[F] extends S ...
- 我们的动机(Our motivation)
我们的动机(Our motivation) There are many PHP frameworks nowadays, but none of them is like Phalcon (Real ...
- bzoj2548[Cstc2002]灭鼠行动
Description 最近,有一些繁殖力很强的老鼠在下水道非常猖獗,灭鼠特工队正在计划消灭这些老鼠.下水道只有东西方向和南北方向的管道,如图所示. 灭鼠特工队的队员拥有强大的武器.他们将在某些时刻t ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
随机推荐
- C++-bool的值
/////////////////////////////////////////////////////////////////////////////// // // FileName : boo ...
- Unity开发Android应用程序:调用安卓应用程序功能
开发环境: Eclipse3.4 + adt12 + jdk6 + AndroidSDK2.2 Unity3.4 + windows7 测试设备: HTC Desire HD 本文要涉及到的几个重点问 ...
- 系统的 host文件的作用
有些用户可能已经注意到,我们在上网时除了可使用常规的 http://www.xxx.com或http://www.xxx.com.cn等形式的网站域名之外,还可以使用类似于“202.106.184.2 ...
- Ubuntu 14.10 下安装navicat
1 下载navicat,网址http://www.navicat.com.cn/download,我下载的是navicat111_premium_cs.tar.gz 2 解压到合适的位置 3 进入解压 ...
- UITableView错误 ‘unable to dequeue a cell with identifier Cell'
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequeueReusableCellWithIdentif ...
- 属性的定义以及@synthesize的使用
1.属性通常是指某些由对象封装或储存的数据.它可以是标志(如名称或颜色),也可以是与一个或多个其他对象的关系. 2.属性的基本声明使用 @property 编译器指令,后面紧跟属性的类型信息和名称.您 ...
- 数据结构《13》----二叉树 Morris 前序遍历
三种二叉树的后序遍历的方法: 1. 递归 O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈) O(n) 时间复杂度, O(n) 空间 ...
- ie7下 滚动条内容不动问题
ie7+ 版式正常 ie7滚动内容不跟着动 解决方法 加上 overflow-x: hidden; overflow-y: auto; *position:relative; *le ...
- C语言基础:进制转换,变量,常量,表达式,基本数据类型,输出函数,输入函数,运算符. 分类: iOS学习 c语言基础 2015-06-10 21:39 25人阅读 评论(0) 收藏
二进制:以0b开头,只有0和1两种数字.如0101 十进制:0~9十个数字表示.如25 十六进制:以0~9,A~F表示,以0X开头.如0X2B 十进制转换为X进制:连除倒取余 X进制转换为十进制:按权 ...
- # 20145210 《Java程序设计》第05周学习总结
教材学习内容总结 第八章 异常处理 8.1语法与继承架构 •使用 try.catch •Java中所有信息都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理 • ...