BZOJ3509 [CodeChef] COUNTARI 【分块 + fft】
题目链接
题解
化一下式子,就是
\]
所以我们对一个位置两边的数构成的生成函数相乘即可
但是由于这样做是\(O(n^2logn)\)的,我们考虑如何优化
显然可以分块做,我们不对所有数左右求卷积,只对\(B\)个块左右做,这样\(i\)和\(k\)都在块外的情况就可以统计出来
\(i\)或\(k\)在块内的情况可以暴力扫一遍计算
复杂度\(O(Bnlogn + nB)\)
经计算\(B = \sqrt{nlogn}\)最优
但考虑到\(fft\)的常数问题,\(B = 2000\)左右比较合理
复杂度就大概是\(O((nlogn)^{\frac{3}{2}})\)
竟然能\(A\)....
交上去就垫底了。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define REP(i,n) for (register int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
#define LL long long int
#define res register
using namespace std;
const int maxn = 400005,maxm = 4005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
struct E{
double a,b;
E(){}
E(double x,double y):a(x),b(y) {}
E(int x,int y):a(x),b(y) {}
inline E operator =(const int& b){
this->a = b; this->b = 0;
return *this;
}
inline E operator =(const double& b){
this->a = b; this->b = 0;
return *this;
}
inline E operator /=(const double& b){
this->a /= b; this->b /= b;
return *this;
}
};
inline E operator *(const E& a,const E& b){
return E(a.a * b.a - a.b * b.b,a.a * b.b + a.b * b.a);
}
inline E operator *=(E& a,const E& b){
return a = E(a.a * b.a - a.b * b.b,a.a * b.b + a.b * b.a);
}
inline E operator +(const E& a,const E& b){
return E(a.a + b.a,a.b + b.b);
}
inline E operator -(const E& a,const E& b){
return E(a.a - b.a,a.b - b.b);
}
const double pi = acos(-1);
int R[maxn];
void fft(E* a,int n,int f){
for (res int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (res int i = 1; i < n; i <<= 1){
E wn(cos(pi / i),f * sin(pi / i));
for (res int j = 0; j < n; j += (i << 1)){
E w(1,0),x,y;
for (res int k = 0; k < i; k++,w = w * wn){
x = a[j + k],y = w * a[j + k + i];
a[j + k] = x + y; a[j + k + i] = x - y;
}
}
}
if (f == -1) for (int i = 0; i < n; i++) a[i] /= n;
}
E A[maxn],C[maxn];
int N,B,val[maxn],b[maxn],bl[maxm],br[maxm],bi;
LL ans;
void work1(){
for (res int x = 2; x < bi; x++){
int deg1 = 0,deg2 = 0;
for (res int i = 1; b[i] != x; i++)
A[val[i]].a++,deg1 = max(deg1,val[i]);
for (res int i = N; b[i] != x; i--)
C[val[i]].a++,deg2 = max(deg2,val[i]);
int n = 1,L = 0;
while (n <= deg1 + deg2) n <<= 1,L++;
for (res int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
fft(A,n,1); fft(C,n,1);
for (res int i = 0; i < n; i++) A[i] *= C[i];
fft(A,n,-1);
for (res int i = bl[x]; i <= br[x]; i++)
ans += (LL)floor(A[val[i] << 1].a + 0.1);
for (res int i = 0; i < n; i++) A[i] = C[i] = 0.0;
}
}
int bac[maxn],M;
void work2(){
for (res int i = 1; i <= N; i++){
for (res int j = i + 1; b[j] == b[i]; j++){
if (val[j] >= val[i] && val[i] >= val[j] - val[i])
ans += bac[val[i] - (val[j] - val[i])];
if (val[j] < val[i] && val[i] + val[i] - val[j] <= 30000)
ans += bac[val[i] + val[i] - val[j]];
}
bac[val[i]]++;
}
REP(i,N) bac[val[i]]--;
for (res int i = N; i; i--){
for (res int j = i - 1; b[j] == b[i]; j--){
if (val[j] >= val[i] && val[i] >= val[j] - val[i])
ans += bac[val[i] - (val[j] - val[i])];
if (val[j] < val[i] && val[i] + val[i] - val[j] <= 30000)
ans += bac[val[i] + val[i] - val[j]];
}
bac[val[i]]++;
}
REP(i,N) bac[val[i]]--;
for (res int x = 1; x <= bi; x++){
for (res int i = bl[x]; i <= br[x]; i++){
for (res int j = i + 1; j <= br[x]; j++){
if (val[j] >= val[i] && val[i] >= val[j] - val[i])
ans -= bac[val[i] - (val[j] - val[i])];
if (val[j] < val[i] && val[i] + val[i] - val[j] <= 30000)
ans -= bac[val[i] + val[i] - val[j]];
}
bac[val[i]]++;
}
for (int i = bl[x]; i <= br[x]; i++) bac[val[i]]--;
}
}
int main(){
N = read(); B = 2000;
REP(i,N){
val[i] = read(),b[i] = i / B + 1,M = max(M,val[i]);
if (b[i] != b[i - 1]) br[b[i - 1]] = i - 1,bl[b[i]] = i;
}
br[b[N]] = N; bi = b[N];
work1();
work2();
printf("%lld\n",ans);
return 0;
}
BZOJ3509 [CodeChef] COUNTARI 【分块 + fft】的更多相关文章
- BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT
分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...
- CC countari & 分块+FFT
题意: 求一个序列中顺序的长度为3的等差数列. SOL: 对于这种计数问题都是用个数的卷积来进行统计.然而对于这个题有顺序的限制,不好直接统计,于是竟然可以分块?惊为天人... 考虑分块以后的序列: ...
- bzoj 3509: [CodeChef] COUNTARI] [分块 生成函数]
3509: [CodeChef] COUNTARI 题意:统计满足\(i<j<k, 2*a[j] = a[i] + a[k]\)的个数 \(2*a[j]\)不太好处理,暴力fft不如直接暴 ...
- BZOJ3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 339 Solved: 85[Submit][St ...
- [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块)
[BZOJ 3509] [CodeChef] COUNTARI (FFT+分块) 题面 给出一个长度为n的数组,问有多少三元组\((i,j,k)\)满足\(i<j<k,a_j-a_i=a_ ...
- BZOJ 3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 883 Solved: 250[Submit][S ...
- BZOJ 3509 分块FFT
思路: 跟今年WC的题几乎一样 (但是这道题有重 不能用bitset水过去) 正解:分块FFT http://blog.csdn.net/geotcbrl/article/details/506364 ...
- CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)
题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...
- CodeChef - COUNTARI Arithmetic Progressions (FFT)
题意:求一个序列中,有多少三元组$(i,j,k)i<j<k $ 满足\(A_i + A_k = 2*A_i\) 构成等差数列. https://www.cnblogs.com/xiuwen ...
随机推荐
- linux下,将一个目录中的图片文件合成为gif图片
# {} 为文件所在目录位置 # {} 为gif图片位置 convert -delay -depth -layers optimize -quality -loop {} {}
- 谈谈你对Java异常处理机制的理解
先谈谈我的理解:异常处理机制可以说是让我们编写的程序运行起来更加的健壮,无论是在程序调试.运行期间发生的异常情况的捕获,都提供的有效的补救动作,任何业务逻辑都会存在异常情况,这时只需要记录这些异常情况 ...
- JY播放器【喜马拉雅FM电脑端,附带下载功能】
今天给大家带来一款神器----JY播放器.可以不用打开网页就在电脑端听喜马拉雅FM的节目,而且可以直接下载,对于我这种强迫症患者来说真的是神器.我是真的不喜欢电脑任务栏上面密密麻麻的. 目前已经支持平 ...
- Python中元祖,列表,字典的区别
Python中有3种內建的数据结构:列表.元祖和字典: 1.列表 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目. 列表中的项目应该包括在方括号中,这样Python就知道 ...
- 数据挖掘学习笔记——kaggle 数据预处理
预处理 1. 删除缺失值 a. 删除行即样本(对于样本如果输出变量存在缺失的则直接删除该行,因为无法用该样本训练) b. 删除列,即特征(采用这种删除方式,应保证训练集和验证集都应当删除相同的特征) ...
- US Customs bond DDP 船运
客户提供目的港门点地址,提供美国进口产品的关税税率基本上就可以了关于ISF信息到时候你发给老外让老外填填好就可以了BAND 货值*0.575%POA 货值*0.335%这二个费用如果国内付就付了,国 ...
- 如何计算FOB价格
FOB价格是国际贸易术语常有的一种算法,针对不同的对象,FOB价格也有不一样的算法.对于做外贸生意的朋友,需要了解FOB价格以及各项费用名称,以及如何计算FOB价格. FOB价格是国际FOB价格语常有 ...
- 工作在Amazon:为何晋升如此难?
英文原文:Why It's So Difficult to Climb Amazon's Corporate Ladder 本文作者 Brad Stone 的新书 The Everything Sto ...
- hashlib模块使用详情
python常用模块目录 一:hashlib简介 1.什么叫hash:hash是一种算法(不同的hash算法只是复杂度不一样)(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224 ...
- 5.airflow问题
1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...