The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

Portal: https://nanti.jisuanke.com/t/41400

Meaning of the questions:

You three arrays a, b, c, to ask you how many triples (i, j, k), such that
\ [\ begin {array} { l} {\ left | A_ {i} -B_ { j} \ right | \ leq C_ {k}, \ text {and}} \\ {\ left | B_ {j} -C_ {k} \ right | \ leq A_ {i}, \ text {and}} \ \ {\ left | A_ {i } -C_ {k} \ right | \ leq B_ {j}} \ end {array} \]

answer:

Above inequality through simplification, we can get

We request the number of triples that \ (A_i, B_j, C_k \ ) can form a triangle

Thus the subject composition is similar to the triangle HDU4609 ( https://www.cnblogs.com/buerdepepeqi/p/11236100.html )

But the difference is that we need to choose from the three arrays

So here on the choice of issues related to duplicate, we consider de-emphasis

A + b is assumed to take it again to do the convolution of the length b of the number of sticks in a +,

We assume that c is the longest side of the triangle, then a, b, c can not form three triangular pieces of wood where c is the length of a + b is greater than the number of

We enumerate the number (a + b) this length, then the number of triangles that can not form composes the current program length (a, b) is greater than the length of the c * of

Therefore, according to this we can get it done three times convolution, c is the longest time are enumerated side, when b is the longest side, a is the maximum length and the number of law when the edges, then all triples minus the number of illegal triad group is the legitimate number of triples

Here's a little trick is small-scale violence? ? If small-scale violence you will not T

(Nostalgia about the room Massacre

Let's analyze why?

We assume that the FFT Complex class with a constant x

In the case of T 100, we simply ran three times FFT, then that is three times the positive FFT, three times IDFT, then that is a constant 6

Complexity is the last

\ [T * 2 ^ {log
(2n)} * log (2n) * 6 * x \\ = 100 * 2 ^ {18} * 18 * 6 = 2,831,155,200 \] However, the complexity of violence is n2

Suppose there are 20 large data set of 80 small data set (1000)

Complexity is then
\ [20 * 18 * 2 ^ {18} * 1000 * 1000 * 6 + 80 = 646,231,040 \]
Therefore, the complexity of large-scale violence small FFT is relatively good drop

(Thanks NE chiefs for complexity analysis

Code:

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
    return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
    double ans = 1.0;
    while(b) {
        if(b % 2)ans = ans * a;
        a = a * a;
        b /= 2;
    } return ans;
}
LL quick_pow(LL x, LL y) {
    LL ans = 1;
    while(y) {
        if(y & 1) {
            ans = ans * x % mod;
        } x = x * x % mod;
        y >>= 1;
    } return ans;
}

LL res[maxn << 2], len;
struct Complex {
    double r, i;

    Complex(double r = 0, double i = 0) : r(r), i(i) {};

    Complex operator+(const Complex &rhs) {
        return Complex(r + rhs.r, i + rhs.i);
    }

    Complex operator-(const Complex &rhs) {
        return Complex(r - rhs.r, i - rhs.i);
    }

    Complex operator*(const Complex &rhs) {
        return Complex(r * rhs.r - i * rhs.i, i * rhs.r + r * rhs.i);
    }
} va[maxn << 2], vb[maxn << 2];

void rader(Complex F[], int len) { //len = 2^M,reverse F[i] with  F[j] j为i二进制反转
    int j = len >> 1;
    for (int i = 1; i < len - 1; ++i) {
        if (i < j) swap(F[i], F[j]);  // reverse
        int k = len >> 1;
        while (j >= k) {
            j -= k;
            k >>= 1;
        }
        if (j < k) j += k;
    }
}

void FFT(Complex F[], const int &len, const int &t) {
    rader(F, len);
    for (int h = 2; h <= len; h <<= 1) {
        Complex wn(cos(-t * 2 * Pi / h), sin(-t * 2 * Pi / h));
        for (int j = 0; j < len; j += h) {
            Complex E(1, 0); //旋转因子
            for (int k = j; k < j + h / 2; ++k) {
                Complex u = F[k];
                Complex v = E * F[k + h / 2];
                F[k] = u + v;
                F[k + h / 2] = u - v;
                E = E * wn;
            }
        }
    }
    if (t == -1)   //IDFT
        for (int i = 0; i < len; ++i)
            F[i].r /= len;
}

void Conv(Complex a[], Complex b[], const int &len) { //求卷积
    FFT(a, len, 1);
    FFT(b, len, 1);
    for (int i = 0; i < len; ++i) a[i] = a[i] * b[i];
    FFT(a, len, -1);
}

void work() {
    Conv(va, vb, len);
    for (int i = 0; i < len; ++i)res[i] = va[i].r + 0.5;
}
int a[maxn], b[maxn], c[maxn];
int numa[maxn], numb[maxn], numc[maxn];
int suma[maxn], sumb[maxn], sumc[maxn];//长度为i的数量
LL resab[maxn], resbc[maxn], resac[maxn];//长度为i的 a,b的个数
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int T;
    int cas = 1;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        int maxlena = 0, maxlenb = 0, maxlenc = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            numa[a[i]]++;
            maxlena = max(maxlena, a[i]);
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            numb[b[i]]++;
            maxlenb = max(maxlenb, b[i]);

        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &c[i]);
            numc[c[i]]++;
            maxlenc = max(maxlenc, c[i]);
        }
        LL ans = 1ll * n * n * n;
        if(n <= 1000) {
            for(int i = 1; i <= maxlena; i++) {
                suma[i] = suma[i - 1] + numa[i];
            }
            for(int i = 1; i <= maxlenb; i++) {
                sumb[i] = sumb[i - 1] + numb[i];
            }
            for(int i = 1; i <= maxlenc; i++) {
                sumc[i] = sumc[i - 1] + numc[i];
            }
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= n; j++) {
                    resab[a[i] + b[j]]++;
                    resbc[b[i] + c[j]]++;
                    resac[a[i] + c[j]]++;
                }
            }
            int mx = max(maxlena, max(maxlenb, maxlenc));
            for(int i = 1; i <= mx; i++) {
                if(i <= maxlenc) ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
                if(i <= maxlenb) ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
                if(i <= maxlena) ans -= resbc[i] * (suma[maxlena] - suma[i]);
            }
            memset(suma, 0, sizeof(int) * (maxlena + 2));
            memset(sumb, 0, sizeof(int) * (maxlenb + 2));
            memset(sumc, 0, sizeof(int) * (maxlenc + 2));
            memset(numa, 0, sizeof(int) * (maxlena + 2));
            memset(numb, 0, sizeof(int) * (maxlenb + 2));
            memset(numc, 0, sizeof(int) * (maxlenc + 2));
            memset(resac, 0, sizeof(LL) * (mx * 2 + 2));
            memset(resab, 0, sizeof(LL) * (mx * 2 + 2));
            memset(resbc, 0, sizeof(LL) * (mx * 2 + 2));
        } else {
            maxlena++, maxlenb++, maxlenc++;
            len = 1;
            int mxab = max(maxlena, maxlenb);
            while(len < 2 * mxab) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxab) {
                    va[i] = Complex(numa[i], 0);
                    vb[i] = Complex(numb[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resab[i] = res[i];

            len = 1;
            int mxac = max(maxlena, maxlenc);
            while(len < 2 * mxac) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxac) {
                    va[i] = Complex(numa[i], 0);
                    vb[i] = Complex(numc[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resac[i] = res[i];

            len = 1;
            int mxbc = max(maxlenb, maxlenc);
            while(len < 2 * mxbc) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxbc) {
                    va[i] = Complex(numb[i], 0);
                    vb[i] = Complex(numc[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resbc[i] = res[i];

            for(int i = 1; i <= maxlena; i++) {
                suma[i] = suma[i - 1] + numa[i];
            }
            for(int i = 1; i <= maxlenb; i++) {
                sumb[i] = sumb[i - 1] + numb[i];
            }
            for(int i = 1; i <= maxlenc; i++) {
                sumc[i] = sumc[i - 1] + numc[i];
            }

            for (int i = 1; i <= 2 * mxab; ++i) {
                if (i > maxlenc) break;
                ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
            }
            for (int i = 1; i <= 2 * mxbc; ++i) {
                if (i > maxlena) break;
                ans -= resbc[i] * (suma[maxlena] - suma[i]);
            }
            for (int i = 1; i <= 2 * mxac; ++i) {
                if (i > maxlenb) break;
                ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
            }
            memset(suma, 0, sizeof(int) * (maxlena + 2));
            memset(sumb, 0, sizeof(int) * (maxlenb + 2));
            memset(sumc, 0, sizeof(int) * (maxlenc + 2));
            memset(numa, 0, sizeof(int) * (maxlena + 2));
            memset(numb, 0, sizeof(int) * (maxlenb + 2));
            memset(numc, 0, sizeof(int) * (maxlenc + 2));
            memset(resac, 0, sizeof(LL) * (mxac * 2 + 2));
            memset(resab, 0, sizeof(LL) * (mxab * 2 + 2));
            memset(resbc, 0, sizeof(LL) * (mxbc * 2 + 2));
        }
        printf("Case #%d: %lld\n", cas++, ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/buerdepepeqi/p/11525304.html