답안 #674971

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674971 2022-12-26T17:52:19 Z kirakaminski968 Pairs (IOI07_pairs) C++17
100 / 100
298 ms 61928 KB
#include <bits/stdc++.h>
using namespace std; 

#define int long long
#define ld long double
#define ar array
 
const int INF = 1e17;
const int MOD = 998244353;

template<class T, int... Ns> struct BIT {
    T val = 0;
    void upd(T v) { val += v; }
    T query() { return val; }  
};
template<class T, int N, int... Ns> struct BIT<T, N, Ns...> {
    BIT<T, Ns...> bit[N+1];
    template<typename... Args> void upd(int pos, Args... args) {
        for (; pos <= N; pos += pos&-pos) bit[pos].upd(args...);
    }
    template<typename... Args> T sum(int pos, Args... args) {
        T res = 0; for (; pos > 0; pos -= pos&-pos) res += bit[pos].query(args...);
        return res;
    }
    template<typename... Args> T query(int l, int r, Args... args) {
        return sum(r, args...) - sum(l-1, args...);
    }
};

BIT<int, 301, 301, 301> bit2;
int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int b, n, d, m; cin>>b>>n>>d>>m;
    int ans = 0;
    if (b == 1) {
        vector<int> a(n); for (int &i : a) cin>>i;
        sort(a.begin(), a.end());
        int lo = 0, hi = 0;
        for (int i = 0; i < n; i++) {
            while (hi + 1 < n && a[hi+1] - a[i] <= d) hi++;
            while (lo < n && a[i] - a[lo] > d) lo++;
            ans += hi - lo;
        }
    } else if (b == 2) {
        vector<ar<int,2>> a(n);
        for (int i = 0; i < n; i++) {
            int x, y; cin>>x>>y;
            a[i] = {x+y, x-y};
        }
        BIT<int, 150001> bit;
        sort(a.begin(), a.end());
        int lo = 0, hi = -1; 
        for (int i = 0; i < n; i++) {
            while (hi + 1 < n && a[hi+1][0] - a[i][0] <= d) bit.upd(a[++hi][1] + m+1, 1);
            while (lo < n && a[i][0] - a[lo][0] > d) bit.upd(a[lo++][1] + m+1, -1);
            ans +=  bit.query(max(1LL, a[i][1] + m+1 - d), min(a[i][1] + m+1 + d, 2*m+1)) - 1;
        }
    } else {
        vector<ar<int,4>> a(n);
        for (int i = 0; i < n; i++) {
            int x, y, z; cin>>x>>y>>z;
            a[i] = {x+y+z, x+y-z, x-y+z, x-y-z}; 
        }
        sort(a.begin(), a.end());
        int lo = 0, hi = -1;
        for (int i = 0; i < n; i++) {
            while (hi + 1 < n && a[hi+1][0] - a[i][0] <= d) {
                ++hi;
                bit2.upd(a[hi][1] + 2*m+1, a[hi][2] + 2*m+1, a[hi][3] + 2*m+1, 1);
            }
            while (lo < n && a[i][0] - a[lo][0] > d) {
                bit2.upd(a[lo][1] + 2*m+1, a[lo][2] + 2*m+1, a[lo][3] + 2*m+1, -1);
                lo++;
            }
            ans +=  bit2.query(max(1LL, a[i][1] + 2*m+1 - d), min(a[i][1] + 2*m+1 + d, 4*m+1),
                max(1LL, a[i][2] + 2*m+1 - d), min(a[i][2] + 2*m+1 + d, 4*m+1),
                max(1LL, a[i][3] + 2*m+1 - d), min(a[i][3] + 2*m+1 + d, 4*m+1)) - 1;
        }
    }
    cout << ans/2 << endl;
} 
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2572 KB Output is correct
2 Correct 13 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3028 KB Output is correct
2 Correct 17 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 3044 KB Output is correct
2 Correct 18 ms 3048 KB Output is correct
3 Correct 19 ms 3056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 2 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 3540 KB Output is correct
2 Correct 28 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 3832 KB Output is correct
2 Correct 30 ms 3828 KB Output is correct
3 Correct 30 ms 3828 KB Output is correct
4 Correct 30 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 4200 KB Output is correct
2 Correct 34 ms 4180 KB Output is correct
3 Correct 35 ms 4092 KB Output is correct
4 Correct 35 ms 4064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 22756 KB Output is correct
2 Correct 10 ms 22880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 7420 KB Output is correct
2 Correct 99 ms 7372 KB Output is correct
3 Correct 68 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 43916 KB Output is correct
2 Correct 263 ms 44008 KB Output is correct
3 Correct 103 ms 43876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 61928 KB Output is correct
2 Correct 266 ms 61888 KB Output is correct
3 Correct 139 ms 61908 KB Output is correct