欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BZOJ3509: [CodeChef] COUNTARI(生成函数 分块)

程序员文章站 2024-03-30 23:58:09
题意 "链接" Sol 这都能分块。。。。 首先移一下项,变为统计多少$i define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second define LL long long define ull ......

题意

sol

这都能分块。。。。

首先移一下项,变为统计多少\(i < j < k\),满足\(2a[j] = a[i] + a[k]\)

发现\(a[i] \leqslant 30000\),那么有一种暴力思路是枚举\(j\),对于之前出现过的数构造一个生成函数,对于之后出现过的数构造一个生成函数,求一下第\(2a[j]\)项的值。复杂度\(o(nvlogv)\)

每次枚举\(j\)暴力卷积显然太zz了,我们可以分一下块,对于每一块之前之后的数分别构造生成函数暴力卷积算,对于块内的直接暴力(这里的暴力不只是统计块内的\((i, j, k)\),还要考虑\(j, k\)在块内\(i\)在块外,以及\(i, j\)在块内,\(k\)在块外的情况,但都是可以暴力的)

如果分成\(b\)块的话,复杂度是\(\frac{n}{b} vlogv + b^2\),假设\(n\)\(v\)同阶的话,\(b\)大概取\(nlogn\)是最优的。此时复杂度为\(o(n \sqrt{nlogn})\)

下面的代码在原bzoj上可能会t

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, inf = 1e9 + 1;
const double eps = 1e-9, pi = acos(-1);
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
namespace poly {
    int rev[maxn], gpow[maxn], a[maxn], b[maxn], c[maxn], lim, inv2;
    const int g = 3, mod = 1004535809, mod2 = 1004535808;
    template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
    template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename a, typename b> inline bool chmax(a &x, b y) {return x < y ? x = y, 1 : 0;}
    template <typename a, typename b> inline bool chmin(a &x, b y) {return x > y ? x = y, 1 : 0;}
    int fp(int a, int p, int p = mod) {
        int base = 1;
        for(; p > 0; p >>= 1, a = 1ll * a * a % p) if(p & 1) base = 1ll * base *  a % p;
        return base;
    }
    int inv(int x) {
        return fp(x, mod - 2);
    }
    int getlen(int x) {
        int lim = 1;
        while(lim < x) lim <<= 1;
        return lim;
    }
    void init(/*int p,*/ int lim) {
        inv2 = fp(2, mod - 2);
        for(int i = 1; i <= lim; i++) gpow[i] = fp(g, (mod - 1) / i);
    }
    void ntt(int *a, int lim, int opt) {
        int len = 0; for(int n = 1; n < lim; n <<= 1) ++len; 
        for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
        for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
        for(int mid = 1; mid < lim; mid <<= 1) {
            int wn = gpow[mid << 1];
            for(int i = 0; i < lim; i += (mid << 1)) {
                for(int j = 0, w = 1; j < mid; j++, w = mul(w, wn)) {
                    int x = a[i + j], y = mul(w, a[i + j + mid]);
                    a[i + j] = add(x, y), a[i + j + mid] = add(x, -y);
                }
            }
        }
        if(opt == -1) {
            reverse(a + 1, a + lim);
            int inv = fp(lim, mod - 2);
            for(int i = 0; i <= lim; i++) mul2(a[i], inv);
        }
    }
    void mul(int *a, int *b, int n, int m) {
        memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
        int lim = 1, len = 0; 
        while(lim <= n + m) len++, lim <<= 1;
        for(int i = 0; i <= n; i++) a[i] = a[i]; 
        for(int i = 0; i <= m; i++) b[i] = b[i];
        ntt(a, lim, 1); ntt(b, lim, 1);
        for(int i = 0; i <= lim; i++) b[i] = mul(b[i], a[i]);
        ntt(b, lim, -1);
        for(int i = 0; i <= n + m; i++) b[i] = b[i];
        memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
    }
};
using namespace poly; 
int n, a[maxn], mx, block, ll[maxn], rr[maxn], belong[maxn], mxblock, num[maxn];
ll solve1(int l, int r) {
    for(int i = 1; i < l; i++) num[a[i]]++; 
    ll ret = 0;
    for(int j = l; j <= r; j++) 
        for(int k = j + 1; k <= r; k++) 
            if(2 * a[j] - a[k] >= 0) ret += num[2 * a[j] - a[k]];
    for(int i = 1; i < l; i++) num[a[i]]--;

    for(int i = n; i > r; i--) num[a[i]]++;
    for(int j = r; j >= l; j--) 
        for(int k = j - 1; k >= l; k--) 
            if(2 * a[j] - a[k] >= 0) ret += num[2 * a[j] - a[k]];
    for(int i = n; i > r; i--) num[a[i]]--;
    
    for(int j = l; j <= r; j++) {
        for(int i = j - 1; i >= l; i--) num[a[i]]++;
        for(int k = j + 1; k <= r; k++) 
            if(2 * a[j] - a[k] >= 0) ret += num[2 * a[j] - a[k]];
        for(int i = j - 1; i >= l; i--) num[a[i]]--;
    }
    return ret;
}
int ta[maxn], tb[maxn], lim;
ll solve2(int l, int r) {
    memset(ta, 0, sizeof(ta));
    memset(tb, 0, sizeof(tb));
    for(int i = l - 1; i >= 1; i--) ta[a[i]]++;
    for(int i = r + 1; i <= n; i++) tb[a[i]]++;
    mul(ta, tb, mx, mx); ll ret = 0;
    for(int i = l; i <= r; i++) ret += tb[2 * a[i]];
    return ret;
}
signed main() {
    //freopen("a.in", "r", stdin);  
    n = read(); block = sqrt(8 *  n * log2(n)); 
    memset(ll, 0x3f, sizeof(ll));
    for(int i = 1; i <= n; i++) {
        belong[i] = (i - 1) / block + 1; chmax(mxblock, belong[i]);
        chmin(ll[belong[i]], i);
        chmax(rr[belong[i]], i);
        a[i] = read(), chmax(mx, a[i]);
    }
    lim = getlen(mx); init(4 * lim);
    ll ans = 0;
    for(int i = 1; i <= mxblock; i++) {
        ans += solve1(ll[i], rr[i]);
        ans += solve2(ll[i], rr[i]);
    }
    cout << ans;
    return 0;
}
/*
7
7 0 4 7 0 8 8 
*/