Submission #641938

# Submission time Handle Problem Language Result Execution time Memory
641938 2022-09-17T23:36:38 Z maomao90 Road Construction (JOI21_road_construction) C++17
6 / 100
392 ms 8652 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 250005;

int n, k;
ii xy[MAXN];
vll ans;

bool isPos(ll v) {
    int lft = 0;
    set<ii> st;
    int rk = k;
    REP (i, 0, n) {
        auto [x, y] = xy[i];
        while (lft < i && x - xy[lft].FI > v) {
            st.erase({xy[lft].SE, lft});
            lft++;
        }
        auto ptr = st.lower_bound({y, -1});
        auto optr = ptr;
        while (ptr != st.end() && ptr -> FI - y <= v) {
            if (--rk <= 0) {
                return 1;
            }
            ptr = next(ptr);
        }
        ptr = optr;
        while (ptr != st.begin() && y - prev(ptr) -> FI <= v) {
            if (--rk <= 0) {
                return 1;
            }
            ptr = prev(ptr);
        }
        st.insert({y, i});
    }
    return 0;
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> k;
    REP (i, 0, n) {
        int x, y; cin >> x >> y;
        xy[i] = {x - y, x + y};
    }
    sort(xy, xy + n);
    ll lo = 0, hi = 4e9 + 5, mid;
    while (lo < hi) {
        mid = lo + hi >> 1;
        if (isPos(mid)) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    int lft = 0;
    set<ii> st;
    lo--;
    REP (i, 0, n) {
        auto [x, y] = xy[i];
        while (lft < i && x - xy[lft].FI > lo) {
            cerr << " - " << xy[lft].SE << ' ' << lft << '\n';
            auto ptr = st.find({xy[lft].SE, lft});
            assert(ptr != st.end());
            st.erase(ptr);
            lft++;
        }
        auto ptr = st.lower_bound({y, -1});
        auto optr = ptr;
        cerr << x << ' ' << y << '\n';
        while (ptr != st.end() && ptr -> FI - y <= lo) {
            ans.pb(max((ll) x - xy[ptr -> SE].FI, (ll) ptr -> FI - y));
            cerr << ' ' << ptr -> FI << ' ' << ptr -> SE << '\n';
            ptr = next(ptr);
        }
        ptr = optr;
        while (ptr != st.begin() && y - prev(ptr) -> FI <= lo) {
            ptr = prev(ptr);
            ans.pb(max((ll) x - xy[ptr -> SE].FI, (ll) y - ptr -> FI));
            cerr << ' ' << ptr -> FI << ' ' << ptr -> SE << '\n';
        }
        st.insert({y, i});
    }
    while (SZ(ans) < k) {
        ans.pb(lo + 1);
    }
    sort(ALL(ans));
    for (ll i : ans) {
        cout << i << '\n';
    }
    return 0;
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:84:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |         mid = lo + hi >> 1;
      |               ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4972 KB Output is correct
2 Correct 129 ms 4972 KB Output is correct
3 Incorrect 108 ms 5096 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 392 ms 8652 KB Output is correct
2 Correct 356 ms 8644 KB Output is correct
3 Correct 103 ms 4972 KB Output is correct
4 Correct 289 ms 8388 KB Output is correct
5 Correct 282 ms 8476 KB Output is correct
6 Correct 295 ms 8604 KB Output is correct
7 Correct 261 ms 7976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 107 ms 7256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 107 ms 7256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4972 KB Output is correct
2 Correct 129 ms 4972 KB Output is correct
3 Incorrect 108 ms 5096 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4972 KB Output is correct
2 Correct 129 ms 4972 KB Output is correct
3 Incorrect 108 ms 5096 KB Output isn't correct
4 Halted 0 ms 0 KB -