#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
void solve1() {
int n, d, m;
cin >> n >> d >> m;
vector<int> a(n);
for (int &i : a) cin >> i;
sort(all(a));
long long ans = 0;
for (int l = 0, r = 0; r < n; r++) {
while (a[r] - a[l] > d) l++;
ans += r - l;
}
cout << ans;
}
struct FakeFenwick {
vector<vector<int>> fw, val;
int n;
FakeFenwick() {}
FakeFenwick(int n): n(n), val(n + 1, vector<int>()), fw(n + 1) {}
bool iscc = 0;
void fakeU(int x, int y) {
iscc = 0;
for (; x <= n; x += x & -x) val[x].push_back(y);
}
void cc() {
if (iscc) return;
for (int x = 1; x <= n; x++) {
sort(all(val[x]));
val[x].erase(unique(all(val[x])), val[x].end());
fw[x].resize(val[x].size() + 1);
}
iscc = 1;
}
void update(int x, int y, int v) {
assert(iscc);
for (; x <= n; x += x & -x) {
int yy = upper_bound(all(val[x]), y) - val[x].begin();
for (; yy <= val[x].size(); yy += yy & -yy) {
fw[x][yy] += v;
}
}
}
int get(int x, int y) {
assert(iscc);
int res = 0;
for (; x; x -= x & -x) {
int yy = upper_bound(all(val[x]), y) - val[x].begin();
for (; yy; yy -= yy & -yy) {
res += fw[x][yy];
}
}
return res;
}
int get(int x1, int y1, int x2, int y2)
{
return get(x2, y2) - get(x2, y1 - 1) - get(x1 - 1, y2) + get(x1 - 1, y1 - 1);
}
};
void solve2() {
// xi - xj + |yi - yj| <= d
// xi + yi <= d + xj + yj if yi >= yj
// xi - yi <= d + xj - yj if yi < yj
int n, m, d;
cin >> n >> d >> m;
vector<pair<int, int>> a(n);
for (auto &[x, y] : a) cin >> x >> y;
sort(all(a));
FakeFenwick t1(m), t2(m);
for (auto [x, y] : a) {
t1.fakeU(y, d + x + y);
t2.fakeU(y, d + x - y);
}
t1.cc();
t2.cc();
long long ans = 0;
for (auto [x, y] : a) {
ans += t1.get(1, x + y, y, d + m + m) + t2.get(y + 1, x - y, m, d + m + m);
t1.update(y, d + x + y, 1);
t2.update(y, d + x - y, 1);
}
cout << ans;
}
void solve3() {
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int type;
cin >> type;
if (type == 1) solve1();
else if (type == 2) solve2();
else solve3();
return 0;
}
| # | 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... |