Submission #1330358

#TimeUsernameProblemLanguageResultExecution timeMemory
133035824ta_tdanhPairs (IOI07_pairs)C++20
100 / 100
284 ms293852 KiB
#include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define ALL(A) A.begin(), A.end()
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define FOR2(i, l, r) for (int i = l; i >= r; i--)
#define ce cout<<endl;
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using str = string;
using T = pair<ll,int>;
const ll INF = (ll)1e18;
const int N = 1e5;
// Author : T_Danh - Tri An High School 

void solve1(){
    int n, d , m;
    cin >> n >> d >> m;
    vector<int> cnt(m + 1 , 0);
    FOR(i,1,n){
        int pos ;
        cin >> pos;
        cnt[pos] ++ ;
    }
    ll cur = 0;
    ll res = 0;
    FOR(i,1,m){
        res += cur * cnt[i];
        cur += cnt[i];
        res += cnt[i] * (cnt[i] - 1) / 2;
        if(i > d){
            cur -= cnt[i - d];
        }
    }
    cout << res <<endl;
}
struct BIT{
    int n;
    vector<ll> st;
    BIT(int _n){
        n = _n;
        st.resize(n + 1 , 0 );
    }
    void upd(int x, int val){
        for(;x <= n ; x += x & -x){
            st[x] += val;;
        }
    }
    ll get(int x){
        ll cnt =  0;
        for(;x > 0  ; x -= x & - x){
            cnt += st[x];
        }
        return cnt;
    }
    ll query(int l , int r){
        if(l > r) swap(l , r);
        return get(r ) - get(l - 1 ); 
    }
};
void solve2(){
    int n , d,  m;
    cin >> n >> d >> m;
    vector<pii> P(n);
    vector<int> tmp;
    FOR(i,0,n - 1){
        int x , y;
        cin >> x >> y;
        P[i] = (make_pair(x + y , x - y));
        tmp.eb(x + y);
        tmp.eb(x - y);
    }
    sort(ALL(P));
    sort(ALL(tmp));
    tmp.erase(unique(ALL(tmp)) , tmp.end());
    BIT st(tmp.size()  + 1);
    function<int(int)> id = [&](int x){
        return lower_bound(ALL(tmp) , x) - tmp.begin()  + 1;
    };
    function<int(int)> id2 = [&](int x){
        return upper_bound(ALL(tmp) , x) - tmp.begin() ;
    };

    ll res = 0;
    int l = 0;
    for(int r = 0 ; r < n ; ++ r){
        while(l < r && P[r].fi - P[l].fi > d){
            st.upd(id(P[l].se) , - 1);
            l ++;
        }
        res += st.query(id(P[r].se - d) , id2(P[r].se + d));
        st.upd(id(P[r].se) , 1);
    }
    cout << res <<endl;
}
struct BIT2{
    int n ;
    vector<vector<vector<int>>> st;
    BIT2(int _n){
        n = _n;
        st.resize(n + 1 , vector<vector<int>>(n + 1 , vector<int>(n + 1 , 0)));
    }
    void upd(int x ,int y , int z , int val){
        for(int i = x; i <= n; i += i & -i){
            for(int j = y; j <= n; j += j & -j){
                for(int k = z; k <= n; k += k & -k){
                    st[i][j][k] += val;
                }
            }
        }
    }
    int get(int x , int y , int z ){
        int res = 0;
        for(int i = x; i > 0; i -= i & -i){
            for(int j = y; j > 0; j -= j & -j){
                for(int k = z; k > 0; k -= k & -k){
                   res += st[i][j][k]; 
                }
            }
        }
        return res;
    }
    int query(int x1 , int x2 , int y1, int y2 , int z1 , int  z2){
        return get(x2,y2,z2)
            - get(x1-1,y2,z2)
            - get(x2,y1-1,z2)
            - get(x2,y2,z1-1)
            + get(x1-1,y1-1,z2)
            + get(x1-1,y2,z1-1)
            + get(x2,y1-1,z1-1)
            - get(x1-1,y1-1,z1-1);    
    }
};
void solve3(){
    int n , d , m;
    cin >> n >> d >> m;
    vector<array<int,4>> A(n);
    FOR(i,0,n-1){
        int x , y, z;
        cin >> x >> y >> z;
        A[i] = {x + y + z , x + y - z , x - y + z , x - y - z};
    }
    sort(ALL(A));
    int l = 0 , h = -1;
    BIT2 st(5 * m + 5);
    ll res = 0;
    FOR(i,0,n-1){
        while(h + 1< n && A[ h + 1][0] - A[i][0] <= d){
            h ++;
            st.upd(A[h][1] + 2*m + 1, A[h][2] + 2*m + 1, A[h][3] + 2*m + 1, 1);
        }
        while(l < n && A[i][0] - A[l][0] > d){
            st.upd(A[l][1] + 2*m + 1, A[l][2] + 2*m + 1, A[l][3] + 2*m + 1, -1);
            l ++;
        }
        res += st.query(max(1 , A[i][1] + 2 * m + 1 - d) ,min(4 * m + 1 , A[i][1] + 2 * m + 1  + d),
                        max(1 , A[i][2] + 2 * m + 1 - d) ,min(4 * m + 1 , A[i][2] + 2 * m + 1  + d),
                        max(1 , A[i][3] + 2 * m + 1 - d) ,min(4 * m + 1 , A[i][3] + 2 * m + 1 + d));
        res--;
    }
    cout << res / 2 <<endl;
}


void solve() {
    int b;
    cin >> b;
    if(b == 1){
        solve1();
    }
    else if(b == 2){
        solve2();
    }
    else{
        solve3();
    }
}   
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define NAME "odometer"
    if (fopen(NAME".in", "r"))
        freopen(NAME".in", "r", stdin),
        freopen(NAME".out", "w", stdout);

    int t = 1;
    while (t--) {
        solve();
    }
    return 0;
}

Compilation message (stderr)

pairs.cpp: In function 'int main()':
pairs.cpp:189:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  189 |         freopen(NAME".in", "r", stdin),
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:190:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  190 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...