This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |