#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl '\n'
#define ll long long
#define int long long
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const ll INF=1e18,mod=1e9+9,N=1e5+5;
template<class T, int... Ns> struct BIT {
T val = 0;
void upd(T v) { val += v; }
T query() { return val; }
};
template<class T, int N, int... Ns> struct BIT<T, N, Ns...> {
BIT<T, Ns...> bit[N+1];
template<typename... Args> void upd(int pos, Args... args) {
for (; pos <= N; pos += pos&-pos) bit[pos].upd(args...);
}
template<typename... Args> T sum(int pos, Args... args) {
T res = 0; for (; pos > 0; pos -= pos&-pos) res += bit[pos].query(args...);
return res;
}
template<typename... Args> T query(int l, int r, Args... args) {
return sum(r, args...) - sum(l-1, args...);
}
};
struct triple{
int x,y,z;
};
void solve(){
int b,n,d,m;
cin>>b>>n>>d>>m;
if(b==1){
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
sort(all(a));
int ans=0;
for(int i=0;i<n;i++){
int j=upper_bound(all(a),a[i]+d)-a.begin();
ans+=j-i-1;
}
cout<<ans<<endl;
}
else if(b==2){
vector<pair<int,int>> a(n);
for(int i=0;i<n;i++){
int x,y;cin>>x>>y;
a[i]={x+y,x-y};
}
sort(all(a));
int ans=0;
BIT<int,150005> bit;
int l=0,r=-1;
for(int i=0;i<n;i++){
while(r+1<n && a[r+1].fr-a[i].fr<=d)
bit.upd(a[++r].sc+m+1,1);
while(l<n && a[i].fr-a[l].fr>d)
bit.upd(a[l++].sc+m+1,-1);
ans+=bit.query(max(1ll,a[i].sc+m+1-d),min(2*m+1,a[i].sc+m+1+d))-1;
}
cout<<ans/2<<endl;
}
else{
vector<triple> a(n);
for(int i=0;i<n;i++){
cin>>a[i].x>>a[i].y>>a[i].z;
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y)+abs(a[i].z-a[j].z)<=d) ans++;
}
}
cout<<ans<<endl;
}
}
main(){
//fopn("team");
ios;
int T=1;
//cin>>T;
for(int i=1;i<=T;i++){
//cout<<"Case #"<<i<<": ";
solve();
}
}
Compilation message
pairs.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main(){
| ^~~~
pairs.cpp: In function 'void fopn(std::string)':
pairs.cpp:13:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:14:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2388 KB |
Output is correct |
2 |
Correct |
14 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2416 KB |
Output is correct |
2 |
Correct |
18 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2388 KB |
Output is correct |
2 |
Correct |
20 ms |
2400 KB |
Output is correct |
3 |
Correct |
18 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1492 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
3540 KB |
Output is correct |
2 |
Correct |
25 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
3744 KB |
Output is correct |
2 |
Correct |
29 ms |
3824 KB |
Output is correct |
3 |
Correct |
28 ms |
3800 KB |
Output is correct |
4 |
Correct |
28 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4052 KB |
Output is correct |
2 |
Correct |
36 ms |
4180 KB |
Output is correct |
3 |
Correct |
34 ms |
4072 KB |
Output is correct |
4 |
Correct |
31 ms |
4076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1528 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4018 ms |
4344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4038 ms |
4564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4042 ms |
4564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |