#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];
void addSeg(int i, int v, int l=0, int r=N-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=N-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 d, int delta = -1) {
memset(SEG,0,sizeof(SEG));
int lb=0, ub=0;
ll res = 0;
FOR(p,sorted1) {
int xMinY = p.fi;
int i = p.se;
while(ub!=sorted2.size() && abs(sorted2[ub].fi - xMinY) <= d) {
int j = sorted2[ub].se;
addSeg(x[j]+y[j],1);
ub++;
}
while(lb < ub && abs(sorted2[lb].fi - xMinY) > d) {
int j = sorted2[lb].se;
addSeg(x[j]+y[j],-1);
lb++;
}
res += ll(getSeg(x[i]+y[i]-d, x[i]+y[i]+d) + 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) {
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:97: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]
97 | while(ub!=sorted2.size() && abs(sorted2[ub].fi - xMinY) <= d) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
14 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
632 KB |
Output is correct |
2 |
Correct |
20 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
716 KB |
Output is correct |
2 |
Correct |
21 ms |
728 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 |
3 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
4060 KB |
Output is correct |
2 |
Correct |
66 ms |
4076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
4032 KB |
Output is correct |
2 |
Correct |
97 ms |
4092 KB |
Output is correct |
3 |
Correct |
99 ms |
4048 KB |
Output is correct |
4 |
Correct |
106 ms |
4104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
4012 KB |
Output is correct |
2 |
Correct |
119 ms |
4032 KB |
Output is correct |
3 |
Correct |
101 ms |
4096 KB |
Output is correct |
4 |
Correct |
118 ms |
4040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
249 ms |
2416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
5404 KB |
Output is correct |
2 |
Correct |
475 ms |
5444 KB |
Output is correct |
3 |
Correct |
359 ms |
5440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1164 ms |
5528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3152 ms |
5772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |