/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
inline int dis(vector<int>&a,vector<int>&b)
{
int z=0;
for (int j=0;j<b.size();j++)
z+=abs(a[j]-b[j]);
return z;
}
inline string er(vector<int>&a)
{
string z;
for (auto i:a)
z+=to_string(i)+' ';
return z;
}
inline void solve()
{
long long B,N,D,M;
cin>>B>>N>>D>>M;
int a[N][B];
vector<int>mi(B,1e9+10),ma(B,0);
// map<int,int>d;
for (int i=0;i<N;i++)
{
for (int j=0;j<B;j++)
cin>>a[i][j];
for (int j=0;j<B;j++)
{
mi[j]=min(mi[j],a[i][j]);
ma[j]=max(ma[j],a[i][j]);
}
}
if (dis(mi,ma)<=D)
{
cout<<(N*(N-1))/2<<endl;
return;
}
if (B==1)
{
vector<int>z;
for (int i=0;i<N;i++)
z.push_back(a[i][0]);
sort(begin(z),end(z));
int ans=0;
for (int i=0;i<N;i++)
{
int x=upper_bound(begin(z),end(z),z[i]+D)-begin(z);
ans+=x-i-1;
}
cout<<ans<<endl;return;
}
M=min(M,D);
int g=1;
for (int i=0;i<B;i++)
g*=M;
int ans=0;
set<vector<int>>s;
vector<vector<int>>z;
for (int i=0;i<N;i++)
{
vector<int>g;
for (int j=0;j<B;j++)
g.push_back(a[i][j]);
s.insert(g);
z.push_back(g);
}
sort(begin(z),end(z));
int f=0;
int d[s.size()+10]={};
d[0]=1;
for (int i=1;i<z.size();i++)
{
if (z[i]==z[i-1])
d[f]++;
else
{
f++;
d[f]=1;
}
}
z={begin(s),end(s)};
for (int i=0;i<z.size();i++)
{
int g=d[i];
ans+=(g*(g-1))/2;
for (int j=i+1;j<z.size();j++)
{
int di=0;
for (int k=0;k<B;k++)
di+=abs(z[i][k]-z[j][k]);
if (di<=D)
ans+=d[i]*d[j];
}
}
cout<<ans<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
Compilation message
pairs.cpp: In function 'long long int dis(std::vector<long long int>&, std::vector<long long int>&)':
pairs.cpp:19:17: 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]
19 | for (int j=0;j<b.size();j++)
| ~^~~~~~~~~
pairs.cpp: In function 'void solve()':
pairs.cpp:85:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int i=1;i<z.size();i++)
| ~^~~~~~~~~
pairs.cpp:96:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i=0;i<z.size();i++)
| ~^~~~~~~~~
pairs.cpp:100:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int j=i+1;j<z.size();j++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2780 KB |
Output is correct |
2 |
Correct |
15 ms |
2780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
3152 KB |
Output is correct |
2 |
Correct |
18 ms |
3036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
3036 KB |
Output is correct |
2 |
Correct |
20 ms |
3032 KB |
Output is correct |
3 |
Correct |
18 ms |
3004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
9472 KB |
Output is correct |
2 |
Correct |
180 ms |
9468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
23552 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4049 ms |
24464 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
804 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
8700 KB |
Output is correct |
2 |
Correct |
50 ms |
8964 KB |
Output is correct |
3 |
Correct |
50 ms |
9224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4064 ms |
21788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
24576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |