4836: [Lydsy2017年4月月赛]二元运算

Time Limit: 8 Sec  Memory Limit: 128 MB

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

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 ;
}

最新文章

  1. c#下载共享文件夹下的文件并记录错误日志
  2. 使用/调用 函数的时候, 前面加不加 对象或 this?
  3. DP:Cow Bowling(POJ 3176)
  4. rman进行备份、恢复
  5. android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)
  6. JSEL 表达式
  7. 另外一种方式装win2008r2
  8. 基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码
  9. github 上传下载代码简单使用
  10. httpclient案例二(调用百度地图的接口)
  11. ado.net的简单数据库操作(二)之封装SqlHelperl类
  12. [Alibaba-ARouter] 简单好用的Android页面路由框架
  13. python 往mysql数据库中插入多条记录。
  14. linux 软硬链接
  15. 【Ubuntu】boot空间不足
  16. 在ns2.35下完成柯老师lab18实验
  17. 分享Memcached shell启动停止脚本
  18. Linux工具-nmon
  19. maven(一)maven自带的插件
  20. java.lang.NullPointerException: Attempt to invoke virtual method &#39;java.util.List com.yunweather.app.db.YunWeatherDB.loadProvinces()&#39; on a null object reference

热门文章

  1. isolation forest进行异常点检测
  2. wget下载整个网站或特定目录
  3. Java杂知识汇总(自己积累的)
  4. DEDECMS如何让栏目外部链接在新窗口中打开
  5. WordPress 用户管理插件 WP User Manager
  6. 用WP SMTP插件实现邮件发送功能
  7. 禁用Flash P2P上传
  8. day6 time和datetime模块
  9. python使用smtplib库和smtp.qq.com邮件服务器发送邮件
  10. bzoj 1877 最小费用流