BZOJ4836: [Lydsy1704月赛]二元运算【分治FFT】【卡常(没卡过)】
Description
定义二元运算 opt 满足
现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问。每次询问给定一个数字 c
你需要求出有多少对 (i, j) 使得 a_i opt b_j=c 。
Input
第一行是一个整数 T (1≤T≤10) ,表示测试数据的组数。
对于每组测试数据:
第一行是三个整数 n,m,q (1≤n,m,q≤50000) 。
第二行是 n 个整数,表示 a_1,a_2,?,a_n (0≤a_1,a_2,?,a_n≤50000) 。
第三行是 m 个整数,表示 b_1,b_2,?,b_m (0≤b_1,b_2,?,b_m≤50000) 。
第四行是 q 个整数,第 i 个整数 c_i (0≤c_i≤100000) 表示第 i 次查询的数。
Output
对于每次查询,输出一行,包含一个整数,表示满足条件的 (i, j) 对的个数。
Sample Input
2
2 1 5
1 3
2
1 2 3 4 5
2 2 5
1 3
2 4
1 2 3 4 5
Sample Output
1
0
1
0
0
1
0
1
0
1
思路
在上面两种情况中,减法可以把y取反直接fft,然后加法因为有限制可以在值域上进行分治fft
然后其实很板子的。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace io {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
char obuf[BUFSIZE], *os = obuf, *ot = obuf + BUFSIZE - 1;
char read_char() {
if (is == it)
it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return *is++;
}
int read_int() {
int x = 0, f = 1;
char c = read_char();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = read_char();
}
while (isdigit(c)) x = x * 10 + c - '0', c = read_char();
return x * f;
}
ll read_ll() {
ll x = 0, f = 1;
char c = read_char();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = read_char();
}
while (isdigit(c)) x = x * 10 + c - '0', c = read_char();
return x * f;
}
void read_string(char* s) {
char c = read_char();
while (isspace(c)) c = read_char();
while (!isspace(c)) *s++ = c, c = read_char();
*s = 0;
}
void flush() {
fwrite(obuf, 1, os - obuf, stdout);
os = obuf;
}
void print_char(char c) {
*os++ = c;
if (os == ot) flush();
}
void print_int(int x) {
static char q[20];
if (!x) print_char('0');
else {
if (x < 0) print_char('-'), x = -x;
int top = 0;
while (x) q[top++] = x % 10 + '0', x /= 10;
while (top--) print_char(q[top]);
}
}
void print_ll(ll x) {
static char q[20];
if (!x) print_char('0');
else {
if (x < 0) print_char('-'), x = -x;
int top = 0;
while (x) q[top++] = x % 10 + '0', x /= 10;
while (top--) print_char(q[top]);
}
}
struct flusher_t {
~flusher_t() {
flush();
}
} flusher;
};
using namespace io;
const int N = 3e5 + 10;
const int M = 5e4 + 10;
const double eps = 1e-6;
const double PI = acos(-1);
typedef complex<double> Complex;
typedef vector<Complex> Poly;
Complex w[2][N];
void init() {
for (int i = 1; i < (1 << 18); i <<= 1) {
w[0][i] = w[1][i] = Complex(1, 0);
Complex wn(cos(PI / i), sin(PI / i));
for (int j = 1; j < i; j++)
w[1][i + j] = w[1][i + j - 1] * wn;
wn = Complex(cos(PI / i), -sin(PI / i));
for (int j = 1; j < i; j++)
w[0][i + j] = w[0][i + j - 1] * wn;
}
}
void transform(Complex *t, int len, int typ) {
for (int i = 0, j = 0, k; j < len; j++) {
if (i > j) swap(t[i], t[j]);
for (k = (len >> 1); k & i; k >>= 1) i ^= k;
i ^= k;
}
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += (i << 1)) {
for (int k = 0; k < i; k++) {
Complex x = t[j + k], y = w[typ][i + k] * t[j + k + i];
t[j + k] = x + y;
t[j + k + i] = x - y;
}
}
}
if (typ) return;
for (int i = 0; i < len; i++)
t[i] = Complex(t[i].real() / (double) len, t[i].imag());
}
bool equ0(double x) {
return fabs(x) < eps;
}
Poly mul(const Poly a, const Poly b) {
static Poly cura, curb;
cura = a, curb = b;
int len = 1 << (int) ceil(log2(cura.size() + curb.size() - 1));
cura.resize(len), curb.resize(len);
transform(&cura[0], len, 1);
transform(&curb[0], len, 1);
for (int i = 0; i < len; i++)
cura[i] *= curb[i];
transform(&cura[0], len, 0);
return cura;
}
Poly ansadd(N, 0), anssub(N, 0);
int n, m, q, maxa, maxb;
int a[N], b[N];
int cnta[N], cntb[N];
void devide(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
devide(l, mid);
devide(mid + 1, r);
Poly x, y;
int sizl = mid - l + 1, sizr = r - mid;
x.resize(sizl);
y.resize(sizr);
for (int i = 0; i < sizl; i++)
x[i] = cnta[i + l];
for (int i = 0; i < sizr; i++)
y[i] = cntb[i + mid + 1];
x = mul(x, y);
for (int i = 0; i < (signed) x.size(); i++)
ansadd[i + l + mid + 1] += x[i];
}
void calcadd() {
devide(0, max(maxa, maxb));
}
void calcsub() {
static Poly x, y;
x.resize(maxb + maxa + 1);
y.resize(maxb + maxa + 1);
for (int i = 0; i <= maxb + maxa; i++)
x[i] = y[i] = 0;
for (int i = 0; i <= maxa; i++)
x[maxb + i] = cnta[i];
for (int i = 0; i <= maxb; i++)
y[maxb - i] = cntb[i];
x = mul(x, y);
for (int i = maxb * 2; i < (signed) x.size(); i++)
anssub[i - maxb * 2] = x[i];
}
void solve() {
n = read_int(), m = read_int(), q = read_int();
maxa = maxb = 0;
for (int i = 1; i <= n; i++) {
cnta[a[i] = read_int()]++;
maxa = max(maxa, a[i]);
}
for (int i = 1; i <= m; i++) {
cntb[b[i] = read_int()]++;
maxb = max(maxb, b[i]);
}
calcsub();
calcadd();
while (q--) {
int c = read_int();
print_ll((ll) round(ansadd[c].real()) + (ll) round(anssub[c].real()));
print_char('\n');
}
for (int i = 0; i <= maxa; i++)
cnta[i] = 0;
for (int i = 0; i <= maxb; i++)
cntb[i] = 0;
for (int i = 0; i <= maxa + maxb; i++)
ansadd[i] = anssub[i] = 0;
}
int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
init();
int T = read_int();
while (T--) solve();
return 0;
}
BZOJ4836: [Lydsy1704月赛]二元运算【分治FFT】【卡常(没卡过)】的更多相关文章
- bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数
[Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 577 Solved: 201[Submit][Status][Di ...
- BZOJ4836 [Lydsy1704月赛]二元运算 分治 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8830036.html 题目传送门 - BZOJ4836 题意 定义二元运算$opt$满足 $$x\ opt\ y ...
- BZOJ 4836: [Lydsy1704月赛]二元运算 分治FFT
Code: #include<bits/stdc++.h> #define ll long long #define maxn 500000 #define setIO(s) freope ...
- BZOJ4836: [Lydsy1704月赛]二元运算
BZOJ4836: [Lydsy1704月赛]二元运算 https://lydsy.com/JudgeOnline/problem.php?id=4836 分析: 分开做,维护两个桶. 分治每次求\( ...
- 【bzoj4836】[Lydsy2017年4月月赛]二元运算 分治+FFT
题目描述 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使得 a_ ...
- bzoj 4836: [Lydsy2017年4月月赛]二元运算 -- 分治+FFT
4836: [Lydsy2017年4月月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MB Description 定义二元运算 opt 满足 现在给定一 ...
- [BZOJ4836]二元运算(分治FFT)
4836: [Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 578 Solved: 202[Submit][Stat ...
- 【bzoj4836】二元运算 分治FFT
Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使 ...
- LOJ575. 「LibreOJ NOI Round #2」不等关系 [容斥,分治FFT]
LOJ 思路 发现既有大于又有小于比较难办,使用容斥,把大于改成任意减去小于的. 于是最后的串就长成这样:<<?<?<??<<<?<.我们把一段连续的& ...
随机推荐
- steam
1.steam 教育 Science(科学), Technology(技术), Engineering(工程), Arts(艺术), Maths(数学) 2. steam 平台 Steam英文原译为 ...
- 解决dos窗口乱码问题
大家有没有遇到这样的情况,看着就糟心 打开dos窗口, 输入命令 chcp 936 (936表示中文编码GBK, 也可以设置其他编码), 回车一下执行. 鼠标右键 -> 属性 (关键一步): ...
- Java 写数据到文件
private boolean writeToFile(BusGpsBean gpsBean) { String dataStr = DateUtil.date2String(new Date(), ...
- LeetCode--110--平衡二叉树
问题描述: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,n ...
- JS-Object (3) JSON; Event Object相关知识(事件冒泡,事件监听, stopPropagation()
通常用于在网站上表示和传输数据 使用JavaScript处理JSON的所有工作,包括访问JSON对象中的数据项并编写自己的JSON. JSON text基本上就像是一个JavaScript对象,这句话 ...
- 『OpenCV3』Harris角点特征_API调用及python手动实现
一.OpenCV接口调用示意 介绍了OpenCV3中提取图像角点特征的函数: # coding=utf- import cv2 import numpy as np '''Harris算法角点特征提取 ...
- nyoj737区间dp(石子合并)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的 ...
- 计时(.NET)
using System.Diagnostics; // 开始计时 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 要计时的操 ...
- HeaderExchangeClient
HeaderExchangeClient 注释是DefaultMessageClient,类中定义了心跳定时器HeaderExchangeChannel 发送请求HeaderExchangeHandl ...
- The Architecture of Open Source Applications——阅读笔记part 1
Architects look at thousands of buildings during their training, and study critiques of those buildi ...