Submission #396712

#TimeUsernameProblemLanguageResultExecution timeMemory
396712MarcoMeijerPairs (IOI07_pairs)C++14
100 / 100
2468 ms4088 KiB
#include <bits/stdc++.h> using namespace std; // macros typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> lll; typedef tuple<int, int, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<ll> vll; typedef vector<lll> vlll; #define REP(a,b,c) for(int a=int(b); a<int(c); a++) #define RE(a,c) REP(a,0,c) #define RE1(a,c) REP(a,1,c+1) #define REI(a,b,c) REP(a,b,c+1) #define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--) #define FOR(a,b) for(auto& a : b) #define all(a) a.begin(), a.end() #define INF 1e18 #define EPS 1e-9 #define pb push_back #define popb pop_back #define fi first #define se second mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // input template<class T> void IN(T& x) {cin >> x;} template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); } // output template<class T1, class T2> void OUT(const pair<T1,T2>& x); template<class T> void OUT(const vector<T>& x); template<class T> void OUT(const T& x) {cout << x;} template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); } template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);} template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);} template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); } template<class H> void OUTLS(const H& h) {OUTL(h); } template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); } // dp template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;} template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;} void program(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); program(); } //===================// // begin program // //===================// const int MX = 5e5; const int N = (1<<18); int b, n, d, m; int x[MX], y[MX], z[MX]; // segment tree int SEG[N*2]; int SegN=N; void addSeg(int i, int v, int l=0, int r=SegN-1, int p=1) { if(i < l || i > r) return; if(l == r) { SEG[p] += v; return; } int m=(l+r)/2; addSeg(i,v,l ,m,p*2 ); addSeg(i,v,m+1,r,p*2+1); SEG[p] = SEG[p*2] + SEG[p*2+1]; } int getSeg(int i, int j, int l=0, int r=SegN-1, int p=1) { if(j < l || i > r) return 0; if(i <= l && j >= r) return SEG[p]; int m=(l+r)/2; int a=getSeg(i,j,l ,m,p*2 ); int b=getSeg(i,j,m+1,r,p*2+1); return a+b; } ll get2d(const vii& sorted1, const vii& sorted2, int cd, int delta = -1) { memset(SEG,0,sizeof(int)*2*SegN); int lb=0, ub=0; ll res = 0; FOR(p,sorted1) { int xMinY = p.fi; int i = p.se; while(ub!=sorted2.size() && sorted2[ub].fi - xMinY <= cd) { int j = sorted2[ub].se; addSeg(x[j]+y[j],1); ub++; } while(lb < ub && xMinY - sorted2[lb].fi > cd) { int j = sorted2[lb].se; addSeg(x[j]+y[j],-1); lb++; } res += ll(getSeg(x[i]+y[i]-cd, x[i]+y[i]+cd) + delta); } return res; } void program() { IN(b,n,d,m); if(b == 1) { RE(i,n) IN(x[i]); sort(x,x+n); int lb=0, ub=0; ll res = 0; RE(i,n) { while(ub!=n-1 && abs(x[i]-x[ub+1]) <= d) ub++; while(abs(x[i]-x[lb]) > d) lb++; res += ll(ub-lb); } OUTL(res/2); } if(b == 2) { RE(i,n) IN(x[i],y[i]); vii sorted; RE(i,n) sorted.pb({x[i]-y[i],i}); sort(all(sorted)); ll res = get2d(sorted, sorted, d); OUTL(res/2); } if(b == 3) { SegN = (1<<8); RE(i,n) IN(x[i],y[i],z[i]); RE(i,n) x[i]--, y[i]--, z[i]--; vector<vii> sortedZ; sortedZ.resize(m); RE(i,n) sortedZ[z[i]].pb({x[i]-y[i],i}); RE(i,m) sort(all(sortedZ[i])); ll res = 0; RE(i,m) RE(j,m) { if(abs(i-j) > d) continue; res += get2d(sortedZ[i],sortedZ[j],d-abs(i-j), i == j ? -1 : 0); } OUTL(res/2); } }

Compilation message (stderr)

pairs.cpp: In function 'll get2d(const vii&, const vii&, int, int)':
pairs.cpp:98:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         while(ub!=sorted2.size() && sorted2[ub].fi - xMinY <= cd) {
      |               ~~^~~~~~~~~~~~~~~~
#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...