#include<bits/stdc++.h>
#define FileName "test"
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
struct OneD {
int n, d, m; ll res = 0;
void Solve() {
cin >> n >> d >> m;
vector<int> v(n);
for(int& x: v) cin >> x;
sort(v.begin(), v.end());
for(int& x: v) res += upper_bound(v.begin(), v.end(), x + d) - lower_bound(v.begin(), v.end(), x - d);
cout << (res - n) / 2;
}
};
struct TwoD {
#define N 75000
#define M (N << 1)
int n, m, d, x, y, t[M];
vector<pii> v;
ll res;
void Update(int x, int val) {for(; x < M; x += (x & -x)) t[x] += val;}
int Get(int x) {int res = 0; for(; x > 0; x -= (x & -x)) res += t[x]; return res;}
void Solve() {
cin >> n >> d >> m; v.resize(n);
for(int i = 0; i < n; ++i) {
cin >> x >> y;
v[i].fi = x + y;
v[i].se = x - y + N;
}
sort(v.begin(), v.end());
int j = 0; res = 0;
for(int i = 0; i < n; ++i) {
while(v[i].fi - v[j].fi > d && j < i) Update(v[j].se, -1), ++j;
res += Get(min(v[i].se + d, M - 1)) - Get(v[i].se - d - 1);
Update(v[i].se, 1);
}
cout << res;
}
#undef N
#undef M
};
struct ThrD {
#define N 80
int cnt[N][N][N], D, xD;
int x, y, z, n, m, d;
vector<pair<pii, int>> v;
void Solve() {
cin >> n >> d >> m; v.resize(n);
for(int i = 0; i < n; ++i) {
cin >> x >> y >> z;
v[i] = mp(mp(x, y), z);
++cnt[x][y][z];
}
for(int i = 1; i <= 75; ++i) {
for(int j = 1; j <= 75; ++j)
for(int k = 1; k <= 75; ++k)
cnt[i][j][k] += cnt[i][j][k - 1];
}
ll res = 0;
for(int i = 0; i < n; ++i) {
for(int j = max(v[i].fi.fi - d, 1), limj = min(v[i].fi.fi + d, 75); j <= limj; ++j) {
D = d - abs(v[i].fi.fi - j);
for(int k = max(v[i].fi.se - D, 1), limk = min(v[i].fi.se + D, 75); k <= limk; ++k) {
xD = D - abs(v[i].fi.se - k);
res += cnt[j][k][min(v[i].se + xD, 75)] - cnt[j][k][max(v[i].se - xD - 1, 0)];
}
}
}
cout << (res - n) / 2;
}
#undef N
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(FileName".inp", "r")) {
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
}
int B; cin >> B;
if(B == 1) {
OneD one;
one.Solve();
} else if(B == 2) {
TwoD two;
two.Solve();
} else {
ThrD thr;
thr.Solve();
}
}
Compilation message
pairs.cpp: In function 'int main()':
pairs.cpp:89:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:90:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
760 KB |
Output is correct |
2 |
Correct |
21 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
760 KB |
Output is correct |
2 |
Correct |
28 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
760 KB |
Output is correct |
2 |
Correct |
30 ms |
760 KB |
Output is correct |
3 |
Correct |
28 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
1144 KB |
Output is correct |
2 |
Correct |
32 ms |
1188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
1144 KB |
Output is correct |
2 |
Correct |
39 ms |
1192 KB |
Output is correct |
3 |
Correct |
39 ms |
1144 KB |
Output is correct |
4 |
Correct |
38 ms |
1148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
1784 KB |
Output is correct |
2 |
Correct |
43 ms |
1656 KB |
Output is correct |
3 |
Correct |
43 ms |
1656 KB |
Output is correct |
4 |
Correct |
42 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2296 KB |
Output is correct |
2 |
Correct |
45 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
3480 KB |
Output is correct |
2 |
Correct |
96 ms |
3420 KB |
Output is correct |
3 |
Correct |
322 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
3324 KB |
Output is correct |
2 |
Correct |
2712 ms |
3448 KB |
Output is correct |
3 |
Execution timed out |
4025 ms |
3448 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1201 ms |
3448 KB |
Output is correct |
2 |
Correct |
3768 ms |
3448 KB |
Output is correct |
3 |
Execution timed out |
4050 ms |
3448 KB |
Time limit exceeded |