#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
#define int long long
using namespace __gnu_pbds;
typedef tree<pair<pair<int,int>,int>,null_type,less<pair<pair<int,int>,int> >,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
ordered_set bit[75010]; // 2D bit in regular matrices but only supporting single add/del
int id = 0;
map<pair<int, int>,vector<int> > mp;
void insert(int x, int y) {
id++;
mp[{y, x}].push_back(id);
for(;x<75010;x+=x&-x) bit[x].insert({{y, x}, id});
}
void erase(int x, int y) {
int ono = mp[{y, x}].back(); mp[{y, x}].pop_back();
for(;x<75010;x+=x&-x) bit[x].erase({{y, x}, ono});
}
int query(int x, int y) {
int sum = 0;
for(;x;x-=x&-x) sum += bit[x].order_of_key({{y+1, 0}, 0});
return sum;
}
bool cmp1(pair<int, int> x, pair<int, int> y) {
return x.first - x.second < y.first - y.second;
}
bool cmp2(pair<int, int> x, pair<int, int> y) {
return x.first + x.second < y.first + y.second;
}
void solve(int tc) {
int b, n, d, m;
cin >> b >> n >> d >> m;
if(b == 1) {
int a[n+1];
for(int i=1; i<=n; i++) cin >> a[i];
sort(a+1, a+1+n);
int ans = 0;
for(int i=1; i<n; i++) {
int lb = i+1, rb = n;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(a[mid] - a[i] <= d) lb = mid;
else rb = mid - 1;
}
if(a[lb] - a[i] <= d) ans += lb - i;
}
cout << ans << "\n";
return;
}
if(b == 2) {
int ans = 0;
pair<int, int> p[n+1];
for(int i=1; i<=n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p+1, p+1+n, cmp1);
int j = 1;
for(int i=1; i<=n; i++) {
while(j < i && (p[i].first - p[i].second) - (p[j].first - p[j].second) > d) {
erase(p[j].first, 75001-p[j].second);
j++;
}
ans += query(p[i].first, 75001-p[i].second);
insert(p[i].first, 75001-p[i].second);
}
for(int i=j; i<=n; i++) erase(p[i].first, 75001-p[i].second);
j = 1;
sort(p+1, p+1+n, cmp2);
for(int i=1; i<=n; i++) {
while(j < i && (p[i].first + p[i].second) - (p[j].first + p[j].second) > d) {
erase(p[j].first, p[j].second);
j++;
}
ans += query(p[i].first, p[i].second);
insert(p[i].first, p[i].second);
}
vector<int> c[75001];
for(int i=1; i<=n; i++) c[p[i].second].push_back(p[i].first);
for(int i=1; i<=75000; i++) sort(c[i].begin(), c[i].end());
for(int i=1; i<=75000; i++) {
for(int j=0; j+1<c[i].size(); j++) {
int lb = j+1, rb = c[i].size() - 1;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(c[i][mid] - c[i][j] <= d) lb = mid;
else rb = mid-1;
}
if(c[i][lb] - c[i][j] <= d) ans -= (lb - j);
}
}
for(int i=1; i<=75000; i++) c[i].clear();
for(int i=1; i<=n; i++) c[p[i].first].push_back(p[i].second);
for(int i=1; i<=75000; i++) sort(c[i].begin(), c[i].end());
for(int i=1; i<=75000; i++) {
for(int j=0; j+1<c[i].size(); j++) {
int lb = j+1, rb = c[i].size() - 1;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(c[i][mid] - c[i][j] <= d) lb = mid;
else rb = mid-1;
}
if(c[i][lb] - c[i][j] <= d) ans -= (lb - j);
}
}
for(int i=1; i<=75000; i++) c[i].clear();
map<pair<int,int>,int> mp;
for(int i=1; i<=n; i++) {
ans += mp[p[i]];
mp[p[i]]++;
}
cout << ans << "\n"; return;
}
if(b == 3) {
int x[n+1], y[n+1], z[n+1];
for(int i=1; i<=n; i++) cin >> x[i] >> y[i] >> z[i];
int ans = 0;
for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++) ans += (abs(x[i] - x[j]) + abs(y[i] - y[j]) + abs(z[i] - z[j]) <= d);
cout << ans << "\n"; return;
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message
pairs.cpp: In function 'void solve(long long int)':
pairs.cpp:103:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int j=0; j+1<c[i].size(); j++) {
| ~~~^~~~~~~~~~~~
pairs.cpp:117:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int j=0; j+1<c[i].size(); j++) {
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10196 KB |
Output is correct |
2 |
Correct |
7 ms |
10228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
11084 KB |
Output is correct |
2 |
Correct |
20 ms |
11088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
10988 KB |
Output is correct |
2 |
Correct |
23 ms |
10964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
11084 KB |
Output is correct |
2 |
Correct |
27 ms |
11064 KB |
Output is correct |
3 |
Correct |
28 ms |
11084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10580 KB |
Output is correct |
2 |
Correct |
13 ms |
10676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2047 ms |
22560 KB |
Output is correct |
2 |
Correct |
1980 ms |
120108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2394 ms |
42240 KB |
Output is correct |
2 |
Correct |
3493 ms |
83696 KB |
Output is correct |
3 |
Correct |
2783 ms |
80444 KB |
Output is correct |
4 |
Correct |
2687 ms |
70992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1884 ms |
43340 KB |
Output is correct |
2 |
Correct |
2919 ms |
69848 KB |
Output is correct |
3 |
Correct |
1427 ms |
84244 KB |
Output is correct |
4 |
Correct |
1513 ms |
65516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10260 KB |
Output is correct |
2 |
Correct |
8 ms |
10324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4011 ms |
12572 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4045 ms |
12628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4037 ms |
12628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |