Submission #550778

# Submission time Handle Problem Language Result Execution time Memory
550778 2022-04-19T07:42:45 Z Jomnoi Pairs (IOI07_pairs) C++17
60 / 100
50 ms 6080 KB
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;

// B = 1

long long solve1(int N, int D, int M) {
    vector <int> X(N + 1);
    for(int i = 1; i <= N; i++) {
        cin >> X[i];
    }

    sort(X.begin() + 1, X.end());

    long long ans = 0;
    for(int l = 1, r = 1; r <= N; r++) {
        while(l < r and X[r] - X[l] > D) {
            l++;
        }
        ans += r - l;
    }
    return ans;
}

// B = 2

class FenwickTree {
private :
    int N;
    vector <int> fenwick;
public :
    FenwickTree() {}
    FenwickTree(const int &n) : N(n), fenwick(N + 1, 0) {}

    void add(const int &idx, const int &v) {
        for(int i = idx; i <= N; i += (i & -i)) {
            fenwick[i] += v;
        }
    }

    int get(const int &idx) {
        int res = 0;
        for(int i = idx; i >= 1; i -= (i & -i)) {
            res += fenwick[i];
        }
        return res;
    }
};

class Query {
public :
    int x, y, t;
    Query() {}
    Query(const int &x_, const int &y_, const int &t_) : x(x_), y(y_), t(t_) {}

    bool operator<(const Query &o) const {
        if(x == o.x) {
            if(t == o.t) {
                return y < o.y;
            }
            return t < o.t;
        }
        return x < o.x;
    }  
};

long long solve2(int N, int D, int M) {
    FenwickTree fw(2 * M);
    vector <int> X(N + 1, 0), Y(N + 1, 0);
    for(int i = 1; i <= N; i++) {
        cin >> X[i] >> Y[i];
    }

    vector <Query> query;
    for(int i = 1; i <= N; i++) {
        query.push_back(Query(X[i] - Y[i], X[i] + Y[i], 1));
        query.push_back(Query(X[i] - Y[i] + D + 1, X[i] + Y[i], -1));
    }

    sort(query.begin(), query.end());

    long long ans = 0;
    for(auto [x, y, t] : query) {
        if(t == -1) {
            fw.add(y, -1);
        }
        else if(t == 1) {
            ans += fw.get(min(2 * M, y + D)) - fw.get(y - D - 1);
            fw.add(y, 1);
        }
    }
    return ans;
}

// B = 3

long long solve3(int N, int D, int M) {
    return -1;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int B, N, D, M;
    cin >> B >> N >> D >> M;
    if(B == 1) {
        cout << solve1(N, D, M);
    }
    else if(B == 2) {
        cout << solve2(N, D, M);
    }
    else if(B == 3) {
        cout << solve3(N, D, M);
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 724 KB Output is correct
2 Correct 13 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 596 KB Output is correct
2 Correct 21 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 596 KB Output is correct
2 Correct 20 ms 596 KB Output is correct
3 Correct 17 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 4308 KB Output is correct
2 Correct 39 ms 4800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 4296 KB Output is correct
2 Correct 44 ms 5056 KB Output is correct
3 Correct 43 ms 5056 KB Output is correct
4 Correct 42 ms 5060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 4852 KB Output is correct
2 Correct 50 ms 6012 KB Output is correct
3 Correct 49 ms 6080 KB Output is correct
4 Correct 50 ms 5968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -