답안 #778381

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
778381 2023-07-10T09:00:43 Z fve5 Pairs (IOI07_pairs) C++17
100 / 100
134 ms 8764 KB
    #include <bits/stdc++.h>
    using namespace std;
     
    struct Fenwick {
        vector<int> arr;
        
        void add(int x, int d) {
            for (; x < arr.size(); x += x & -x)
                arr[x] += d;
        }
        
        int query(int x) {
            int ans = 0;
            for (; x; x -= x & -x)
                ans += arr[x];
            return ans;
        }
        
        Fenwick(int size) : arr(size) { }  
    };
     
    void _1D(int N, int D, int M) {
        vector<int> animals(N);
        for (auto &x: animals) cin >> x;
        sort(animals.begin(), animals.end());
        
        long long ans = 0;
        queue<int> in_range;
        for (auto x: animals) {
            while (!in_range.empty() && x - in_range.front() > D) {
                in_range.pop();
            }
            ans += in_range.size();
            in_range.push(x);
        }
        cout << ans << '\n';
    }
     
    void _2D(int N, int D, int M) {
        vector<pair<int, int>> animals(N);
        for (auto &[x, y]: animals) {
            int i, j; cin >> i >> j;
            x = i + j - 1;
            y = j - i + M;
        }
        
        sort(animals.begin(), animals.end());
        Fenwick fenwick(2 * M);
        
        long long ans = 0;
        queue<pair<int, int>> in_range;
        for (auto [x, y]: animals) {
            while (!in_range.empty() && x - in_range.front().first > D) {
                fenwick.add(in_range.front().second, -1);
                in_range.pop();
            }
            ans += fenwick.query(min(2 * M - 1, y + D)) - fenwick.query(max(0, y - D - 1));
            in_range.emplace(x, y);
            fenwick.add(y, 1);
        }
        cout << ans << '\n';
    }
     
    void _3D(int N, int D, int M) {
        vector<tuple<int, int, int>> animals(N);
        vector prefix_sums(M + 1, vector(2 * M, vector(2 * M, 0)));
        for (auto &[x, y, z]: animals) {
            int i, j, k; cin >> i >> j >> k;
            x = i + j - 1;
            y = j - i + M;
            z = k;
            
            prefix_sums[z][x][y]++;
        }
        
        for (int k = 1; k <= M; k++) {
            for (int i = 1; i < 2 * M; i++) {
                for (int j = 1; j < 2 * M; j++) {
                    prefix_sums[k][i][j] += prefix_sums[k][i - 1][j] + prefix_sums[k][i][j - 1] - prefix_sums[k][i - 1][j - 1];
                }
            }
        }
        
        long long ans = 0;
        for (auto [x, y, z]: animals) {
            for (int curr_z = max(1, z - D); curr_z <= min(M, z + D); curr_z++) {
                int off = abs(z - curr_z);
                ans += prefix_sums[curr_z][min(2 * M - 1, x + D - off)][min(2 * M - 1, y + D - off)]
                     - prefix_sums[curr_z][min(2 * M - 1, x + D - off)][max(0, y - D + off - 1)]
                     - prefix_sums[curr_z][max(0, x - D + off - 1)][min(2 * M - 1, y + D - off)]
                     + prefix_sums[curr_z][max(0, x - D + off - 1)][max(0, y - D + off - 1)];
            } 
        }
        cout << (ans - N) / 2 << endl;
    }
     
    int main() {
      	ios::sync_with_stdio(false); cin.tie(NULL);
        int B, N, D, M; cin >> B >> N >> D >> M;
        
        if (B == 1) _1D(N, min(D, B * M), M);
        if (B == 2) _2D(N, min(D, B * M), M);
        if (B == 3) _3D(N, min(D, B * M), M);
    }

Compilation message

pairs.cpp: In member function 'void Fenwick::add(int, int)':
pairs.cpp:8:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |             for (; x < arr.size(); x += x & -x)
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 724 KB Output is correct
2 Correct 13 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 596 KB Output is correct
2 Correct 15 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 724 KB Output is correct
2 Correct 14 ms 724 KB Output is correct
3 Correct 18 ms 728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 1108 KB Output is correct
2 Correct 19 ms 1884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 1108 KB Output is correct
2 Correct 29 ms 1564 KB Output is correct
3 Correct 22 ms 1504 KB Output is correct
4 Correct 28 ms 1368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 1628 KB Output is correct
2 Correct 26 ms 2020 KB Output is correct
3 Correct 26 ms 2204 KB Output is correct
4 Correct 30 ms 2008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7480 KB Output is correct
2 Correct 8 ms 7472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 1524 KB Output is correct
2 Correct 19 ms 1520 KB Output is correct
3 Correct 16 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5204 KB Output is correct
2 Correct 91 ms 5272 KB Output is correct
3 Correct 42 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 8744 KB Output is correct
2 Correct 134 ms 8752 KB Output is correct
3 Correct 58 ms 8764 KB Output is correct