제출 #1287722

#제출 시각아이디문제언어결과실행 시간메모리
1287722canhnam357Pairs (IOI07_pairs)C++20
100 / 100
491 ms70320 KiB
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
void solve1() {
    int n, d, m;
    cin >> n >> d >> m;
    vector<int> a(n);
    for (int &i : a) cin >> i;
    sort(all(a));
    long long ans = 0;
    for (int l = 0, r = 0; r < n; r++) {
        while (a[r] - a[l] > d) l++;
        ans += r - l;
    }
    cout << ans;
}
struct FakeFenwick { 
        vector<vector<int>> fw, val; 
        int n;
        FakeFenwick() {}
        FakeFenwick(int n): n(n), val(n + 1, vector<int>()), fw(n + 1) {} 
        bool iscc = 0; 
        void fakeU(int x, int y) { 
                iscc = 0; 
                for (; x <= n; x += x & -x) val[x].push_back(y); 
        }
        void cc() { 
                if (iscc) return; 
                for (int x = 1; x <= n; x++) { 
                        sort(all(val[x])); 
                        val[x].erase(unique(all(val[x])), val[x].end()); 
                        fw[x].resize(val[x].size() + 1); 
                }
                iscc = 1; 
        }
        void update(int x, int y, int v) { 
                assert(iscc); 
                for (; x <= n; x += x & -x) { 
                        int yy = upper_bound(all(val[x]), y) - val[x].begin(); 
                        for (; yy <= val[x].size(); yy += yy & -yy) { 
                                fw[x][yy] += v; 
                        }
                         
                }
        }
        int get(int x, int y) { 
                assert(iscc); 
                int res = 0; 
                for (; x; x -= x & -x) { 
                        int yy = upper_bound(all(val[x]), y) - val[x].begin(); 
                        for (; yy; yy -= yy & -yy) { 
                                res += fw[x][yy]; 
                        }
                }
                return res; 
        }
        int get(int x1, int y1, int x2, int y2)
        {
            return get(x2, y2) - get(x2, y1 - 1) - get(x1 - 1, y2) + get(x1 - 1, y1 - 1);
        }
};
void solve2() {
    // xi - xj + |yi - yj| <= d
    // xi + yi <= d + xj + yj if yi >= yj
    // xi - yi <= d + xj - yj if yi <  yj
    int n, m, d;
    cin >> n >> d >> m;
    vector<pair<int, int>> a(n);
    for (auto &[x, y] : a) cin >> x >> y;
    sort(all(a));
    FakeFenwick t1(m), t2(m);
    for (auto [x, y] : a) {
        t1.fakeU(y, d + x + y);
        t2.fakeU(y, d + x - y);
    }
    t1.cc();
    t2.cc();
    long long ans = 0;
    for (auto [x, y] : a) {
        ans += t1.get(1, x + y, y, d + m + m) + t2.get(y + 1, x - y, m, d + m + m);
        t1.update(y, d + x + y, 1);
        t2.update(y, d + x - y, 1);
    }
    cout << ans;
}
struct fenwick3D {
    int m;
    fenwick3D() {}
    fenwick3D(int m) {
        this->m = m;
    }
    #define N 76
    long long bit[5 * N][N][N]{};
    void update(int x, int y, int z, int w) {
        x += 2 * m;
        assert(x > 0);
        for (int a = x; a <= 5 * m; a += a & -a) {
            for (int b = y; b <= m; b += b & -b) {
                for (int c = z; c <= m; c += c & -c) {
                    bit[a][b][c] += w;
                }
            }
        }
    }
    long long get(int x, int y, int z) {
        x += 2 * m;
        if (x < 0) return 0;
        long long res = 0;
        for (int a = x; a > 0; a -= a & -a) {
            for (int b = y; b > 0; b -= b & -b) {
                for (int c = z; c > 0; c -= c & -c) {
                    res += bit[a][b][c];
                }
            }
        }
        return res;
    }
    long long get(int x1, int x2, int y1, int y2, int z1, int z2) {
    long long ans = 0;
        vector<int> x = {x1 - 1, x2}, y = {y1 - 1, y2}, z = {z1 - 1, z2};
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    int l = i + j + k;
                    int f = (l & 1 ? 1 : -1);
                    ans += f * get(x[i], y[j], z[k]);
                }
            }
        }
        return ans;
    }
};
void solve3() {
    int n, d, m;
    cin >> n >> d >> m;
    vector<tuple<int, int, int>> a(n);
    for (auto &[x, y, z] : a) cin >> x >> y >> z;
    sort(all(a));
    /*
            xi - xj + yi - yj + zi - zj <= d
        ->  xi + yi + zi - d <= xj + yj + zj if yi >= yj && zi >= zj
            get (xi + yi + zi, 3 * m, 1, yi, 1, yi)
            update (d + xi + yi + zi)
            range [3 + 2 * m, 5 * m]

            xi - xj + yi - yj + zj - zi <= d
        ->  xi + yi - zi <= d + xj + yj - zj if yi >= yj && zi <  zj
            get (xi + yi - zi, 2 * m - 1, yi, m, zi + 1, m)
            update (d + xi + yi - zi)
            range [2 + m, 4 * m - 1]

            xi - xj + yj - yi + zi - zj <= d
        ->  xi - yi + zi <= d + xj - yj + zj if yi <  yj && zi >= zj
            get (xi - yi + zi, 2 * m - 1, yi + 1, m, 1, zi)
            update (d + xi - yi + zi)
            range [2 + m, 4 * m - 1]

            xi - xj + yj - yi + zj - zi <= d
        ->  xi - yi - zi <= d + xj - yj - zj if yi <  yj && zi <  zj
            get (xi - yi - zi, m - 2, yi + 1, m, zi + 1, m)
            update (d + xi - yi - zi)
            range [1, 3 * m - 2]
    */
    fenwick3D t1(m), t2(m), t3(m), t4(m);
    long long ans = 0;
    for (auto [x, y, z] : a) {
        ans += t1.get(x + y + z - d, 3 * m, 1, y, 1, z) +
               t2.get(x + y - z - d, 2 * m - 1, 1, y, z + 1, m) +
               t3.get(x - y + z - d, 2 * m - 1, y + 1, m, 1, z) +
               t4.get(x - y - z - d, m - 2, y + 1, m, z + 1, m);
        t1.update(x + y + z, y, z, 1);
        t2.update(x + y - z, y, z, 1);
        t3.update(x - y + z, y, z, 1);
        t4.update(x - y - z, y, z, 1);
    }
    cout << ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int type;
    cin >> type;
    if (type == 1) solve1();
    else if (type == 2) solve2();
    else solve3();   
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...