Submission #1116669

# Submission time Handle Problem Language Result Execution time Memory
1116669 2024-11-22T07:00:05 Z _callmelucian Road Construction (JOI21_road_construction) C++14
100 / 100
7033 ms 19812 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 250003;
vector<ll> cmpSum, cmpDiff;
pl a[mn];
ll n;

struct BIT {
    vector<int> tr;
    BIT (int sz) : tr(sz + 1) {}

    int p (int k) { return k & -k; }

    void update (int k, int val) {
        for (; k < tr.size(); k += p(k)) tr[k] += val;
    }

    int preSum (int k, int ans = 0) {
        for (; k; k -= p(k)) ans += tr[k];
        return ans;
    }

    void refresh() { fill(all(tr), 0); }
} treeSum(mn), treeDiff(mn);

int getSum (ll targ) {
    return lower_bound(all(cmpSum), targ) - cmpSum.begin() + 1;
}

int getDiff (ll targ) {
    return lower_bound(all(cmpDiff), targ) - cmpDiff.begin() + 1;
}

int pointSum (int i) {
    ll x, y; tie(x, y) = a[i];
    return lower_bound(all(cmpSum), x + y) - cmpSum.begin() + 1;
}

int pointDiff (int i) {
    ll x, y; tie(x, y) = a[i];
    return lower_bound(all(cmpDiff), x - y) - cmpDiff.begin() + 1;
}

ll countClose (ll ub) {
    ll cnt = 0;
    treeSum.refresh(), treeDiff.refresh();
    for (int L = 0, R = 0; R < n; R++) {
        treeSum.update(pointSum(R), 1);
        treeDiff.update(pointDiff(R), 1);
        while (a[R].first - a[L].first > ub) {
            treeSum.update(pointSum(L), -1);
            treeDiff.update(pointDiff(L), -1);
            L++;
        }
        cnt += L;

        ll x, y; tie(x, y) = a[R];
        cnt += treeSum.preSum(getSum(x + y - ub) - 1);
        cnt += treeDiff.preSum(getDiff(x - y - ub) - 1);
    }
    return n * (n - 1) / 2 - cnt;
}

ll distance (ll x, ll y, ll i, ll j) {
    return abs(x - i) + abs(y - j);
}

