답안 #396687

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396687 2021-04-30T15:28:24 Z MarcoMeijer Pairs (IOI07_pairs) C++14
30 / 100
139 ms 11252 KB
#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<<20);

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;
}


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));

        memset(SEG,0,sizeof(SEG));
        
        int lb=0, ub=0;
        ll res = 0;
        FOR(p,sorted) {
            int xMinY = p.fi;
            int i = p.se;
            while(ub!=n-1 && abs(sorted[ub].fi - xMinY) <= d) {
                int j = sorted[ub].se;
                addSeg(x[j]+y[j],1);
                ub++;
            }
            while(abs(sorted[lb].fi - xMinY) > d) {
                int j = sorted[lb].se;
                addSeg(x[j]+y[j],-1);
                lb++;
            }
            res += ll(getSeg(x[i]+y[i]-d, x[i]+y[i]+d));
        }
        OUTL(res/2);
    }
}
# 결과 실행 시간 메모리 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 16 ms 652 KB Output is correct
2 Correct 14 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 712 KB Output is correct
2 Correct 19 ms 732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 728 KB Output is correct
2 Correct 21 ms 724 KB Output is correct
3 Correct 23 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 8524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 89 ms 10796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 125 ms 10888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 139 ms 11252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -