Submission #386553

# Submission time Handle Problem Language Result Execution time Memory
386553 2021-04-06T19:29:55 Z PurpleCrayon Road Construction (JOI21_road_construction) C++17
100 / 100
9453 ms 35820 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(v.size())
typedef long long ll;
const int MAXN = 2.5e5+10;

struct FT {
    int n, bit[MAXN];
    void init(int _n){ n = _n+1; memset(bit, 0, sizeof(bit)); }
    int qry(int idx) { int ret = 0; for (++idx; idx > 0; idx -= idx & -idx) ret += bit[idx]; return ret; }
    int qry(int l, int r) { return qry(r) - qry(l - 1); }
    void upd(int idx, int delta) { for (++idx; idx < n; idx += idx & -idx) bit[idx] += delta; }
} ft;

string to_str(ar<ll, 2> a){ return "(" + to_string(a[0]) + ", " + to_string(a[1]) + ")"; }

ll dist(ar<ll, 2> a, ar<ll, 2> b){
    return abs<ll>(a[0]-b[0]) + abs<ll>(a[1]-b[1]);
}

int n, k;
ar<ll, 2> a[MAXN], oa[MAXN];
ar<ll, 3> b[MAXN];
int cmp[MAXN];
map<ll, int> mp;

int main(){
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> a[i][0] >> a[i][1], b[i][0] = a[i][0]+a[i][1], b[i][1] = a[i][0]-a[i][1], b[i][2] = i;
        oa[i] = a[i];
    }

    sort(a, a+n), sort(b, b+n);

    for (int i = 0; i < n; i++) mp[b[i][1]]++;
    int cc=0;
    for (auto& it : mp) it.second=cc++;
    for (int i = 0; i < n; i++) cmp[i] = mp[b[i][1]];

    auto cnt = [&](ll x) -> ll { //rotate 90 degrees, now want to find the point in some square
        ft.init(n);
        
        ll ans=0;
        for (int i=0, j=i; i < n; i++){
            for (; j < n && b[j][0]-b[i][0] <= x; j++) 
                ft.upd(cmp[j], 1);

            const ll cl = b[i][1]-x, cr = b[i][1]+x;
    
            auto rit = mp.upper_bound(cr);
            if (rit != mp.begin()){
                rit = prev(rit);

                auto lit = mp.lower_bound(cl);
                if (lit != mp.end()){
                    ans += ft.qry(lit->second, rit->second);
                }
            }
            if (ans-(i+1) >= k) return ans-(i+1);
            ft.upd(cmp[i], -1);
        }
        return ans-n;
    };

    ll lo=-1, hi=1e18+10, mid=(lo+hi)/2;
    while (lo < mid && mid < hi) {
        if (cnt(mid) >= k) hi = mid;
        else lo = mid;
        mid = (lo+hi)/2;
    }

    vector<ll> ans{hi}; ans.reserve(k);
    
    set<pair<ll, int>> s;
    ll x=hi-1;
    for (int i=0, j=i; i < n; i++){
        assert(j >= i);
        for (; j < n && b[j][0]-b[i][0] <= x; j++) 
            s.insert({b[j][1], j});
        s.erase(s.find({b[i][1], i}));

        ll cl = b[i][1]-x, cr = b[i][1]+x;

        auto lit = s.lower_bound({cl, -1});
        while (lit != s.end() && lit->first <= cr) {
            int cv = lit->second;
            ll d = dist(oa[b[i][2]], oa[b[cv][2]]);

            //cerr << i << ' ' << cv << ' ' << to_str(b[i]) << ' ' << to_str(b[cv]) << endl;
            ans.push_back(d);
            lit = next(lit);
        }
    }

    sort(ans.begin(), ans.end());
    while (sz(ans) < k) ans.push_back(ans.back());

    for (auto& it : ans) cout << it << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5996 KB Output is correct
