#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
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) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
728 KB |
Output is correct |
2 |
Correct |
15 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
716 KB |
Output is correct |
2 |
Correct |
20 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
616 KB |
Output is correct |
2 |
Correct |
21 ms |
724 KB |
Output is correct |
3 |
Correct |
19 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2380 KB |
Output is correct |
2 |
Correct |
4 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
4040 KB |
Output is correct |
2 |
Correct |
72 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
4032 KB |
Output is correct |
2 |
Correct |
105 ms |
4008 KB |
Output is correct |
3 |
Correct |
96 ms |
4084 KB |
Output is correct |
4 |
Correct |
96 ms |
4016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
4088 KB |
Output is correct |
2 |
Correct |
120 ms |
4024 KB |
Output is correct |
3 |
Correct |
102 ms |
4048 KB |
Output is correct |
4 |
Correct |
104 ms |
4032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
384 KB |
Output is correct |
2 |
Correct |
12 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
2764 KB |
Output is correct |
2 |
Correct |
273 ms |
2760 KB |
Output is correct |
3 |
Correct |
196 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
685 ms |
2664 KB |
Output is correct |
2 |
Correct |
1913 ms |
2660 KB |
Output is correct |
3 |
Correct |
1202 ms |
2756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1712 ms |
2900 KB |
Output is correct |
2 |
Correct |
2468 ms |
2900 KB |
Output is correct |
3 |
Correct |
1571 ms |
3744 KB |
Output is correct |