#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pint = pair <int, int>;
using tint = array <int, 3>;
int b, n, d, m;
LL ans = 0;
void calc1()
{
vector <int> A(n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
sort(A.begin(), A.end());
for (int i = 1, j = 0; i < n; i++){
while (A[i] - A[j] > d) j++;
ans += i - j;
}
}
struct seg1D {
int node[800000], nn = 160000;
void update(int x, int val){
for (x += nn - 1; x > 0; x /= 2) node[x] += val;
}
int query(int L, int R){
int ret = 0;
for (L += nn - 1, R += nn - 1; L <= R; L /= 2, R /= 2){
if (L % 2 == 1) ret += node[L++];
if (R % 2 == 0) ret += node[R--];
}
return ret;
}
};
void calc2()
{
vector <pint> A(n);
for (int i = 0, x, y; i < n; i++){
scanf("%d %d", &x, &y);
A[i] = {x + y, x - y + 75000};
}
sort(A.begin(), A.end());
seg1D tree;
tree.update(A[0].second, 1);
for (int i = 1, j = 0; i < n; i++){
while (A[i].first - A[j].first > d){
tree.update(A[j].second, -1);
j++;
}
ans += tree.query(max(A[i].second - d, 1), min(A[i].second + d, 160000));
tree.update(A[i].second, 1);
}
}
int s[80][160][160];
void calc3()
{
vector <tint> A(n);
for (int i = 0, x, y, z; i < n; i++){
scanf("%d %d %d", &x, &y, &z);
s[z][x + y][x - y + 80]++;
A[i] = {x, y, z};
}
for (int z = 0; z <= 75; z++){
for (int i = 1; i < 160; i++){
for (int j = 1; j < 160; j++){
s[z][i][j] += s[z][i - 1][j] + s[z][i][j - 1] - s[z][i - 1][j - 1];
}
}
}
for (int i = 0, d1, d2, az; i < n; i++){
d1 = A[i][0] + A[i][1], d2 = A[i][0] - A[i][1] + 80, az = A[i][2];
for (int j = 0; j <= 75; j++){
int dist = d - abs(az - j);
if (dist < 0) continue;
ans += s[j][min(dist + d1, 159)][min(dist + d2, 159)];
ans -= s[j][min(dist + d1, 159)][max(-dist + d2 - 1, 0)];
ans -= s[j][max(-dist + d1 - 1, 0)][min(dist + d2, 159)];
ans += s[j][max(-dist + d1 - 1, 0)][max(-dist + d2 - 1, 0)];
}
ans -= 1;
}
ans /= 2;
}
int main()
{
scanf("%d %d %d %d", &b, &n, &d, &m);
if (b == 1) calc1();
if (b == 2) calc2();
if (b == 3) calc3();
printf("%lld\n", ans);
return 0;
}
Compilation message
pairs.cpp: In function 'void calc1()':
pairs.cpp:12:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | for (int i = 0; i < n; i++) scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
pairs.cpp: In function 'void calc2()':
pairs.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
pairs.cpp: In function 'void calc3()':
pairs.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp: In function 'int main()':
pairs.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%d %d %d %d", &b, &n, &d, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
596 KB |
Output is correct |
2 |
Correct |
11 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
596 KB |
Output is correct |
2 |
Correct |
14 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
596 KB |
Output is correct |
2 |
Correct |
14 ms |
596 KB |
Output is correct |
3 |
Correct |
14 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
4180 KB |
Output is correct |
2 |
Correct |
23 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
31 ms |
4180 KB |
Output is correct |
3 |
Correct |
29 ms |
4180 KB |
Output is correct |
4 |
Correct |
31 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4180 KB |
Output is correct |
2 |
Correct |
36 ms |
4180 KB |
Output is correct |
3 |
Correct |
38 ms |
5244 KB |
Output is correct |
4 |
Correct |
38 ms |
5240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7904 KB |
Output is correct |
2 |
Correct |
9 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
9044 KB |
Output is correct |
2 |
Correct |
41 ms |
9032 KB |
Output is correct |
3 |
Correct |
51 ms |
9072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
9036 KB |
Output is correct |
2 |
Correct |
123 ms |
9080 KB |
Output is correct |
3 |
Correct |
88 ms |
9040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
9064 KB |
Output is correct |
2 |
Correct |
131 ms |
8992 KB |
Output is correct |
3 |
Correct |
84 ms |
9044 KB |
Output is correct |