2 Correct 81 ms 5980 KB Output is correct
3 Correct 68 ms 6124 KB Output is correct
4 Correct 68 ms 6236 KB Output is correct
5 Correct 67 ms 4972 KB Output is correct
6 Correct 7 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1663 ms 34992 KB Output is correct
2 Correct 1657 ms 34824 KB Output is correct
3 Correct 63 ms 5996 KB Output is correct
4 Correct 1474 ms 34700 KB Output is correct
5 Correct 1371 ms 34796 KB Output is correct
6 Correct 1400 ms 34952 KB Output is correct
7 Correct 1199 ms 34116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3842 ms 31724 KB Output is correct
2 Correct 5263 ms 31684 KB Output is correct
3 Correct 3 ms 1260 KB Output is correct
4 Correct 1055 ms 31724 KB Output is correct
5 Correct 1046 ms 16236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3842 ms 31724 KB Output is correct
2 Correct 5263 ms 31684 KB Output is correct
3 Correct 3 ms 1260 KB Output is correct
4 Correct 1055 ms 31724 KB Output is correct
5 Correct 1046 ms 16236 KB Output is correct
6 Correct 5790 ms 31684 KB Output is correct
7 Correct 5404 ms 31724 KB Output is correct
8 Correct 4 ms 1260 KB Output is correct
9 Correct 3 ms 1260 KB Output is correct
10 Correct 5071 ms 30480 KB Output is correct
11 Correct 1126 ms 31724 KB Output is correct
12 Correct 1080 ms 16236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5996 KB Output is correct
2 Correct 81 ms 5980 KB Output is correct
3 Correct 68 ms 6124 KB Output is correct
4 Correct 68 ms 6236 KB Output is correct
5 Correct 67 ms 4972 KB Output is correct
6 Correct 7 ms 1388 KB Output is correct
7 Correct 2394 ms 17516 KB Output is correct
8 Correct 2459 ms 17472 KB Output is correct
9 Correct 69 ms 6264 KB Output is correct
10 Correct 1801 ms 15980 KB Output is correct
11 Correct 1297 ms 15368 KB Output is correct
12 Correct 523 ms 11244 KB Output is correct
13 Correct 453 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5996 KB Output is correct
2 Correct 81 ms 5980 KB Output is correct
3 Correct 68 ms 6124 KB Output is correct
4 Correct 68 ms 6236 KB Output is correct
5 Correct 67 ms 4972 KB Output is correct
6 Correct 7 ms 1388 KB Output is correct
7 Correct 1663 ms 34992 KB Output is correct
8 Correct 1657 ms 34824 KB Output is correct
9 Correct 63 ms 5996 KB Output is correct
10 Correct 1474 ms 34700 KB Output is correct
11 Correct 1371 ms 34796 KB Output is correct
12 Correct 1400 ms 34952 KB Output is correct
13 Correct 1199 ms 34116 KB Output is correct
14 Correct 3842 ms 31724 KB Output is correct
15 Correct 5263 ms 31684 KB Output is correct
16 Correct 3 ms 1260 KB Output is correct
17 Correct 1055 ms 31724 KB Output is correct
18 Correct 1046 ms 16236 KB Output is correct
19 Correct 5790 ms 31684 KB Output is correct
20 Correct 5404 ms 31724 KB Output is correct
21 Correct 4 ms 1260 KB Output is correct
22 Correct 3 ms 1260 KB Output is correct
23 Correct 5071 ms 30480 KB Output is correct
24 Correct 1126 ms 31724 KB Output is correct
25 Correct 1080 ms 16236 KB Output is correct
26 Correct 2394 ms 17516 KB Output is correct
27 Correct 2459 ms 17472 KB Output is correct
28 Correct 69 ms 6264 KB Output is correct
29 Correct 1801 ms 15980 KB Output is correct
30 Correct 1297 ms 15368 KB Output is correct
31 Correct 523 ms 11244 KB Output is correct
32 Correct 453 ms 9708 KB Output is correct
33 Correct 9453 ms 35652 KB Output is correct
34 Correct 9195 ms 35820 KB Output is correct
35 Correct 7732 ms 31316 KB Output is correct
36 Correct 1229 ms 19772 KB Output is correct
37 Correct 1114 ms 20232 KB Output is correct
38 Correct 1327 ms 18568 KB Output is correct