Submission #1294210

#TimeUsernameProblemLanguageResultExecution timeMemory
1294210avighnaPairs (IOI07_pairs)C++20
52 / 100
4115 ms545164 KiB
#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> namespace algo { template <typename T, int d> class fenwick_tree; template <typename T> class fenwick_tree<T, 1> { private: std::size_t n; std::unordered_map<std::size_t, T> f; public: fenwick_tree(std::size_t n) : n(n) {} void apply(std::size_t i, T x) { for (++i; i <= n; i += i & -i) { f[i] = f[i] + x; } } T query(std::size_t i) { T ans = T{}; for (++i; i > 0; i -= i & -i) { ans = ans + f[i]; } return ans; } T query(std::size_t l, std::size_t r) requires requires(T a, T b) { a - b; } { return l == 0 ? query(r) : query(r) - query(l - 1); } }; } // namespace algo namespace algo { template <typename T, int d> class fenwick_tree { private: std::size_t n; std::vector<fenwick_tree<T, d - 1>> f; public: template <typename... ints> fenwick_tree(std::size_t n, ints... dims) : n(n), f(n + 1, fenwick_tree<T, d - 1>{dims...}) {} template <typename... ints> void apply(std::size_t i, ints... next) { for (++i; i <= n; i += i & -i) { f[i].apply(next...); } } template <typename... ints> requires(sizeof...(ints) == d - 1) T query(std::size_t i, ints... dims) { T ans = T{}; for (++i; i > 0; i -= i & -i) { ans = ans + f[i].query(dims...); } return ans; } template <typename... ints> T query(std::size_t l, std::size_t r, ints... dims) requires requires(T a, T b) { a - b; } { T ans = T{}; for (++r; r > 0; r -= r & -r) { ans = ans + f[r].query(dims...); } for (; l > 0; l -= l & -l) { ans = ans - f[l].query(dims...); } return ans; } }; }; // namespace algo using namespace std; using namespace algo; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int b, n, d, m; cin >> b >> n >> d >> m; m++; if (b == 1) { vector<int> a(n); for (int &i : a) { cin >> i; } sort(a.begin(), a.end()); int64_t ans = 0; for (int i = 0, j = 0; i < n; ++i) { while (j < n && a[j] - a[i] <= d) { j++; } ans += j - i - 1; } cout << ans << '\n'; return 0; } if (b == 2) { int64_t ans = 0; fenwick_tree<int, 2> fenwick(3 * m, 2 * m); for (int i = 0, u, v; i < n; ++i) { cin >> u >> v; int a = v + u + m, b = v - u + m; ans += fenwick.query(max(0, a - d), min(a + d, 3 * m - 1), max(0, b - d), min(b + d, 2 * m - 1)); fenwick.apply(a, b, 1); } cout << ans << '\n'; return 0; } }

Compilation message (stderr)

pairs.cpp: In instantiation of 'algo::fenwick_tree<T, d>::fenwick_tree(std::size_t, ints ...) [with ints = {int}; T = int; int d = 2; std::size_t = long unsigned int]':
pairs.cpp:105:46:   required from here
pairs.cpp:45:85: warning: narrowing conversion of 'dims#0' from 'int' to 'std::size_t' {aka 'long unsigned int'} [-Wnarrowing]
   45 |   fenwick_tree(std::size_t n, ints... dims) : n(n), f(n + 1, fenwick_tree<T, d - 1>{dims...}) {}
      |                                                                                     ^~~~
#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...