题目链接

题意:给定两个长度为n的数组a与长度为m的数组b, 给定一个操作符op满足 x op y = x < y ? x+y : x-y.  有q个询问,每次给出询问c,问:有多少对(i, j)满足a[i] op b[j] = c ?

0 <= c <= 100000, 其余数据范围在[0, 50000].

题解:问题的关键在于如何分隔开 x < y与x >= y. cdq分治,合并的时候a[l, mid]与b[mid+1, r]卷积一次计算a[] < b[] , a[mid+1, r]与b[l, mid]再卷积一次a[] > b[]即可。

卡时,memset的时候优化了一下。

 #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e5+;
struct comp{
double r,i;comp(double _r=,double _i=){r=_r;i=_i;}
comp operator+(const comp x){return comp(r+x.r,i+x.i);}
comp operator-(const comp x){return comp(r-x.r,i-x.i);}
comp operator*(const comp x){return comp(r*x.r-i*x.i,r*x.i+i*x.r);}
}x[N<<], y[N<<];
const double pi=acos(-1.0);
void FFT(comp a[],int n,int t){
for(int i=,j=;i<n-;i++){
for(int s=n;j^=s>>=,~j&s;);
if(i<j)swap(a[i],a[j]);
}
for(int d=;(<<d)<n;d++){
int m=<<d,m2=m<<;
double o=pi/m*t;comp _w(cos(o),sin(o));
for(int i=;i<n;i+=m2){
comp w(,);
for(int j=;j<m;j++){
comp &A=a[i+j+m],&B=a[i+j],t=w*A;
A=B-t;B=B+t;w=w*_w;
}
}
}
if(t==-)for(int i=;i<n;i++)a[i].r/=n;
}
int a[N], b[N], n, m, q;
ll ans[N];
void cdq(int l, int r){
if(l == r){
ans[] += a[l]*b[l];
return;
}
int mid = l+r >> ;
cdq(l, mid);
int len = ;
while(len <= (r-l+)) len <<= ;
memset(x, , sizeof(comp)*len );
memset(y, , sizeof(comp)*len );
for(int i = l; i <= mid; i++)
x[i-l] = comp(a[i], );
for(int i = mid+; i <= r; i++)
y[i-mid-] = comp(b[i], );
FFT(x, len, ); FFT(y, len, );
for(int i = ; i < len; i++)
x[i] = x[i]*y[i];
FFT(x, len, -);
for(int i = l+mid+; i <= mid+r; i++)
ans[i] += x[i-l-mid-].r+0.5;
for(int i = ; i < len; i++)
x[i] = y[i] = comp(, );
for(int i = mid+; i <= r; i++)
x[i-mid-] = comp(a[i], );
for(int i = l; i <= mid; i++)
y[mid+-i] = comp(b[i], );
FFT(x, len, ); FFT(y, len, );
for(int i = ; i < len; i++)
x[i] = x[i]*y[i];
FFT(x, len, -);
for(int i = ; i <= r-l; i++)
ans[i] += x[i].r+0.5;
cdq(mid+, r);
} int main(){
int t, x, maxn; scanf("%d", &t);
while(t--){
scanf("%d%d%d", &n, &m, &q);
maxn = ;
for(int i = ; i < n; i++){
scanf("%d", &x);
maxn = max(maxn, x);
a[x]++;
}
for(int i = ; i < m; i++){
scanf("%d", &x);
maxn = max(maxn, x);
b[x]++;
}
cdq(, maxn);
while(q--){
scanf("%d", &x);
printf("%lld\n", ans[x]);
}
memset(a, , sizeof(int)*(maxn+));
memset(b, , sizeof(int)*(maxn+));
memset(ans, , sizeof(ll)*(maxn*+));
}
return ;
}

[玲珑OJ1044] Quailty and Binary Operation (FFT+cdq分治)的更多相关文章

  1. Quailty and Binary Operation

    Quailty and Binary Operation 题意 分别给\(N,M(N,M \le 50000)\)两个数组\(A\)和\(B\),满足\(0 \le A_i,B_i \le 50000 ...

  2. hdu 5830 FFT + cdq分治

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. HDU5730 FFT+CDQ分治

    题意:dp[n] = ∑ ( dp[n-i]*a[i] )+a[n], ( 1 <= i < n) cdq分治. 计算出dp[l ~ mid]后,dp[l ~ mid]与a[1 ~ r-l ...

  4. hdu 5730 Shell Necklace fft+cdq分治

    题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...

  5. BNUOJ 51279[组队活动 Large](cdq分治+FFT)

    传送门 大意:ACM校队一共有n名队员,从1到n标号,现在n名队员要组成若干支队伍,每支队伍至多有m名队员,求一共有多少种不同的组队方案.两个组队方案被视为不同的,当且仅当存在至少一名队员在两种方案中 ...

  6. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  7. HDU 5730 Shell Necklace(CDQ分治+FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...

  8. 【BZOJ3456】轩辕朗的城市规划 无向连通图计数 CDQ分治 FFT 多项式求逆 多项式ln

    题解 分治FFT 设\(f_i\)为\(i\)个点组成的无向图个数,\(g_i\)为\(i\)个点组成的无向连通图个数 经过简单的推导(枚举\(1\)所在的连通块大小),有: \[ f_i=2^{\f ...

  9. 【CF553E】Kyoya and Train 最短路+cdq分治+FFT

    [CF553E]Kyoya and Train 题意:有一张$n$个点到$m$条边的有向图,经过第i条边要花$c_i$元钱,经过第i条边有$p_{i,k}$的概率要耗时k分钟.你想从1走到n,但是如果 ...

随机推荐

  1. sqlite3把字段为int32(用c++的time(nullptr)获取的)的秒数显示为年月日时分秒

    select id, type, msg, datetime(updateTime, 'unixepoch', 'localtime') from ServerLog

  2. [C++]C++标准里 string和wstring

    typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; 前者string是常用类型, ...

  3. ACM题目————吝啬的国度

    描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设 ...

  4. Oracle绑定变量

    select * from table where id = ? 类似于上面这样的sql,如果不用绑定变量,每次执行时Oracle会认为是不同的sql,会在每次执行时生成一遍执行计划,而执行计划的生成 ...

  5. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10:37 18人阅读 评论(0) 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

  6. ural 1109,NYOJ 239,匈牙利算法邻接表

    NYOJ 239:http://acm.nyist.net/JudgeOnline/problem.php?pid=239 ural 1109 :http://acm.timus.ru/problem ...

  7. torch基本命令

    命令行输入th进入torch框架 命令行输入th + lua文件表示用torch执行lua文件

  8. NOIP 营业额统计 splay tree 纯模板

    2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 389       ...

  9. hdu 3123

    GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  10. WebForm上传文件FileUpload

    //Button1的点击事件 //FileUpload1.FileName为所传文件的名字. //以DateTime.Now.ToString("yyyyMMddhhmmssms" ...