Submission #197661

#TimeUsernameProblemLanguageResultExecution timeMemory
197661arnold518Pairs (IOI07_pairs)C++14
100 / 100
1752 ms134052 KiB
#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 (stderr)

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);
            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...