Submission #1308652

#TimeUsernameProblemLanguageResultExecution timeMemory
1308652TymondRoad Construction (JOI21_road_construction)C++20
0 / 100
10091 ms99824 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_hash{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

const int MAXN = 3e5 + 7;
const int BASE = (1 << 20);
vector<pll> tree[2 * BASE + 7];
ll siz[2 * BASE + 7];
pll A[MAXN];
ll n, L;
vll dist = {};
pll NOW;
map<pii, int> usu;

int query(int v, int l, int p, int a, int b, bool f){
    if(p < a || b < l){
        return 0;
    }

    if(a <= l && p <= b){
        if(f){
            int wsk = 0;
            while(wsk < sz(tree[v])){
                if(!usu[tree[v][wsk]]){
                    wsk++;
                    continue;
                }
                swap(tree[v][wsk], tree[v][sz(tree[v]) - 1]);
                tree[v].pop_back();
            }

            for(auto& ele : tree[v]){
                if(ele != NOW){
                   // cerr << ele << ' ' << NOW << ' ' << '\n';
                    dist.pb((ll)abs((NOW.fi - (NOW.fi + NOW.se) / 2) - (ele.fi - (ele.fi + ele.se) / 2)) + 
                        abs(((NOW.fi + NOW.se) / 2) - ((ele.fi + ele.se) / 2)));
                    //cerr << dist.back() << '\n';
                }
            }
        }

        return siz[v];
    }

    int mid = (l + p) / 2;
    return query(2 * v, l, mid, a, b, f) + query(2 * v + 1, mid + 1, p, a, b, f);
}

void upd(int ind, pii val, bool f){
    ind += BASE;
    while(ind > 0){
        if(f){
            tree[ind].pb(val);
            siz[ind]++;
        }else{
            siz[ind]++;
        }

        ind /= 2;
    }
}

void del(int ind, pii val){
    usu[val] = 1;
}

ll count(ll K, bool gettingPairs){
    for(int i = 1; i <= 2 * BASE; i++){
        siz[i] = 0LL;
    }

    map<ll, int> M;
    for(int i = 1; i <= n; i++){
        M[A[i].fi] = M[A[i].fi - K] = M[A[i].fi + K] = 1;
    }

    int ch = 0;
    for(auto& ele : M){
        ele.se = ch++;
    }

    vector<pair<pii, pii>> vec = {};
    for(int i = 1; i <= n; i++){
        vec.pb(mp(mp(A[i].se - K, 0), mp(M[A[i].fi - K], M[A[i].fi + K])));
        vec.pb(mp(mp(A[i].se, 1), mp(A[i].fi, A[i].se)));
        vec.pb(mp(mp(A[i].se + K, 2), mp(A[i].fi, A[i].se)));
        vec.pb(mp(mp(A[i].se + K, 3), mp(A[i].fi, A[i].se)));
    }

    sort(all(vec));

    ll res = 0LL;
    for(auto ele : vec){
        if(ele.fi.se == 0){
            res -= query(1, 0, BASE - 1, ele.se.fi, ele.se.se, false);
        }else if(ele.fi.se == 2){
            NOW = ele.se;
            res += query(1, 0, BASE - 1, M[ele.se.fi - K], M[ele.se.fi + K], gettingPairs);
            res--;
        }else if(ele.fi.se == 1){
            upd(M[ele.se.fi], ele.se, gettingPairs);
        }else if((ele.fi.se == 3) && gettingPairs){
            del(M[ele.se.fi], ele.se);
        }
    }

    return res / 2;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> L;
    for(int i = 1; i <= n; i++){
        cin >> A[i].fi >> A[i].se;
        pii val = mp(A[i].fi + A[i].se, A[i].se - A[i].fi);
        A[i] = val;
        //cerr << A[i] << '\n';
    }

    ll l = 1;
    ll p = (ll)4e9;
    ll mid;
    while(l < p){
        mid = (l + p) / 2;
        //cerr << l << ' ' << p << ' ' << mid << ' ' << count(mid, false) << '\n';
        if(count(mid, false) >= L){
            p = mid;
        }else{
            l = mid + 1;
        }
    }
   // debug(l);
    count(l - 1, true);
    sort(all(dist));
    //debug(dist);
    vll ndist = {};
    for(int i = 0; i < sz(dist); i += 2){
        ndist.pb(dist[i]);
    }
    dist = ndist;
    //debug(dist);

    while(sz(dist) < L){
        dist.pb(l);
    }

    for(auto ele : dist){
        cout << ele << '\n';
    }
    
    return 0;
}
#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...