Submission #550777

#TimeUsernameProblemLanguageResultExecution timeMemory
550777JomnoiPairs (IOI07_pairs)C++17
37 / 100
45 ms4856 KiB
#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(y == o.y) { return t < o.t; } return y < o.y; } 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 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...