Submission #1300707

#TimeUsernameProblemLanguageResultExecution timeMemory
1300707ArtPairs (IOI07_pairs)C++20
100 / 100
136 ms1692 KiB
///     - Art -
#include <bits/stdc++.h>

#define el              cout << '\n'

#define FOR(i, a, b)    for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, b, a)    for (int i = (b), _a = (a); i >= _a; --i)
#define REP(i, c)       for (int i = 0, _c = (c); i < _c; ++i)

const int N = 1e5 + 7;

using namespace std;

int B, n, D, M;

namespace B_1 {
    bool checkSubtask() {
        return B == 1;
    }

    int point[N];

    void solve() {
        FOR (i, 1, n) {
            cin >> point[i];
        }
        sort(point + 1, point + n + 1);
        int it = 1;
        long long res = 0;
        FOR (i, 1, n) {
            while (it <= n && point[it] - point[i] <= D) {
                ++it;
            }
            res += it - i - 1;
        }
        cout << res, el;
    }
}

struct FenwickTree {
    int n;
    vector<int> bit;

    FenwickTree(int _n = 0) {
        n = _n;
        bit.assign(_n + 7, 0);
    }

    void update(int u, int add) {
        for (; u <= n; u += u & -u) {
            bit[u] += add;
        }
    }

    int get(int u) {
        int res = 0;
        for (; u > 0; u -= u & -u) {
            res += bit[u];
        }
        return res;
    }
    int get(int l, int r) {
        return get(r) - get(l - 1);
    }
};

namespace B_2 {
    bool checkSubtask() {
        return B == 2;
    }

    pair<int, int> point[N];

    void solve() {
        FOR (i, 1, n) {
            int x, y;
            cin >> x >> y;
            point[i] = {x + y + M, x - y + M};
        }
        sort(point + 1, point + n + 1);

        FenwickTree fen(2 * M);

        int it = 1;
        long long res = 0;
        FOR (i, 1, n) {
            while (it <= n && point[it].first - point[i].first <= D) {
                fen.update(point[it].second, +1);
                ++it;
            }
            fen.update(point[i].second, -1);
            res += fen.get(point[i].second - D, min(point[i].second + D, 2 * M));
        }
        cout << res, el;
    }
}

namespace B_3 {
    bool checkSubtask() {
        return B == 3;
    }

    vector<pair<int, int>> point[77];

    long long countSingle(int idx) {
        FenwickTree fen(2 * M);
        int it = 0;
        long long res = 0;
        REP (i, point[idx].size()) {
            while (it < point[idx].size() &&
                   point[idx][it].first - point[idx][i].first <= D) {
                fen.update(point[idx][it].second, +1);
                ++it;
            }
            fen.update(point[idx][i].second, -1);
            res += fen.get(point[idx][i].second - D, min(point[idx][i].second + D, 2 * M));
        }
        return res;
    }

    long long countDouble(int idx_i, int idx_j) {
        FenwickTree fen(2 * M);
        int nD = D - abs(idx_j - idx_i);
        if (nD < 0) {
            return 0;
        }
        int sta = 0, fin = 0;
        long long res = 0;
        REP (i, point[idx_i].size()) {
            while (fin < point[idx_j].size() && /// point_i[i] < point_j[fin]
                   point[idx_j][fin].first - point[idx_i][i].first <= nD) {
                fen.update(point[idx_j][fin].second, +1);
                ++fin;
            }
            while (sta < fin && /// Lỗi 1: coi i là trung tâm sta, fin là 2 bên của j
                                /// nên ko được phép dùng abs của cả 2 phép trên
                                /// point_i[i] > point_j[sta]
                   point[idx_i][i].first - point[idx_j][sta].first > nD) {
                fen.update(point[idx_j][sta].second, -1);
                ++sta;
            }
            res += fen.get(point[idx_i][i].second - nD, min(point[idx_i][i].second + nD, 2 * M));
        }
        return res;
    }

    void solve() {
        FOR (i, 1, n) {
            int x, y, z;
            cin >> x >> y >> z;
            point[x].emplace_back(y + z + M, y - z + M);
        }

        long long res = 0;
        FOR (i, 1, M) if (!point[i].empty()) {
            sort(point[i].begin(), point[i].end());
            res += countSingle(i);
            FOR (j, 1, i - 1) if (!point[j].empty()) {
                res += countDouble(j, i);
            }
        }
        cout << res, el;
    }
}

int main() {

    #define name "art"
    if (fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> B >> n >> D >> M;

    if (B_1::checkSubtask()) return B_1::solve(), 0;
    if (B_2::checkSubtask()) return B_2::solve(), 0;
    if (B_3::checkSubtask()) return B_3::solve(), 0;

    return 0;
}

Compilation message (stderr)

pairs.cpp: In function 'int main()':
pairs.cpp:170:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...