题目链接

BZOJ3509

题解

化一下式子,就是

\[2A[j] = A[i] + A[k]
\]

所以我们对一个位置两边的数构成的生成函数相乘即可

但是由于这样做是\(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】的更多相关文章

  1. BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT

    分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...

  2. CC countari & 分块+FFT

    题意: 求一个序列中顺序的长度为3的等差数列. SOL: 对于这种计数问题都是用个数的卷积来进行统计.然而对于这个题有顺序的限制,不好直接统计,于是竟然可以分块?惊为天人... 考虑分块以后的序列: ...

  3. bzoj 3509: [CodeChef] COUNTARI] [分块 生成函数]

    3509: [CodeChef] COUNTARI 题意:统计满足\(i<j<k, 2*a[j] = a[i] + a[k]\)的个数 \(2*a[j]\)不太好处理,暴力fft不如直接暴 ...

  4. BZOJ3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 85[Submit][St ...

  5. [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块)

    [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块) 题面 给出一个长度为n的数组,问有多少三元组\((i,j,k)\)满足\(i<j<k,a_j-a_i=a_ ...

  6. BZOJ 3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 883  Solved: 250[Submit][S ...

  7. BZOJ 3509 分块FFT

    思路: 跟今年WC的题几乎一样 (但是这道题有重 不能用bitset水过去) 正解:分块FFT http://blog.csdn.net/geotcbrl/article/details/506364 ...

  8. CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)

    题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...

  9. CodeChef - COUNTARI Arithmetic Progressions (FFT)

    题意:求一个序列中,有多少三元组$(i,j,k)i<j<k $ 满足\(A_i + A_k = 2*A_i\) 构成等差数列. https://www.cnblogs.com/xiuwen ...

随机推荐

  1. Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用

    Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Object ...

  2. Excel小技巧整理(持续更新)

    合并某列中相同单元格 参考https://jingyan.baidu.com/article/9158e00006db70a25512286f.html 使用方法 先给需要合并的列排序,这样相同数据会 ...

  3. katalon系列十四:执行Windows命令&获取项目路径

    Katalon Studio中也可以运行Windows命令执行一些系统操作. 根据官方文档,在test case中输入命令:cmd = 'del E:\\shot\\*.xlsx E:\\shot\\ ...

  4. 那些年安装Appium遇到的坑

      安装appium以及相关的总体记录   1 主要流程是参照这个来 https://www.cnblogs.com/wangyinghao/p/5780151.html 细节参考虫师的博客 http ...

  5. jvm之GC知识点

    GCRoots:        虚拟机栈(栈帧中的局部变量表)引用的对象       方法区中静态属性引用的对象       方法去中常量引用的对象       本地方法栈中JNI(NATIVE方法) ...

  6. throttle(节流)和debounce(防抖)

    防抖和节流都是用来控制频繁调用的问题,但是这两种的应用场景是有区别的. throttle(节流) 有一个调用周期,在一个很长的时间里分为多段,每一段执行一次.例如onscroll,resize,500 ...

  7. 【转】: 塞尔达组在GDC2017演讲的文字翻译:显示的力量

      塞尔达系列推出新作的时候,美术风格都有明显变化.本作的风格比起写实,笔触轻快变化幅度大是其特征.2011年公开的技术演示中,画面风格要更加写实.最终版则更接近于卡通.5年里到底发生了什么呢? ▲2 ...

  8. 5 种使用 Python 代码轻松实现数据可视化的方法

    数据可视化是数据科学家工作中的重要组成部分.在项目的早期阶段,你通常会进行探索性数据分析(Exploratory Data Analysis,EDA)以获取对数据的一些理解.创建可视化方法确实有助于使 ...

  9. [C++] Solve "Launch Failed. Binary not found." error on Eclipse

    This error is that the default lanch configuration is not being created for this project. To solve i ...

  10. USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)

    Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...