#include <algorithm>
#include <iostream>
#include <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::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, 3 * 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, 3 * m - 1));
fenwick.apply(a, b, 1);
}
cout << ans << '\n';
return 0;
}
}
컴파일 시 표준 에러 (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 time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |