#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define int long long
const int N=3e5+5;
void sbtsk1(int N, int D , int M){
vector<int>v;
for(int i=0;i<N;i++){
int a;
cin>>a;
v.pb(a);
}
sort(v.begin(),v.end());
int cnt=0;
for(auto i=v.begin();i!=v.end();i++){
int a=*i;
auto it= upper_bound(v.begin(),v.end(),a-D-1);
cnt+= i-it;
}
cout<<cnt;
}
void sbtsk2(int N, int D , int M){
vector<pair<int,int>>v;
vector<int>vx[M+23];
vector<int>vy[M+23];
for(int i=0;i<N;i++){
int x,y;
cin>>x>>y;
vx[x].pb(y);
vy[y].pb(x);
v.pb({x,y});
}
for(int i=0;i<=M;i++){
sort(vx[i].begin(),vx[i].end());
}
int cnt=0;
for(int i=0;i<N;i++){
int x=v[i].ff;
int y=v[i].ss;
int p=0;
for(int x1=0;x1<=M;x1++){
if(abs(x1-x)>D)continue;
if(vx[x1].empty())continue;
int k=D-abs(x-x1);
auto lo=upper_bound(vx[x1].begin(),vx[x1].end(),y-k-1);
auto hi=upper_bound(vx[x1].begin(),vx[x1].end(),k+y);
cnt+=hi-lo;
}
cnt--;
}
cout<<cnt/2;
}
void sbtsk3(int N, int D, int M){
vector<pair<pair<int,int>,int>>v;
for(int i=0;i<N;i++){
int x,y,z;
cin>>x>>y>>z;
v.pb({{x,y},z});
}
int cnt=0;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
int d=abs(v[i].ff.ff-v[j].ff.ff)+abs(v[i].ff.ss-v[j].ff.ss)+abs(v[i].ss-v[j].ss);
if(d<=D)cnt++;
}
}
cout<<cnt;
}
signed main(){
int B,N,D,M;
cin>>B>>N>>D>>M;
if(B==1){
sbtsk1(N,D,M);
return 0;
}
if(B==2){
sbtsk2(N,D,M);
return 0;
}
sbtsk3(N,D,M);
}
Compilation message
pairs.cpp: In function 'void sbtsk2(long long int, long long int, long long int)':
pairs.cpp:43:7: warning: unused variable 'p' [-Wunused-variable]
43 | int p=0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1352 KB |
Output is correct |
2 |
Correct |
23 ms |
1488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
1484 KB |
Output is correct |
2 |
Correct |
38 ms |
1488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
1488 KB |
Output is correct |
2 |
Correct |
31 ms |
1492 KB |
Output is correct |
3 |
Correct |
31 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
3932 KB |
Output is correct |
2 |
Correct |
85 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
4300 KB |
Output is correct |
2 |
Correct |
220 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1141 ms |
4296 KB |
Output is correct |
2 |
Execution timed out |
4040 ms |
5068 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4034 ms |
8896 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4050 ms |
3536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4062 ms |
3536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4065 ms |
4792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |