#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
namespace sub1 {
const int maxN = 1e5 + 20;
int pos[maxN];
int N, D, M;
void solve() {
cin >> N >> D >> M;
for (int i = 1; i <= N; i++) {
cin >> pos[i];
}
sort(pos + 1, pos + N + 1);
long long res = 0;
for (int i = 1; i <= N; i++) {
res += upper_bound(pos + 1, pos + N + 1, pos[i] + D) - lower_bound(pos + 1, pos + N + 1, pos[i] - D);
}
res = (res - N) / 2;
cout << res;
}
}
namespace sub2 {
const int maxN = 1e5 + 20;
const int maxM = 7.5e4 + 20;
int bit[maxM * 2];
vector<pair<int, pair<int, int>>> events;
int N, D, M;
void add(int lx, int ly, int rx, int ry) {
lx = max(lx, 1);
ly = max(ly, 1);
rx = min(rx, M * 2);
ry = min(ry, M * 2);
events.emplace_back(rx, make_pair(2, ry));
events.emplace_back(rx, make_pair(3, ly - 1));
events.emplace_back(lx - 1, make_pair(3, ry));
events.emplace_back(lx - 1, make_pair(2, ly - 1));
}
void update(int pos) {
for (int i = pos; i <= M * 2; i += i & (-i)) {
bit[i]++;
}
}
int get(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & (-i)) {
res += bit[i];
}
return res;
}
void solve() {
cin >> N >> D >> M;
for (int i = 1; i <= N; i++) {
int X, Y;
cin >> X >> Y;
events.emplace_back(X - Y + M, make_pair(1, X + Y));
add(X - Y + M - D, X + Y - D, X - Y + M + D, X + Y + D);
}
sort(events.begin(), events.end());
long long res = 0;
for (auto e: events) {
int type = e.second.first;
int pos = e.second.second;
if (type == 1) {
update(pos);
}
if (type == 2) {
res += get(pos);
}
if (type == 3) {
res -= get(pos);
}
}
res = (res - N) / 2;
cout << res;
}
}
namespace sub3 {
const int maxN = 1e5 + 20;
const int maxM = 80;
vector<pair<int, int>> pos[maxM];
int pref[maxM * 2][maxM * 2];
int N, D, M;
void build(int Z) {
for (int i = 0; i <= M * 2; i++) {
for (int j = 0; j <= M * 2; j++) {
pref[i][j] = 0;
}
}
for (auto p: pos[Z]) {
pref[p.first][p.second]++;
}
for (int i = 1; i <= M * 2; i++) {
for (int j = 1; j <= M * 2; j++) {
pref[i][j] += pref[i][j - 1];
}
}
for (int i = 1; i <= M * 2; i++) {
for (int j = 1; j <= M * 2; j++) {
pref[i][j] += pref[i - 1][j];
}
}
}
int get(int lx, int ly, int rx, int ry) {
lx = max(lx, 1);
ly = max(ly, 1);
rx = min(rx, M * 2);
ry = min(ry, M * 2);
return pref[rx][ry] - pref[rx][ly - 1] - pref[lx - 1][ry] + pref[lx - 1][ly - 1];
}
void solve() {
cin >> N >> D >> M;
for (int i = 1; i <= N; i++) {
int X, Y, Z;
cin >> X >> Y >> Z;
pos[Z].emplace_back(X - Y + M, X + Y);
}
long long res = 0;
for (int i = 1; i <= M; i++) {
build(i);
for (int j = max(i - D, 1); j <= min(i + D, M); j++) {
for (auto p: pos[j]) {
res += get(p.first - D + abs(i - j), p.second - D + abs(i - j), p.first + D - abs(i - j), p.second + D - abs(i - j));
}
}
}
res = (res - N) / 2;
cout << res;
}
}
void just_do_it() {
int B;
cin >> B;
if (B == 1) {
sub1::solve();
}
if (B == 2) {
sub2::solve();
}
if (B == 3) {
sub3::solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
612 KB |
Output is correct |
2 |
Correct |
17 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
716 KB |
Output is correct |
2 |
Correct |
30 ms |
708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
712 KB |
Output is correct |
2 |
Correct |
24 ms |
664 KB |
Output is correct |
3 |
Correct |
21 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
7084 KB |
Output is correct |
2 |
Correct |
54 ms |
7012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
7108 KB |
Output is correct |
2 |
Correct |
98 ms |
7148 KB |
Output is correct |
3 |
Correct |
93 ms |
7148 KB |
Output is correct |
4 |
Correct |
80 ms |
7236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
7852 KB |
Output is correct |
2 |
Correct |
103 ms |
7984 KB |
Output is correct |
3 |
Correct |
89 ms |
7968 KB |
Output is correct |
4 |
Correct |
89 ms |
7952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
444 KB |
Output is correct |
2 |
Correct |
7 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1536 KB |
Output is correct |
2 |
Correct |
29 ms |
1540 KB |
Output is correct |
3 |
Correct |
23 ms |
1556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
1492 KB |
Output is correct |
2 |
Correct |
60 ms |
1540 KB |
Output is correct |
3 |
Correct |
58 ms |
1544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
1620 KB |
Output is correct |
2 |
Correct |
71 ms |
1636 KB |
Output is correct |
3 |
Correct |
64 ms |
1720 KB |
Output is correct |