void getPoints (ll ub, ll k, vector<ll> &ans) {
    set<pl> s;
    for (int L = 0, R = 0; R < n; R++) {
        ll x, y; tie(x, y) = a[R];
        s.emplace(y, x);

        while (a[R].first - a[L].first > ub) {
            ll x, y; tie(x, y) = a[L++];
            s.erase(make_pair(y, x));
        }

        for (auto it = s.lower_bound(make_pair(y - ub, 0)); it != s.end() && it->first <= y + ub; it++) {
            ll cur = distance(it->second, it->first, x, y);
            if (0 < cur && cur <= ub) ans.push_back(cur);
            if (ans.size() == k) break;
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll k; cin >> n >> k;
    for (int i = 0; i < n; i++) {
        ll x, y; cin >> x >> y;
        cmpSum.push_back(x + y), cmpDiff.push_back(x - y);
        a[i] = {x, y};
    }
    sort(a, a + n), sort(all(cmpSum)), sort(all(cmpDiff));
    filter(cmpSum), filter(cmpDiff);

    ll ans = 0, save = 0;
    for (ll mask = 1LL << 31; mask > 0; mask >>= 1) {
        ll cur = countClose(ans | mask);
        if (cur < k) ans |= mask, save = cur;
    }

    vector<ll> vec;
    getPoints(ans, save, vec);
    sort(all(vec));

    for (ll u : vec) cout << u << "\n";
    for (int i = 0; i < k - save; i++) cout << ans + 1 << "\n";

    return 0;
}

Compilation message

road_construction.cpp: In member function 'void BIT::update(int, int)':
road_construction.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for (; k < tr.size(); k += p(k)) tr[k] += val;
      |                ~~^~~~~~~~~~~
road_construction.cpp: In function 'void getPoints(ll, ll, std::vector<long long int>&)':
road_construction.cpp:92:28: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   92 |             if (ans.size() == k) break;
      |                 ~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7112 KB Output is correct
2 Correct 46 ms 7056 KB Output is correct
3 Correct 41 ms 7120 KB Output is correct
4 Correct 38 ms 7228 KB Output is correct
5 Correct 43 ms 6088 KB Output is correct
6 Correct 10 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2175 ms 17320 KB Output is correct
2 Correct 2261 ms 17532 KB Output is correct
3 Correct 35 ms 7112 KB Output is correct
4 Correct 2208 ms 17380 KB Output is correct
5 Correct 2225 ms 17428 KB Output is correct
6 Correct 2184 ms 17324 KB Output is correct
7 Correct 2112 ms 17644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5872 ms 15732 KB Output is correct
2 Correct 5827 ms 15736 KB Output is correct
3 Correct 3 ms 2384 KB Output is correct
4 Correct 2212 ms 13320 KB Output is correct
5 Correct 2312 ms 15976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5872 ms 15732 KB Output is correct
2 Correct 5827 ms 15736 KB Output is correct
3 Correct 3 ms 2384 KB Output is correct
4 Correct 2212 ms 13320 KB Output is correct
5 Correct 2312 ms 15976 KB Output is correct
6 Correct 5904 ms 15720 KB Output is correct
7 Correct 5868 ms 15724 KB Output is correct
8 Correct 4 ms 2384 KB Output is correct
9 Correct 4 ms 2384 KB Output is correct
10 Correct 5655 ms 15728 KB Output is correct
11 Correct 2261 ms 13336 KB Output is correct
12 Correct 2268 ms 15976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7112 KB Output is correct
2 Correct 46 ms 7056 KB Output is correct
3 Correct 41 ms 7120 KB Output is correct
4 Correct 38 ms 7228 KB Output is correct
5 Correct 43 ms 6088 KB Output is correct
6 Correct 10 ms 2384 KB Output is correct
7 Correct 2546 ms 12440 KB Output is correct
8 Correct 2528 ms 12436 KB Output is correct
9 Correct 43 ms 7368 KB Output is correct
10 Correct 2123 ms 11592 KB Output is correct
11 Correct 1942 ms 11600 KB Output is correct
12 Correct 756 ms 10236 KB Output is correct
13 Correct 874 ms 11528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7112 KB Output is correct
2 Correct 46 ms 7056 KB Output is correct
3 Correct 41 ms 7120 KB Output is correct
4 Correct 38 ms 7228 KB Output is correct
5 Correct 43 ms 6088 KB Output is correct
6 Correct 10 ms 2384 KB Output is correct
7 Correct 2175 ms 17320 KB Output is correct
8 Correct 2261 ms 17532 KB Output is correct
9 Correct 35 ms 7112 KB Output is correct
10 Correct 2208 ms 17380 KB Output is correct
11 Correct 2225 ms 17428 KB Output is correct
12 Correct 2184 ms 17324 KB Output is correct
13 Correct 2112 ms 17644 KB Output is correct
14 Correct 5872 ms 15732 KB Output is correct
15 Correct 5827 ms 15736 KB Output is correct
16 Correct 3 ms 2384 KB Output is correct
17 Correct 2212 ms 13320 KB Output is correct
18 Correct 2312 ms 15976 KB Output is correct
19 Correct 5904 ms 15720 KB Output is correct
20 Correct 5868 ms 15724 KB Output is correct
21 Correct 4 ms 2384 KB Output is correct
22 Correct 4 ms 2384 KB Output is correct
23 Correct 5655 ms 15728 KB Output is correct
24 Correct 2261 ms 13336 KB Output is correct
25 Correct 2268 ms 15976 KB Output is correct
26 Correct 2546 ms 12440 KB Output is correct
27 Correct 2528 ms 12436 KB Output is correct
28 Correct 43 ms 7368 KB Output is correct
29 Correct 2123 ms 11592 KB Output is correct
30 Correct 1942 ms 11600 KB Output is correct
31 Correct 756 ms 10236 KB Output is correct
32 Correct 874 ms 11528 KB Output is correct
33 Correct 7033 ms 19704 KB Output is correct
34 Correct 6765 ms 19700 KB Output is correct
35 Correct 5606 ms 19568 KB Output is correct
36 Correct 1963 ms 17564 KB Output is correct
37 Correct 2077 ms 17520 KB Output is correct
38 Correct 2181 ms 19812 KB Output is correct