#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
struct Point
{
int x, y, z;
Point() {}
Point(int x) : x(x), y(0), z(0) {}
Point(int x, int y) : x(x), y(y), z(0) {}
Point(int x, int y, int z) : x(x), y(y), z(z) {}
bool operator < (const Point &p) const { return x<p.x; }
};
struct Query
{
int x, y1, y2, d;
bool operator < (const Query &p) const { return x<p.x; }
};
int B, N, D, M;
Point A[MAXN+10];
ll ans;
struct BIT
{
vector<int> tree;
BIT() : tree(2*M+10) {}
void update(int i) { for(; i<2*M; i+=(i&-i)) tree[i]++; }
int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
int query(int l, int r) { return query(r)-query(l-1); }
};
ll f(vector<Point> &V, vector<Point> &S, vector<int> &R)
{
int i, j;
vector<Point> V2;
for(i=0; i<V.size(); i++) V2.push_back(Point(V[i].x+V[i].y-1, V[i].x-V[i].y+M));
vector<Query> S2;
for(i=0; i<S.size(); i++)
{
int x1=S[i].x+S[i].y-1-R[i], x2=S[i].x+S[i].y-1+R[i];
int y1=S[i].x-S[i].y+M-R[i], y2=S[i].x-S[i].y+M+R[i];
x1=max(x1, 1); x2=min(x2, 2*M-1);
y1=max(y1, 1); y2=min(y2, 2*M-1);
S2.push_back({x1-1, y1, y2, -1});
S2.push_back({x2, y1, y2, 1});
}
sort(S2.begin(), S2.end());
sort(V2.begin(), V2.end());
BIT bit=BIT();
ll ret=0;
for(i=0, j=0; i<S2.size(); i++)
{
for(; j<V2.size() && V2[j].x<=S2[i].x; j++) bit.update(V2[j].y);
ret+=S2[i].d*bit.query(S2[i].y1, S2[i].y2);
}
return ret;
}
vector<Point> V[100], S[100];
vector<int> R[100];
int main()
{
int i, j;
scanf("%d%d%d%d", &B, &N, &D, &M);
for(i=1; i<=N; i++)
{
if(B==1) scanf("%d", &A[i].x);
if(B==2) scanf("%d%d", &A[i].x, &A[i].y);
if(B==3) scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].z);
}
if(B==1)
{
sort(A+1, A+N+1);
for(i=1; i<=N; i++) ans+=upper_bound(A+1, A+N+1, Point(A[i].x+D))-lower_bound(A+1, A+N+1, Point(A[i].x-D));
ans-=N; ans/=2;
printf("%lld", ans);
return 0;
}
if(B==2)
{
vector<Point> V, S;
vector<int> R;
for(i=1; i<=N; i++) V.push_back(A[i]), S.push_back(A[i]), R.push_back(D);
ans=f(V, S, R);
ans-=N; ans/=2;
printf("%lld", ans);
return 0;
}
if(B==3)
{
for(i=1; i<=N; i++)
{
V[A[i].z].push_back(A[i]);
for(j=1; j<=M; j++)
{
if(D-abs(j-A[i].z)<0) continue;
S[j].push_back(A[i]);
R[j].push_back(D-abs(j-A[i].z));
}
}
for(i=1; i<=M; i++) ans+=f(V[i], S[i], R[i]);
ans-=N; ans/=2;
printf("%lld", ans);
return 0;
}
}
Compilation message
pairs.cpp: In function 'll f(std::vector<Point>&, std::vector<Point>&, std::vector<int>&)':
pairs.cpp:44:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<V.size(); i++) V2.push_back(Point(V[i].x+V[i].y-1, V[i].x-V[i].y+M));
~^~~~~~~~~
pairs.cpp:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<S.size(); i++)
~^~~~~~~~~
pairs.cpp:62:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0, j=0; i<S2.size(); i++)
~^~~~~~~~~~
pairs.cpp:64:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j<V2.size() && V2[j].x<=S2[i].x; j++) bit.update(V2[j].y);
~^~~~~~~~~~
pairs.cpp: In function 'int main()':
pairs.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &B, &N, &D, &M);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:80:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
if(B==1) scanf("%d", &A[i].x);
~~~~~^~~~~~~~~~~~~~~
pairs.cpp:81:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
if(B==2) scanf("%d%d", &A[i].x, &A[i].y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:82:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
if(B==3) scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1528 KB |
Output is correct |
2 |
Correct |
26 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
1656 KB |
Output is correct |
2 |
Correct |
33 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
1548 KB |
Output is correct |
2 |
Correct |
36 ms |
1528 KB |
Output is correct |
3 |
Correct |
34 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1020 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
10712 KB |
Output is correct |
2 |
Correct |
50 ms |
10780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
10828 KB |
Output is correct |
2 |
Correct |
63 ms |
10692 KB |
Output is correct |
3 |
Correct |
64 ms |
10652 KB |
Output is correct |
4 |
Correct |
64 ms |
10784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
10748 KB |
Output is correct |
2 |
Correct |
76 ms |
10752 KB |
Output is correct |
3 |
Correct |
69 ms |
10652 KB |
Output is correct |
4 |
Correct |
69 ms |
10752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1400 KB |
Output is correct |
2 |
Correct |
9 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
10088 KB |
Output is correct |
2 |
Correct |
265 ms |
27864 KB |
Output is correct |
3 |
Correct |
205 ms |
27556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
457 ms |
41488 KB |
Output is correct |
2 |
Correct |
1366 ms |
107276 KB |
Output is correct |
3 |
Correct |
920 ms |
109412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1152 ms |
92604 KB |
Output is correct |
2 |
Correct |
1752 ms |
133408 KB |
Output is correct |
3 |
Correct |
1551 ms |
134052 KB |
Output is correct |