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 ...
随机推荐
- Python 条件判断 循环
age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的 ...
- java中this的用法?
2008-07-28 08:10cztx5479 | 分类:JAVA相关 | 浏览4533次 java中this的用法? import java.awt.*; import java.awt.even ...
- ognl表达式root中取值顺序
不加#,先从栈顶取,如果没有(是没有这个属性而不是这个属性没有值),再往下取. 如果栈顶和非栈顶的对象拥有同一个属性名称,想直接取非栈顶的属性可以在ognl中用#root[i].属性名,可以取到属性的 ...
- poj1125(Floyd最短路)
//Accepted 164 KB 0 ms //floyd #include <cstdio> #include <cstring> #include <iostrea ...
- 如何从SAP中查找BADI
如何从SAP中查找BADI 如何从SAP中查找BADI http://blog.csdn.net/CompassButton/article/details/1231652 BADI作为SAP的第 ...
- hdu 2052
PS:竟然一次AC....惊喜...注意每个实例后跟一个空行.. 学到一个快速清空数组的用法...memst函数. memst(void *s,char a,sizeof n) 把 s里面的前n个 ...
- IOS使用Asyncsocket进行socket编程
iphone的标准推荐CFNetwork C库编程.但是编程比较烦躁.在其它OS往往用类来封装的对Socket函数的处理.比如MFC的CAsysncSocket.在iphone也有类似于开源项目.co ...
- 对NSNumber的理解
1.nsnumber最重要的作用是可以封装任何的值对象,就是说nsnumber对象的类型可以是任何的类型. 如nsnumber *number = @"12" nsnumber * ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...
- VBS_For Each...Next
For Each...Next 循环与 For...Next 循环类似.For Each...Next 不是将语句运行指定的次数,而是对于数组中的每个元素或对象集合中的每一项重复一组语句.这在不知道集 ...