bzoj 4836: [Lydsy2017年4月月赛]二元运算 -- 分治+FFT
4836: [Lydsy2017年4月月赛]二元运算
Time Limit: 8 Sec Memory Limit: 128 MB
Description
Input
Output
对于每次查询,输出一行,包含一个整数,表示满足条件的 (i, j) 对的个数。
Sample Input
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
0
1
0
0
1
0
1
0
1
HINT
Source
可能我的常数真的巨大。。。
转载一个姜大爷的题解http://blog.csdn.net/neither_nor/article/details/70889246
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 1000000007
#define ll long long
#define PI acos(-1)
#define M 400010
inline int rd()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
#define db double
struct cl{
db x,y;
cl(){}
cl(db _x,db _y){x=_x;y=_y;}
friend cl operator +(const cl &x,const cl &y){return cl(x.x+y.x,x.y+y.y);}
friend cl operator -(const cl &x,const cl &y){return cl(x.x-y.x,x.y-y.y);}
friend cl operator *(const cl &x,const cl &y){return cl(x.x*y.x-x.y*y.y,x.x*y.y+x.y*y.x);}
friend cl operator /(const cl &x,const db &y){return cl(x.x/y,x.y/y);}
cl con(){return cl(x,-y);}
};
cl a[M],b[M];
int n,L,R[M];
int A[M],B[M],mx,T;
ll ans[M];
void fft(cl *x,int f,int n)
{
for(int i=;i<n;i++) if(i<R[i]) swap(x[i],x[R[i]]);
for(int i=;i<n;i<<=)
{
cl wn(cos(PI/i),f*sin(PI/i));
for(int j=;j<n;j+=(i<<))
{
cl w(,),X,Y;
for(int k=;k<i;k++,w=w*wn)
{
X=x[j+k];Y=w*x[j+k+i];
x[j+k]=X+Y;x[j+k+i]=X-Y;
}
}
}
if(f==-) for(int i=;i<n;++i) x[i]=x[i]/n;
}
void cal(int l,int r,int n,int L)
{
if(l==r)
{
ans[]+=(ll)A[l]*B[l];
return ;
}
register int i,j,mid=l+r>>;
for(i=;i<n;++i) R[i]=(R[i>>]>>)|((i&)<<L);
for(i=;i<n;++i) a[i]=b[i]=cl(,);
for(i=l;i<=mid;++i) a[i-l].x=A[i];
for(i=mid+;i<=r;++i) a[i-mid-].y=B[i];
fft(a,,n);
for(i=;i<n;i++)
{
j=(n-i)&(n-);
b[i]=(a[i]*a[i]-(a[j]*a[j]).con())*cl(,-0.25);
}
fft(b,-,n);
for(i=;i<n;++i) ans[l+mid++i]+=(ll)(b[i].x+0.1); for(i=;i<n;++i) a[i]=b[i]=cl(,);
for(i=mid+;i<=r;i++) a[i-mid-].x=A[i];
for(i=l;i<=mid;i++) a[mid-i].y=B[i];
fft(a,,n);
for(i=;i<n;i++)
{
j=(n-i)&(n-);
b[i]=(a[i]*a[i]-(a[j]*a[j]).con())*cl(,-0.25);
}
fft(b,-,n);
for(i=;i<n;++i) ans[+i]+=(ll)(b[i].x+0.1); cal(l,mid,n>>,L-);
cal(mid+,r,n>>,L-);
}
int main()
{
register int n1,n2,q,i,x;
T=rd();
while(T--)
{
n1=rd();n2=rd();q=rd();mx=;
memset(A,,sizeof(A));
memset(B,,sizeof(B));
memset(ans,,sizeof(ans));
for(i=;i^n1;++i)
{
x=rd();A[x]++;
mx=max(mx,x);
}
for(i=;i^n2;i++)
{
x=rd();B[x]++;
mx=max(mx,x);
}
for(n=,L=-;n<(mx<<);n<<=) L++;
cal(,n-,n,L);
while(q--)
{
x=rd();
printf("%lld\n",ans[x]);
}
}
return ;
}
bzoj 4836: [Lydsy2017年4月月赛]二元运算 -- 分治+FFT的更多相关文章
- 【bzoj4836】[Lydsy2017年4月月赛]二元运算 分治+FFT
题目描述 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使得 a_ ...
- BZOJ 4881: [Lydsy2017年5月月赛]线段游戏
4881: [Lydsy2017年5月月赛]线段游戏 Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 164 Solved: 81[Submit][St ...
- bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数
[Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 577 Solved: 201[Submit][Status][Di ...
- bzoj4836 [Lydsy2017年4月月赛]二元运算
Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) ...
- bzoj 4885: [Lydsy2017年5月月赛]长方体
Description 给定一个a*b*c的长方体,定义其表面上两个点的距离为沿着长方体的表面走的最短路径的长度,请找到距离最远的点对,你需要保证找到的两个点里至少有一个是长方体顶点. Input 第 ...
- BZOJ 4883 [Lydsy2017年5月月赛]棋盘上的守卫(最小生成环套树森林)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4883 [题目大意] 在一个n*m的棋盘上要放置若干个守卫. 对于n行来说,每行必须恰好 ...
- BZOJ 4884 [Lydsy2017年5月月赛]太空猫(单调DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4884 [题目大意] 太空猫(SpaceCat)是一款画面精致.玩法有趣的休闲游戏, 你 ...
- BZOJ 4836: [Lydsy1704月赛]二元运算 分治FFT
Code: #include<bits/stdc++.h> #define ll long long #define maxn 500000 #define setIO(s) freope ...
- 【BZOJ 4832 】 4832: [Lydsy2017年4月月赛]抵制克苏恩 (期望DP)
4832: [Lydsy2017年4月月赛]抵制克苏恩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 275 Solved: 87 Descripti ...
随机推荐
- 一款基于react-native的弹窗提示组件
介绍一款基于react-native的弹窗提示插件 react-native-ms , github地址:https://github.com/jiangzhenfei/react-native-ms ...
- 面向过程编程(OPP) 和面向对象编程(OOP)的关系
面向过程编程(OPP) 和面向对象编程(OOP)的关系 原文链接:http://blog.csdn.net/phphot/article/details/3985480 关于面向过程的编程(OPP)和 ...
- 41、和为S的连续正数序列
一.题目 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- 64_p7
python-flask-whooshalchemy-0.6-10.fc26.noarch.rpm 12-Feb-2017 11:04 51894 python-flask-wtf-0.10.0-8. ...
- nginx升级至1.12.1版本
nginx升级至1.12.1 编号 名称 说明 1 nginx-1.12.1.tar.gz nginx安装程序 2 nginx_upstream_check_module-master.zip 实现后 ...
- PHP扩展插件 imagick 、PDO_MYSQL 安装
环境准备 echo $LC_ALL echo "export LC_ALL=C" >> /etc/profile source /etc/profile yum ins ...
- linux抓包工具tcpdump基本使用
tcpdump 是一款灵活.功能强大的抓包工具,能有效地帮助排查网络故障问题. tcpdump 是一个命令行实用工具,允许你抓取和分析经过系统的流量数据包.它通常被用作于网络故障分析工具以及安全工具. ...
- 洛谷 P2369 EXCEEDED WARNING A 题解
题目传送门 直接用sort排序最后输出即可.但是数组要使用short int 类型.否则会超内存. #include<bits/stdc++.h> using namespace std; ...
- Hadoop(八)Hadoop数据压缩与企业级优化
一 Hadoop数据压缩 1.1 概述 压缩技术能够有效减少底层存储系统(HDFS)读写字节数.压缩提高了网络带宽和磁盘空间的效率.在Hadood下,尤其是数据规模很大和工作负载密集的情况下,使用数据 ...
- log4net 写日志配置
1. nuget install package log4net 2.站点跟目录新建配置文件 LogWriterConfig.xml <?xml version="1.0" ...